001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.plugins.clustersupport;
006:
007: import com.opensymphony.oscache.base.Cache;
008: import com.opensymphony.oscache.base.Config;
009: import com.opensymphony.oscache.base.FinalizationException;
010: import com.opensymphony.oscache.base.InitializationException;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: /**
016: * <p>This should be used in conjunction with the cluster test cases. Run this
017: * program to set up listeners for the various clustering implementations so
018: * you can see that the test messages are being received correctly.</p>
019: *
020: * <p>A shutdown hook is installed so the listeners can be shut down cleanly.</p>
021: *
022: * @author <a href="mailto:chris@swebtec.com">Chris Miller</a>
023: */
024: public final class ListenForClusterTests {
025: ArrayList listeners = new ArrayList();
026: Cache cache;
027:
028: private void mainLoop() {
029: Thread shutdownHook = new ShutdownHookThread("");
030: Runtime.getRuntime().addShutdownHook(shutdownHook);
031: System.out.println();
032: System.out
033: .println("------------------------------------------------");
034: System.out
035: .println("Waiting for cluster messages... (CTRL-C to exit)");
036: System.out
037: .println("------------------------------------------------");
038:
039: while (true) {
040: try {
041: Thread.sleep(250);
042: } catch (InterruptedException ie) {
043: }
044: }
045: }
046:
047: private void initListeners() {
048: BaseTestBroadcastingListener testcase = null;
049: AbstractBroadcastingListener listener;
050: Cache cache = new Cache(true, false, false);
051:
052: // Add the JavaGroups listener
053: try {
054: testcase = new TestJavaGroupsBroadcastingListener(
055: "JavaGroups");
056: listener = testcase.getListener();
057: listener.initialize(cache, testcase.getConfig());
058: cache.addCacheEventListener(listener);
059: listeners.add(listener);
060: } catch (InitializationException e) {
061: System.out
062: .println("The JavaGroups listener could not be initialized: "
063: + e);
064: }
065:
066: // Add the JMS listener
067: try {
068: testcase = new TestJMSBroadcastingListener("JMS");
069: listener = testcase.getListener();
070:
071: Config config = testcase.getConfig();
072: config.set("cache.cluster.jms.node.name", "cacheNode2");
073:
074: listener.initialize(cache, config);
075: cache.addCacheEventListener(listener);
076: listeners.add(listener);
077: } catch (InitializationException e) {
078: System.out
079: .println("The JMS listener could not be initialized: "
080: + e);
081: }
082: }
083:
084: /**
085: * Starts up the cluster listeners.
086: */
087: public static void main(String[] args) {
088: ListenForClusterTests listen = new ListenForClusterTests();
089:
090: listen.initListeners();
091:
092: listen.mainLoop();
093: }
094:
095: /**
096: * Inner class that handles the shutdown event
097: */
098: class ShutdownHookThread extends Thread {
099: protected String message;
100:
101: public ShutdownHookThread(String message) {
102: this .message = message;
103: }
104:
105: /**
106: * This is executed when the application is forcibly shutdown (via
107: * CTRL-C etc). Any configured listeners are shut down here.
108: */
109: public void run() {
110: System.out
111: .println("Shutting down the cluster listeners...");
112:
113: for (Iterator it = listeners.iterator(); it.hasNext();) {
114: try {
115: ((AbstractBroadcastingListener) it.next())
116: .finialize();
117: } catch (FinalizationException e) {
118: System.out
119: .println("The listener could not be shut down cleanly: "
120: + e);
121: }
122: }
123:
124: System.out.println("Shutdown complete.");
125: }
126: }
127: }
|