FileDocCategorySizeDatePackage
AsynchEventHandler.javaAPI DocJBoss 4.2.14788Fri Jul 13 20:52:38 BST 2007org.jboss.ha.framework.server

AsynchEventHandler

public class AsynchEventHandler extends Object implements Runnable
Utility class that accepts objects into a queue and maintains a separate thread that reads them off the queue and passes them to a registered "processor".
todo
find a better home for this than the cluster module
author
Brian Stansberry
version
$Revision$

Fields Summary
private String
name
private EDU.oswego.cs.dl.util.concurrent.LinkedQueue
events
The LinkedQueue of events to pass to our processor
private boolean
blocking
Whether we're blocking on the queue
private AsynchEventProcessor
processor
private boolean
stopped
private Thread
handlerThread
private Logger
log
Constructors Summary
public AsynchEventHandler(AsynchEventProcessor processor, String name)
Create a new AsynchEventHandler.

param
processor object to which objects placed in the queue should be handed when dequeued
param
name name for this instance. Appended to the processor's class name to create a log category, and used to name to handler thread

   
                                                                                                                 
       
   
      super();
      this.processor = processor;
      if (name == null)
         name = "AsynchEventHandler";
      this.name = name;
      this.log = Logger.getLogger(processor.getClass().getName() + "." + name);
   
Methods Summary
public booleanisStopped()

      return stopped;
   
public voidqueueEvent(java.lang.Object event)
Place the given object in the queue.

param
event the object to asynchronously pass to the AsynchEventHandler.
throws
InterruptedException if the thread is interrupted while blocking on the queue.

      if (event != null)
         events.put(event);
   
public voidrun()

      log.debug("Begin " + name + " Thread");
      stopped = false;
      while( !stopped )
      {
         try
         {
            blocking = true;
            Object event = events.take();
            blocking = false;
            
            if (!stopped) 
            {
               processor.processEvent(event);
            }
         }
         catch(InterruptedException e)
         {
            blocking = false;
            log.debug(name + " Thread interrupted", e);               
            if (stopped)
               break;
         }
         catch (Throwable t)
         {
            log.error("Caught Throwable handling asynch events", t);
         }
      }
      log.debug("End " + name + " Thread");
   
public voidstart()
Starts the handler thread.

      handlerThread = new Thread(this, name + " Thread");
      handlerThread.start();
   
public voidstop()
Stops the handler thread.

      stopped = true;
      if (blocking)
         handlerThread.interrupt(); // it's just waiting on the LinkedQueue
      
      if (handlerThread.isAlive()) {
         // Give it up to 100ms to finish whatever it's doing
         try
         {
            handlerThread.join(100);
         }
         catch (Exception ignored) {}
      }
      
      if (handlerThread.isAlive())
         handlerThread.interrupt(); // kill it