1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
;
5 import com
.google
.apphosting
.api
.ApiProxy
;
7 import java
.util
.HashMap
;
10 import java
.util
.logging
.Logger
;
11 import java
.security
.AccessController
;
12 import java
.security
.PrivilegedAction
;
15 * Information about the current status of the Java Runtime.
18 public final class LifecycleManager
{
20 private static final LifecycleManager instance
= new LifecycleManager();
21 private static final Logger log
= Logger
.getLogger(LifecycleManager
.class.getName());
22 private boolean shuttingDown
= false;
23 private long deadline
= -1L;
25 private LifecycleManager() { }
27 public static LifecycleManager
getInstance() {
31 public boolean isShuttingDown() {
36 * Register a ShutdownHook to be called when the runtime shuts down.
38 public synchronized void setShutdownHook(ShutdownHook hook
) {
39 hooks
.put(currentAppVersionId(), hook
);
43 * Calls Thread.interrupt() on all threads running requests for this
46 public void interruptAllRequests() {
47 AccessController
.doPrivileged(
48 new PrivilegedAction() {
50 List
<Thread
> threads
= ApiProxy
.getRequestThreads();
51 if (threads
!= null) {
52 for (Thread thread
: threads
) {
62 * If the runtime is shutting down, returns how long, in
63 * milliseconds, is left for shutdown code to clean up. Otherwise,
66 public long getRemainingShutdownTime() {
67 if (deadline
== -1L) {
70 return deadline
- System
.currentTimeMillis();
75 * For testing purposes only:
76 * Notifies the LifecycleManager that the runtime is shutting down.
78 public synchronized void beginShutdown(long deadline
) {
79 this.shuttingDown
= true;
80 this.deadline
= deadline
;
81 ShutdownHook hook
= hooks
.get(currentAppVersionId());
87 private String
currentAppVersionId() {
88 ApiProxy
.Environment env
= ApiProxy
.getCurrentEnvironment();
89 return env
.getAppId() + "/" + env
.getVersionId();
92 private Map
<String
, ShutdownHook
> hooks
= new HashMap
<String
, ShutdownHook
>();
94 public interface ShutdownHook
{
95 public void shutdown();