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());
23 private volatile boolean shuttingDown
= false;
24 private volatile long deadline
= -1L;
26 private LifecycleManager() { }
28 public static LifecycleManager
getInstance() {
32 public boolean isShuttingDown() {
37 * Register a ShutdownHook to be called when the runtime shuts down.
39 public synchronized void setShutdownHook(ShutdownHook hook
) {
40 hooks
.put(currentAppVersionId(), hook
);
44 * Calls Thread.interrupt() on all threads running requests for this
47 public void interruptAllRequests() {
48 AccessController
.doPrivileged(
49 new PrivilegedAction() {
51 List
<Thread
> threads
= ApiProxy
.getRequestThreads();
52 if (threads
!= null) {
53 for (Thread thread
: threads
) {
63 * If the runtime is shutting down, returns how long, in
64 * milliseconds, is left for shutdown code to clean up. Otherwise,
67 public long getRemainingShutdownTime() {
68 long value
= deadline
;
72 return value
- System
.currentTimeMillis();
77 * For testing purposes only:
78 * Notifies the LifecycleManager that the runtime is shutting down.
80 public synchronized void beginShutdown(long deadline
) {
81 this.shuttingDown
= true;
82 this.deadline
= deadline
;
83 ShutdownHook hook
= hooks
.get(currentAppVersionId());
89 private String
currentAppVersionId() {
90 ApiProxy
.Environment env
= ApiProxy
.getCurrentEnvironment();
91 return env
.getAppId() + "/" + env
.getVersionId();
94 private Map
<String
, ShutdownHook
> hooks
= new HashMap
<String
, ShutdownHook
>();
96 public interface ShutdownHook
{
97 public void shutdown();