1 package org.tanukisoftware.wrapper.test; 2 3 27 28 import java.io.File ; 29 import java.io.FileWriter ; 30 import java.io.IOException ; 31 import java.io.PrintWriter ; 32 import java.util.Date ; 33 34 import org.tanukisoftware.wrapper.WrapperListener; 35 import org.tanukisoftware.wrapper.WrapperManager; 36 37 43 public class LoadedWrapperListener 44 implements WrapperListener, Runnable 45 { 46 private String [] m_startMainArgs; 47 private boolean m_mainComplete; 48 private Integer m_mainExitCode; 49 private boolean m_waitTimedOut; 50 51 54 private LoadedWrapperListener() 55 { 56 } 57 58 61 73 public Integer start( String [] args ) 74 { 75 if ( WrapperManager.isDebugEnabled() ) 76 { 77 System.out.println( "LoadedWrapperListener: start(args)" ); 78 } 79 80 Thread mainThread = new Thread ( this, "LoadedWrapperListenerMain" ); 81 synchronized ( this ) 82 { 83 m_startMainArgs = args; 84 mainThread.start(); 85 try 87 { 88 this.wait( 5000 ); 89 } 90 catch ( InterruptedException 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 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 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 } 147 else 148 { 149 if ( WrapperManager.isDebugEnabled() ) 150 { 151 System.out.println( "LoadedWrapperListener: controlEvent(" + event + ") Stopping" ); 152 } 153 154 WrapperManager.stop( 0 ); 157 } 159 } 160 161 164 167 public void run() 168 { 169 Throwable 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 m_mainComplete = true; 187 188 this.notifyAll(); 189 } 190 191 return; 192 } 193 catch (Throwable e) 194 { 195 t = e; 196 } 197 198 System.out.println( "Encountered an error running start main: " + t ); 201 t.printStackTrace(); 202 203 synchronized( this ) 204 { 205 if ( m_waitTimedOut ) 206 { 207 WrapperManager.stop( 1 ); 209 return; } 211 else 212 { 213 m_mainComplete = true; 215 m_mainExitCode = new Integer ( 1 ); 216 this.notifyAll(); 217 return; 218 } 219 } 220 } 221 222 225 228 private void appMain( String [] args ) 229 { 230 System.out.println( "App Main Starting." ); 231 System.out.println(); 232 233 for ( int i = 0; i < 500; i++ ) 237 { 238 System.out.println( new Date () + " Pre " + i + " of output. " 239 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ); 240 } 241 242 Thread diskThrasher = new Thread ( "LoadedWrapperListener_DiskThrasher" ) 244 { 245 public void run() 246 { 247 performDiskThrashing(); 248 } 249 }; 250 diskThrasher.start(); 251 252 Thread memoryThrasher = new Thread ( "LoadedWrapperListener_MemoryThrasher" ) 254 { 255 public void run() 256 { 257 performMemoryThrashing(); 258 } 259 }; 260 memoryThrasher.start(); 261 262 for ( int i = 0; i < 4; i++ ) 264 { 265 Thread cpuThrasher = new Thread ( "LoadedWrapperListener_CPUThrasher_" + i ) 266 { 267 public void run() 268 { 269 performCPUThrashing(); 270 } 271 }; 272 cpuThrasher.start(); 273 } 274 275 for ( int i = 0; i < 5000; i++ ) 279 { 280 System.out.println( new Date () + " 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 file = new File ( "loadedwrapperlistener.dat" ); 292 try 293 { 294 PrintWriter w = new PrintWriter ( new FileWriter ( file ) ); 295 try 296 { 297 for ( int i = 0; i < 100; i++ ) 298 { 299 w.println( new Date () + " Row " + i + " of output. " 300 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ); 301 } 302 } 303 finally 304 { 305 w.close(); 306 } 307 } 308 catch ( IOException 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 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 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 } 343 } 344 345 348 public static void main(String [] args) { 349 System.out.println(); 352 353 System.out.println( "LoadedWrapperListener.main" ); 354 355 WrapperManager.start( new LoadedWrapperListener(), args ); 356 } 357 } 358 359 | Popular Tags |