1 package org.tanukisoftware.wrapper.test; 2 3 45 46 import java.lang.reflect.InvocationTargetException ; 47 import java.lang.reflect.Method ; 48 49 54 public class ShutdownHook { 55 58 public static void main(String [] args) { 59 Method addShutdownHookMethod; 62 try { 63 addShutdownHookMethod = 64 Runtime .class.getMethod("addShutdownHook", new Class [] {Thread .class}); 65 } catch (NoSuchMethodException e) { 66 System.out.println("Shutdown hooks not supported by current JVM."); 67 return; 68 } 69 70 System.out.println("This application registers a shutdown hook which"); 71 System.out.println("should be executed after the JVM has told the Wrapper"); 72 System.out.println("it is exiting."); 73 System.out.println("This is to test the wrapper.jvm_exit.timeout property"); 74 75 Runtime runtime = Runtime.getRuntime(); 76 Thread hook = new Thread () { 77 public void run() { 78 System.out.println("Starting shutdown hook. Loop for 20 seconds."); 79 System.out.println("Should timeout unless this property is set: wrapper.jvm_exit.timeout=25"); 80 81 long start = System.currentTimeMillis(); 82 while(System.currentTimeMillis() - start < 20000) 83 { 84 try 85 { 86 Thread.sleep( 250 ); 87 } 88 catch ( InterruptedException e ) 89 { 90 } 92 } 93 System.out.println("Shutdown look complete. Should exit now."); 94 } 95 }; 96 try { 97 addShutdownHookMethod.invoke(runtime, new Object [] {hook}); 98 } catch (IllegalAccessException e) { 99 System.out.println("Unable to register shutdown hook: " + e.getMessage()); 100 } catch (InvocationTargetException e) { 101 System.out.println("Unable to register shutdown hook: " + e.getMessage()); 102 } 103 104 System.out.println("Application complete. Wrapper should stop, invoking the shutdown hooks."); 105 System.out.println(); 106 } 107 } 108 109 | Popular Tags |