KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > restart > ServerCrasher


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test.restart;
5
6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
7
8 import com.tc.exception.TCRuntimeException;
9 import com.tc.objectserver.control.ServerControl;
10 import com.tc.util.concurrent.ThreadUtil;
11
12 public class ServerCrasher implements Runnable JavaDoc {
13   private final ServerControl server;
14   private final Thread JavaDoc myThread = new Thread JavaDoc(this, "ServerCrasher");
15   private final long crashInterval;
16   private final boolean crash;
17   private final SynchronizedBoolean isRunning = new SynchronizedBoolean(false);
18
19   public ServerCrasher(final ServerControl server, final long crashInterval, final boolean crash) {
20     super();
21     this.server = server;
22     this.crashInterval = crashInterval;
23     this.crash = crash;
24   }
25
26   public void startAutocrash() throws Exception JavaDoc {
27     isRunning.set(true);
28     myThread.start();
29   }
30
31   public void run() {
32     try {
33       while (isRunning.get()) {
34         System.err.println("Starting server...");
35         synchronized (isRunning) {
36           if (isRunning.get()) server.start(30 * 1000);
37         }
38         ThreadUtil.reallySleep(crashInterval);
39         synchronized (isRunning) {
40           if (isRunning.get()) {
41             if (crash) {
42               System.err.println("Crashing server...");
43               server.crash();
44             }
45             else {
46               System.err.println("Shutting server down...");
47               server.shutdown();
48             }
49             if (server.isRunning()) throw new AssertionError JavaDoc("Server is still running even after shutdown or crash.");
50           }
51         }
52       }
53     } catch (Exception JavaDoc e) {
54       throw new TCRuntimeException(e);
55     }
56   }
57
58   public void stop() throws Exception JavaDoc {
59     synchronized (isRunning) {
60       isRunning.set(false);
61       server.crash();
62     }
63   }
64 }
65
Popular Tags