1 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 ; 14 import java.util.Iterator ; 15 16 25 public final class ListenForClusterTests { 26 ArrayList listeners = new ArrayList (); 27 Cache cache; 28 29 private void mainLoop() { 30 Thread 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 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 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 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 80 public static void main(String [] args) { 81 ListenForClusterTests listen = new ListenForClusterTests(); 82 83 listen.initListeners(); 84 85 listen.mainLoop(); 86 } 87 88 91 class ShutdownHookThread extends Thread { 92 protected String message; 93 94 public ShutdownHookThread(String message) { 95 this.message = message; 96 } 97 98 102 public void run() { 103 System.out.println("Shutting down the cluster listeners..."); 104 105 for (Iterator 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 |