1 22 package org.jboss.ha.framework.server; 23 24 import org.jboss.logging.Logger; 25 26 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 27 28 38 class AsynchEventHandler implements Runnable 39 { 40 44 public static interface AsynchEventProcessor 45 { 46 public void processEvent(Object event); 47 } 48 49 private String name; 50 51 private LinkedQueue events = new LinkedQueue(); 52 53 private boolean blocking; 54 private AsynchEventProcessor processor; 55 private boolean stopped = true; 56 private Thread handlerThread; 57 private Logger log; 58 59 68 public AsynchEventHandler(AsynchEventProcessor processor, String name) 69 { 70 super(); 71 this.processor = processor; 72 if (name == null) 73 name = "AsynchEventHandler"; 74 this.name = name; 75 this.log = Logger.getLogger(processor.getClass().getName() + "." + name); 76 } 77 78 87 public void queueEvent(Object event) throws InterruptedException 88 { 89 if (event != null) 90 events.put(event); 91 } 92 93 public void run() 94 { 95 log.debug("Begin " + name + " Thread"); 96 stopped = false; 97 while( !stopped ) 98 { 99 try 100 { 101 blocking = true; 102 Object event = events.take(); 103 blocking = false; 104 105 if (!stopped) 106 { 107 processor.processEvent(event); 108 } 109 } 110 catch(InterruptedException e) 111 { 112 blocking = false; 113 if (stopped) 114 { 115 log.debug(name + " Thread interrupted"); 116 break; 117 } 118 log.error(name + " Thread interrupted", e); 119 } 120 catch (Throwable t) 121 { 122 log.error("Caught Throwable handling asynch events", t); 123 } 124 } 125 log.debug("End " + name + " Thread"); 126 } 127 128 131 public void start() 132 { 133 handlerThread = new Thread (this, name + " Thread"); 134 handlerThread.start(); 135 } 136 137 140 public void stop() 141 { 142 stopped = true; 143 if (blocking) 144 handlerThread.interrupt(); 146 if (handlerThread.isAlive()) { 147 try 149 { 150 handlerThread.join(100); 151 } 152 catch (Exception ignored) {} 153 } 154 155 if (handlerThread.isAlive()) 156 handlerThread.interrupt(); } 158 159 public boolean isStopped() 160 { 161 return stopped; 162 } 163 164 } 165 | Popular Tags |