KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > plugins > clustersupport > ListenForClusterTests


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.plugins.clustersupport;
6
7 import com.opensymphony.oscache.base.Cache;
8 import com.opensymphony.oscache.base.Config;
9 import com.opensymphony.oscache.base.FinalizationException;
10 import com.opensymphony.oscache.base.InitializationException;
11 import com.opensymphony.oscache.base.events.CacheEntryEventListener;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * <p>This should be used in conjunction with the cluster test cases. Run this
18  * program to set up listeners for the various clustering implementations so
19  * you can see that the test messages are being received correctly.</p>
20  *
21  * <p>A shutdown hook is installed so the listeners can be shut down cleanly.</p>
22  *
23  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
24  */

25 public final class ListenForClusterTests {
26     ArrayList JavaDoc listeners = new ArrayList JavaDoc();
27     Cache cache;
28
29     private void mainLoop() {
30         Thread JavaDoc shutdownHook = new ShutdownHookThread("");
31         Runtime.getRuntime().addShutdownHook(shutdownHook);
32         System.out.println();
33         System.out.println("------------------------------------------------");
34         System.out.println("Waiting for cluster messages... (CTRL-C to exit)");
35         System.out.println("------------------------------------------------");
36
37         while (true) {
38             try {
39                 Thread.sleep(250);
40             } catch (InterruptedException JavaDoc ie) {
41             }
42         }
43     }
44
45     private void initListeners() {
46         BaseTestBroadcastingListener testcase = null;
47         AbstractBroadcastingListener listener;
48         Cache cache = new Cache(true, false, false);
49
50         // Add the JavaGroups listener
51
try {
52             testcase = new TestJavaGroupsBroadcastingListener("JavaGroups");
53             listener = testcase.getListener();
54             listener.initialize(cache, testcase.getConfig());
55             cache.addCacheEventListener(listener, CacheEntryEventListener.class);
56             listeners.add(listener);
57         } catch (InitializationException e) {
58             System.out.println("The JavaGroups listener could not be initialized: " + e);
59         }
60
61         // Add the JMS listener
62
try {
63             testcase = new TestJMSBroadcastingListener("JMS");
64             listener = testcase.getListener();
65
66             Config config = testcase.getConfig();
67             config.set("cache.cluster.jms.node.name", "cacheNode2");
68
69             listener.initialize(cache, config);
70             cache.addCacheEventListener(listener, CacheEntryEventListener.class);
71             listeners.add(listener);
72         } catch (InitializationException e) {
73             System.out.println("The JMS listener could not be initialized: " + e);
74         }
75     }
76
77     /**
78      * Starts up the cluster listeners.
79      */

80     public static void main(String JavaDoc[] args) {
81         ListenForClusterTests listen = new ListenForClusterTests();
82
83         listen.initListeners();
84
85         listen.mainLoop();
86     }
87
88     /**
89      * Inner class that handles the shutdown event
90      */

91     class ShutdownHookThread extends Thread JavaDoc {
92         protected String JavaDoc message;
93
94         public ShutdownHookThread(String JavaDoc message) {
95             this.message = message;
96         }
97
98         /**
99          * This is executed when the application is forcibly shutdown (via
100          * CTRL-C etc). Any configured listeners are shut down here.
101          */

102         public void run() {
103             System.out.println("Shutting down the cluster listeners...");
104
105             for (Iterator JavaDoc it = listeners.iterator(); it.hasNext();) {
106                 try {
107                     ((AbstractBroadcastingListener) it.next()).finialize();
108                 } catch (FinalizationException e) {
109                     System.out.println("The listener could not be shut down cleanly: " + e);
110                 }
111             }
112
113             System.out.println("Shutdown complete.");
114         }
115     }
116 }
117
Popular Tags