wrapper 

Commit MetaInfo

Revision173 (tree)
Time2012-02-21 16:30:25
Authormortenson

Log Message

Rewrite the Memory test so it now shutsdown cleanly and also is much cleaner code.

Change Summary

Diff

--- trunk/wrapper/src/java/org/tanukisoftware/wrapper/test/Memory.java (revision 172)
+++ trunk/wrapper/src/java/org/tanukisoftware/wrapper/test/Memory.java (revision 173)
@@ -29,11 +29,10 @@
2929 * included in all copies or substantial portions of the Software.
3030 */
3131
32+import java.io.File;
3233 import java.io.FileWriter;
3334 import java.io.IOException;
3435 import java.io.Writer;
35-import java.lang.reflect.InvocationTargetException;
36-import java.lang.reflect.Method;
3736
3837 /**
3938 *
@@ -42,67 +41,75 @@
4241 */
4342 public class Memory implements Runnable
4443 {
45- private Writer m_writer;
44+ private static Memory c_theInstance;
45+
4646 private Thread m_runner;
4747
4848 /*---------------------------------------------------------------
49+ * Constructor
50+ *-------------------------------------------------------------*/
51+ private Memory()
52+ {
53+ // Start the runner
54+ m_runner = new Thread( this, "runner" );
55+ m_runner.start();
56+ }
57+
58+ /*---------------------------------------------------------------
4959 * Runnable Method
5060 *-------------------------------------------------------------*/
5161 public void run()
5262 {
53- if ( m_runner == null )
54- {
55- // This is the runner
56- m_runner = Thread.currentThread();
57- }
58- else
59- {
60- System.out.println(Main.getRes().getString( "Stopping..." ) );
61- // This is the shutdown hook. Sloppy code, but simple :-)
62- m_runner = null;
63- return;
64- }
65-
6663 long startTime = System.currentTimeMillis();
6764 long lastTest = startTime;
6865 try
6966 {
70- m_writer.write( Main.getRes().getString( "--> Starting Memory Log\n" ) );
71- m_writer.flush();
72-
73- while( m_runner != null )
67+ File file = new File( "../logs/memory.log" );
68+ System.out.println( Main.getRes().getString( "Writing memory Log to: {0}", file ) );
69+
70+ Writer writer = new FileWriter( file );
71+ try
7472 {
75- long now = System.currentTimeMillis();
76- System.out.println( Main.getRes().getString( "Running for {0}ms...", new Long( now - startTime ) ) );
73+ writer.write( Main.getRes().getString( "--> Starting Memory Log\n" ) );
74+ writer.flush();
7775
78- if ( now - lastTest > 15000 )
76+ while( m_runner != null )
7977 {
80- Runtime rt = Runtime.getRuntime();
81- System.gc();
82- long totalMemory = rt.totalMemory();
83- long freeMemory = rt.freeMemory();
84- long usedMemory = totalMemory - freeMemory;
78+ long now = System.currentTimeMillis();
79+ System.out.println( Main.getRes().getString( "Running for {0}ms...", new Long( now - startTime ) ) );
8580
86- m_writer.write( Main.getRes().getString( "total memory=" ) + pad( totalMemory, 10 )
87- + Main.getRes().getString( ", used=" ) + pad( usedMemory, 10 )
88- + Main.getRes().getString( ", free=" ) + pad( freeMemory, 10 ) + "\n" );
89- m_writer.flush();
81+ if ( now - lastTest > 15000 )
82+ {
83+ Runtime rt = Runtime.getRuntime();
84+ System.gc();
85+ long totalMemory = rt.totalMemory();
86+ long freeMemory = rt.freeMemory();
87+ long usedMemory = totalMemory - freeMemory;
88+
89+ writer.write( Main.getRes().getString( "total memory=" ) + pad( totalMemory, 10 )
90+ + Main.getRes().getString( ", used=" ) + pad( usedMemory, 10 )
91+ + Main.getRes().getString( ", free=" ) + pad( freeMemory, 10 ) + "\n" );
92+ writer.flush();
93+
94+ lastTest = now;
95+ }
9096
91- lastTest = now;
97+ try
98+ {
99+ Thread.sleep( 250 );
100+ }
101+ catch ( InterruptedException e )
102+ {
103+ }
92104 }
93105
94- try
95- {
96- Thread.sleep( 250 );
97- }
98- catch ( InterruptedException e )
99- {
100- }
106+ writer.write( Main.getRes().getString( "<-- Stopping Memory Log\n" ) );
107+ writer.flush();
101108 }
102-
103- m_writer.write( Main.getRes().getString( "<-- Stopping Memory Log\n" ) );
104- m_writer.flush();
105- m_writer.close();
109+ finally
110+ {
111+ writer.close();
112+ }
106113 }
107114 catch ( IOException e )
108115 {
@@ -124,6 +131,7 @@
124131 }
125132 return s;
126133 }
134+
127135 /*---------------------------------------------------------------
128136 * Main Method
129137 *-------------------------------------------------------------*/
@@ -131,44 +139,31 @@
131139 {
132140 System.out.println( Main.getRes().getString( "Memory Tester Running...") );
133141
134- // Locate the add and remove shutdown hook methods using reflection so
135- // that this class can be compiled on 1.2.x versions of java.
136- Method addShutdownHookMethod;
137- try {
138- addShutdownHookMethod =
139- Runtime.class.getMethod("addShutdownHook", new Class[] {Thread.class});
140- } catch (NoSuchMethodException e) {
141- System.out.println( Main.getRes().getString( "Shutdown hooks not supported by current JVM.") );
142- addShutdownHookMethod = null;
143- }
142+ c_theInstance = new Memory();
144143
145- Memory app = new Memory();
146-
147- // Create a Writer for the memory output
148- try
149- {
150- app.m_writer = new FileWriter( "memory.log" );
151- }
152- catch ( IOException e )
153- {
154- e.printStackTrace();
155- return;
156- }
157-
158- // Register a shutdown hook using reflection.
159- if (addShutdownHookMethod != null) {
160- Runtime runtime = Runtime.getRuntime();
161- Thread hook = new Thread( app, "shutdown-hook" );
162- try {
163- addShutdownHookMethod.invoke(runtime, new Object[] {hook});
164- } catch (IllegalAccessException e) {
165- System.out.println( Main.getRes().getString( "Unable to register shutdown hook: {0}", e.getMessage() ) );
166- } catch (InvocationTargetException e) {
167- System.out.println( Main.getRes().getString( "Unable to register shutdown hook: {0}", e.getMessage() ) );
168- }
169- }
170-
171- // Start the runner
172- new Thread( app, "runner" ).start();
144+ // Register a shutdown hook.
145+ Runtime.getRuntime().addShutdownHook( new Thread( "shutdown-hook" )
146+ {
147+ public void run()
148+ {
149+ System.out.println(Main.getRes().getString( "Stopping..." ) );
150+
151+ Thread runner = c_theInstance.m_runner;
152+
153+ // Tell the main thread to stop.
154+ c_theInstance.m_runner = null;
155+
156+ // Wait for the thread to actually stop cleanly.
157+ try
158+ {
159+ runner.join();
160+ }
161+ catch ( InterruptedException e )
162+ {
163+ }
164+
165+ System.out.println(Main.getRes().getString( "Stopped." ) );
166+ }
167+ } );
173168 }
174169 }
\ No newline at end of file
旧リポジトリブラウザで表示