KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanukisoftware > wrapper > test > LoadedWrapperListener


1 package org.tanukisoftware.wrapper.test;
2
3 /*
4  * Copyright (c) 1999, 2006 Tanuki Software Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of the Java Service Wrapper and associated
8  * documentation files (the "Software"), to deal in the Software
9  * without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sub-license,
11  * and/or sell copies of the Software, and to permit persons to
12  * whom the Software is furnished to do so, subject to the
13  * following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */

27
28 import java.io.File JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.util.Date JavaDoc;
33
34 import org.tanukisoftware.wrapper.WrapperListener;
35 import org.tanukisoftware.wrapper.WrapperManager;
36
37 /**
38  * This test was created to test timeout problems under heavily loaded
39  * conditions.
40  *
41  * @author Leif Mortenson <leif@tanukisoftware.com>
42  */

43 public class LoadedWrapperListener
44     implements WrapperListener, Runnable JavaDoc
45 {
46     private String JavaDoc[] m_startMainArgs;
47     private boolean m_mainComplete;
48     private Integer JavaDoc m_mainExitCode;
49     private boolean m_waitTimedOut;
50     
51     /*---------------------------------------------------------------
52      * Constructor
53      *-------------------------------------------------------------*/

54     private LoadedWrapperListener()
55     {
56     }
57     
58     /*---------------------------------------------------------------
59      * WrapperListener Methods
60      *-------------------------------------------------------------*/

61     /**
62      * The start method is called when the WrapperManager is signaled by the
63      * native wrapper code that it can start its application. This
64      * method call is expected to return, so a new thread should be launched
65      * if necessary.
66      *
67      * @param args List of arguments used to initialize the application.
68      *
69      * @return Any error code if the application should exit on completion
70      * of the start method. If there were no problems then this
71      * method should return null.
72      */

73     public Integer JavaDoc start( String JavaDoc[] args )
74     {
75         if ( WrapperManager.isDebugEnabled() )
76         {
77             System.out.println( "LoadedWrapperListener: start(args)" );
78         }
79
80         Thread JavaDoc mainThread = new Thread JavaDoc( this, "LoadedWrapperListenerMain" );
81         synchronized ( this )
82         {
83             m_startMainArgs = args;
84             mainThread.start();
85             // Wait for five seconds to give the application a chance to have failed.
86
try
87             {
88                 this.wait( 5000 );
89             }
90             catch ( InterruptedException JavaDoc e ) { }
91             m_waitTimedOut = true;
92
93             if ( WrapperManager.isDebugEnabled() )
94             {
95                 System.out.println( "LoadedWrapperListener: start(args) end. Main Completed=" +
96                     m_mainComplete + ", exitCode=" + m_mainExitCode );
97             }
98             return m_mainExitCode;
99         }
100     }
101     
102     /**
103      * Called when the application is shutting down. The Wrapper assumes that
104      * this method will return fairly quickly. If the shutdown code code
105      * could potentially take a long time, then WrapperManager.signalStopping()
106      * should be called to extend the timeout period. If for some reason,
107      * the stop method can not return, then it must call
108      * WrapperManager.stopped() to avoid warning messages from the Wrapper.
109      *
110      * @param exitCode The suggested exit code that will be returned to the OS
111      * when the JVM exits.
112      *
113      * @return The exit code to actually return to the OS. In most cases, this
114      * should just be the value of exitCode, however the user code has
115      * the option of changing the exit code if there are any problems
116      * during shutdown.
117      */

118     public int stop( int exitCode )
119     {
120         if ( WrapperManager.isDebugEnabled() )
121         {
122             System.out.println( "LoadedWrapperListener: stop(" + exitCode + ")" );
123         }
124         
125         return exitCode;
126     }
127     
128     /**
129      * Called whenever the native wrapper code traps a system control signal
130      * against the Java process. It is up to the callback to take any actions
131      * necessary. Possible values are: WrapperManager.WRAPPER_CTRL_C_EVENT,
132      * WRAPPER_CTRL_CLOSE_EVENT, WRAPPER_CTRL_LOGOFF_EVENT, or
133      * WRAPPER_CTRL_SHUTDOWN_EVENT
134      *
135      * @param event The system control signal.
136      */

137     public void controlEvent( int event )
138     {
139         if ( WrapperManager.isControlledByNativeWrapper() )
140         {
141             if ( WrapperManager.isDebugEnabled() )
142             {
143                 System.out.println( "LoadedWrapperListener: controlEvent(" + event + ") Ignored" );
144             }
145             // Ignore the event as the native wrapper will handle it.
146
}
147         else
148         {
149             if ( WrapperManager.isDebugEnabled() )
150             {
151                 System.out.println( "LoadedWrapperListener: controlEvent(" + event + ") Stopping" );
152             }
153
154             // Not being run under a wrapper, so this isn't an NT service and should always exit.
155
// Handle the event here.
156
WrapperManager.stop( 0 );
157             // Will not get here.
158
}
159     }
160     
161     /*---------------------------------------------------------------
162      * Runnable Methods
163      *-------------------------------------------------------------*/

164     /**
165      * Runner thread which actually launches the application.
166      */

167     public void run()
168     {
169         Throwable JavaDoc t = null;
170         try
171         {
172             if ( WrapperManager.isDebugEnabled() )
173             {
174                 System.out.println( "LoadedWrapperListener: invoking start main method" );
175             }
176             appMain( m_startMainArgs );
177             if ( WrapperManager.isDebugEnabled() )
178             {
179                 System.out.println( "LoadedWrapperListener: start main method completed" );
180             }
181
182             synchronized ( this )
183             {
184                 // Let the start() method know that the main method returned, in case it is
185
// still waiting.
186
m_mainComplete = true;
187                 
188                 this.notifyAll();
189             }
190
191             return;
192         }
193         catch (Throwable JavaDoc e)
194         {
195             t = e;
196         }
197
198         // If we get here, then an error was thrown. If this happened quickly
199
// enough, the start method should be allowed to shut things down.
200
System.out.println( "Encountered an error running start main: " + t );
201         t.printStackTrace();
202
203         synchronized( this )
204         {
205             if ( m_waitTimedOut )
206             {
207                 // Shut down here.
208
WrapperManager.stop( 1 );
209                 return; // Will not get here.
210
}
211             else
212             {
213                 // Let start method handle shutdown.
214
m_mainComplete = true;
215                 m_mainExitCode = new Integer JavaDoc( 1 );
216                 this.notifyAll();
217                 return;
218             }
219         }
220     }
221     
222     /*---------------------------------------------------------------
223      * Methods
224      *-------------------------------------------------------------*/

225     /**
226      * Main method of the actual application.
227      */

228     private void appMain( String JavaDoc[] args )
229     {
230         System.out.println( "App Main Starting." );
231         System.out.println();
232         
233         // Loop and display 500 long lines of text to place to dump a lot of
234
// output before the CPU starts being loaded down. This will strain
235
// the Wrapper just as the CPU suddenly hpegs at 100%.
236
for ( int i = 0; i < 500; i++ )
237         {
238             System.out.println( new Date JavaDoc() + " Pre " + i + " of output. "
239                 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" );
240         }
241         
242         // Start up a thread to thrash the hard disk.
243
Thread JavaDoc diskThrasher = new Thread JavaDoc( "LoadedWrapperListener_DiskThrasher" )
244         {
245             public void run()
246             {
247                 performDiskThrashing();
248             }
249         };
250         diskThrasher.start();
251         
252         // Start up a thread to thrash memory.
253
Thread JavaDoc memoryThrasher = new Thread JavaDoc( "LoadedWrapperListener_MemoryThrasher" )
254         {
255             public void run()
256             {
257                 performMemoryThrashing();
258             }
259         };
260         memoryThrasher.start();
261         
262         // Start up some threads to eat all available CPU
263
for ( int i = 0; i < 4; i++ )
264         {
265             Thread JavaDoc cpuThrasher = new Thread JavaDoc( "LoadedWrapperListener_CPUThrasher_" + i )
266             {
267                 public void run()
268                 {
269                     performCPUThrashing();
270                 }
271             };
272             cpuThrasher.start();
273         }
274         
275         // Loop and display 5000 long lines of text to place a heavy load on the
276
// JVM output processing code of the Wrapper while the above threads are
277
// eating all available CPU.
278
for ( int i = 0; i < 5000; i++ )
279         {
280             System.out.println( new Date JavaDoc() + " Row " + i + " of output. "
281                 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" );
282         }
283         System.out.println();
284         System.out.println( "App Main Complete." );
285     }
286     
287     private void performDiskThrashing()
288     {
289         while( !m_mainComplete )
290         {
291             File JavaDoc file = new File JavaDoc( "loadedwrapperlistener.dat" );
292             try
293             {
294                 PrintWriter JavaDoc w = new PrintWriter JavaDoc( new FileWriter JavaDoc( file ) );
295                 try
296                 {
297                     for ( int i = 0; i < 100; i++ )
298                     {
299                         w.println( new Date JavaDoc() + " Row " + i + " of output. "
300                             + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" );
301                     }
302                 }
303                 finally
304                 {
305                     w.close();
306                 }
307             }
308             catch ( IOException JavaDoc e )
309             {
310                 e.printStackTrace();
311             }
312             file.delete();
313         }
314     }
315     
316     private void performMemoryThrashing()
317     {
318         int count = 0;
319         while( !m_mainComplete )
320         {
321             // 200MB block of memory
322
byte[][] garbage = new byte[200][];
323             for ( int i = 0; i < garbage.length; i++ )
324             {
325                 garbage[i] = new byte[1024 * 1024];
326             }
327             garbage = null;
328             
329             Runtime JavaDoc runtime = Runtime.getRuntime();
330             long totalMemory = runtime.totalMemory();
331             long freeMemory = runtime.freeMemory();
332             System.out.println( "Total Memory=" + totalMemory + ", "
333                 + "Used Memory=" + ( totalMemory - freeMemory ) );
334         }
335     }
336     
337     private void performCPUThrashing()
338     {
339         while( !m_mainComplete )
340         {
341             // Do nothing, we just want a tight loop.
342
}
343     }
344     
345     /*---------------------------------------------------------------
346      * Main Method
347      *-------------------------------------------------------------*/

348     public static void main(String JavaDoc[] args) {
349         // Test an initial line feed as a regression test. Must be
350
// the first output from the JVM
351
System.out.println();
352
353         System.out.println( "LoadedWrapperListener.main" );
354         
355         WrapperManager.start( new LoadedWrapperListener(), args );
356     }
357 }
358
359
Popular Tags