com.google.appengine.api.LifecycleManager Java Examples

The following examples show how to use com.google.appengine.api.LifecycleManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModuleStartServlet.java    From HotswapAgentExamples with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {

	logger.info("module start");
	LifecycleManager.getInstance().setShutdownHook(this);
	// ModulesService modulesApi =
	// ModulesServiceFactory.getModulesService();
	// if ("0".equals(modulesApi.getCurrentInstanceId())) {
	//
	// logger.info("module start");
	// LifecycleManager.getInstance().setShutdownHook(this);
	//
	// }

	return;
}
 
Example #2
Source File: ShutdownServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
private void doHang(HttpServletResponse res) throws IOException {
  res.setContentType("text/plain");
  res.getWriter().println("Starting...");
  final Thread requestThread = Thread.currentThread();
  final boolean[] hookState = new boolean[] {false, false};
  LifecycleManager.getInstance()
      .setShutdownHook(
          new LifecycleManager.ShutdownHook() {
            public void shutdown() {
              hookState[0] = true; // Hook called
              LifecycleManager.getInstance().interruptAllRequests();
              hookState[1] = true;
            }
          });
  hang();
  res.getWriter().println("Hook: " + hookState[0] + " " + hookState[1]);
  res.getWriter().println("Exiting.");
}
 
Example #3
Source File: VmHealthServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
  // Check if the instance is shutting down, in that case return unhealthy (lameduck).
  LifecycleManager lifeCycleManager = LifecycleManager.getInstance();
  if (LifecycleManager.getInstance().isShuttingDown()) {
     long remainingShutdownTime = lifeCycleManager.getRemainingShutdownTime();
     response.sendError(HttpServletResponse.SC_BAD_GATEWAY,
                        "App is shutting down, time remaining: " + remainingShutdownTime + " ms");
     return;
  }
  String expectedVersion = request.getParameter("VersionID");
  String actualVersion = fullVersionId();
  if ((expectedVersion == null) || expectedVersion.equals(actualVersion)){
    response.setContentType("text/plain");
    response.getWriter().write("ok");
  } else {
    response.setContentType("text/plain");
    response.getWriter().write(
        String.format("version mismatch \"%s\" != \"%s\"", expectedVersion, actualVersion));
  }
}
 
Example #4
Source File: WorkerServlet.java    From io2014-codelabs with Apache License 2.0 6 votes vote down vote up
private void doPolling() {
  Queue notificationQueue = QueueFactory.getQueue("notification-delivery");

  Worker worker = new Worker(notificationQueue);
  while (!LifecycleManager.getInstance().isShuttingDown()) {
    boolean tasksProcessed = worker.processBatchOfTasks();
    ApiProxy.flushLogs();

    if (!tasksProcessed) {
      // Wait before trying to lease tasks again.
      try {
        Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED);
      } catch (InterruptedException e) {
        return;
      }
    }
  }

  log.info("Instance is shutting down");
}
 
Example #5
Source File: WorkerServlet.java    From solutions-mobile-backend-starter-java with Apache License 2.0 6 votes vote down vote up
private void doPolling() {
  Queue notificationQueue = QueueFactory.getQueue("notification-delivery");

  Worker worker = new Worker(notificationQueue);
  while (!LifecycleManager.getInstance().isShuttingDown()) {
    boolean tasksProcessed = worker.processBatchOfTasks();
    ApiProxy.flushLogs();

    if (!tasksProcessed) {
      // Wait before trying to lease tasks again.
      try {
        Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED);
      } catch (InterruptedException e) {
        return;
      }
    }
  }

  log.info("Instance is shutting down");
}
 
Example #6
Source File: PushNotificationWorkerServlet.java    From solutions-ios-push-notification-sample-backend-java with Apache License 2.0 6 votes vote down vote up
private void doPolling() {
  Queue notificationQueue = QueueFactory.getQueue("notification-delivery");

  PushNotificationWorker worker = new PushNotificationWorker(notificationQueue);
  while (!LifecycleManager.getInstance().isShuttingDown()) {
    boolean tasksProcessed = worker.processBatchOfTasks();
    ApiProxy.flushLogs();

    if (!tasksProcessed) {
      // Wait before trying to lease tasks again.
      try {
        Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED);
      } catch (InterruptedException e) {
        return;
      }
    }
  }

  log.info("Instance is shutting down");
}
 
Example #7
Source File: ShutdownServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void hang() {
  LifecycleManager runtime = LifecycleManager.getInstance();
  log.info("Shutting down: " + runtime.isShuttingDown());
  while (!runtime.isShuttingDown()) {
    try {
      log.info("Sleeping for 10s");
      Thread.sleep(10000);
    } catch (InterruptedException ex) {
      // expected
    }
    log.info("Shutting down: " + runtime.isShuttingDown());
  }
}
 
Example #8
Source File: ShutdownServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
private void doHello(HttpServletResponse res) throws IOException {
  log.info("Hello!");
  LifecycleManager.getInstance()
      .setShutdownHook(
          new LifecycleManager.ShutdownHook() {
            public void shutdown() {
              log.info("Goodbye, world!");
              LifecycleManager.getInstance().interruptAllRequests();
              if (Thread.interrupted()) {
                log.info("Interrupted");
              }
            }
          });
  res.getWriter().println("Hello, world!");
}
 
Example #9
Source File: ServersStartServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public void service(HttpServletRequest req, HttpServletResponse res) {
  CountServlet.localCount.getAndAdd(datastoreCount());
  final Key key = KeyFactory.createKey("Counter", getKeyName());
  LifecycleManager.getInstance()
      .setShutdownHook(
          new ShutdownHook() {
            @Override
            public void shutdown() {
              datastoreSave(key);
            }
          });
}
 
Example #10
Source File: VmStopFilter.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Handle stop requests.
 *
 * Stop requests are intercepted by this filter and never forwarded to user code.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  // Requests to /_ah/stop are filtered by the appserver (and soon VM-local nginx). Any stop
  // requests that make it here are from trusted code.
  logger.info("Running shutdown hook");
  long deadline = System.currentTimeMillis() + SHUTDOWN_HOOK_DEADLINE_MILLIS;
  LifecycleManager.getInstance().beginShutdown(deadline);

  response.setContentType("text/plain");
  response.getWriter().write("ok");
}
 
Example #11
Source File: InitializationServlet.java    From wt1 with Apache License 2.0 5 votes vote down vote up
public void registerShutdownHook() {
    LifecycleManager.getInstance().setShutdownHook(new ShutdownHook() {
        public void shutdown() {
            logger.info("App will stop");
            ProcessingQueue.getInstance().setShutdown();
        }
    });
}