org.apache.commons.messagelet
Class ConsumerThread

java.lang.Object
  extended by java.lang.Thread
      extended by org.apache.commons.messagelet.ConsumerThread
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
XAConsumerThread

public class ConsumerThread
extends java.lang.Thread

ConsumerThread is a thread which will repeatedly consume JMS messages using a receive() method on Messenger and then process the message. This class is a good base class when implementing some kind of transactional processing of JMS messages

Version:
$Revision: 1.4 $
Author:
James Strachan

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  javax.jms.MessageConsumer consumer
           
private  javax.jms.Destination destination
           
private  javax.jms.MessageListener listener
           
private static org.apache.commons.logging.Log log
          Logger
private  Messenger messenger
           
private  java.lang.String selector
           
private  boolean shouldStop
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ConsumerThread()
           
 
Method Summary
protected  void cancelTransaction()
          Strategy method to represent the code required to cancel a transaction.
protected  void commitTransaction()
          Strategy method to represent the code required to commit a transaction.
protected  javax.jms.MessageConsumer createConsumer()
          Factory method to create a new MessageConsumer
protected  javax.jms.MessageConsumer getConsumer()
           
 javax.jms.Destination getDestination()
          Returns the destination.
 javax.jms.MessageListener getListener()
          Returns the listener.
 Messenger getMessenger()
          Returns the messenger.
 java.lang.String getSelector()
          Returns the selector.
 boolean isShouldStop()
          Returns the shouldStop.
protected  void processMessage(javax.jms.Message message)
          Strategy method to process a given message.
private  javax.jms.Message receive()
          Strategy method to consume a message using a receive() kind of method.
protected  void rollbackTransaction(java.lang.Exception e)
          Strategy method to represent the code required to rollback a transaction.
 void run()
          Starts all the JMS connections and consumes JMS messages, passing them onto the MessageListener and Message Driven Objects
 void setDestination(javax.jms.Destination destination)
          Sets the destination.
 void setListener(javax.jms.MessageListener listener)
          Sets the listener.
 void setMessenger(Messenger messenger)
          Sets the messenger.
 void setSelector(java.lang.String selector)
          Sets the selector.
 void setShouldStop(boolean shouldStop)
          Sets the shouldStop.
protected  void startConsumer()
          Starts consuming messages
protected  void startTransaction()
          Strategy method to represent the code required to start a transaction.
protected  void stopConsumer()
          Stops consuming messages
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log
Logger


consumer

private javax.jms.MessageConsumer consumer

messenger

private Messenger messenger

destination

private javax.jms.Destination destination

selector

private java.lang.String selector

listener

private javax.jms.MessageListener listener

shouldStop

private boolean shouldStop
Constructor Detail

ConsumerThread

public ConsumerThread()
Method Detail

run

public void run()
Starts all the JMS connections and consumes JMS messages, passing them onto the MessageListener and Message Driven Objects

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

getDestination

public javax.jms.Destination getDestination()
Returns the destination.

Returns:
Destination

getListener

public javax.jms.MessageListener getListener()
Returns the listener.

Returns:
MessageListener

getMessenger

public Messenger getMessenger()
Returns the messenger.

Returns:
Messenger

getSelector

public java.lang.String getSelector()
Returns the selector.

Returns:
String

isShouldStop

public boolean isShouldStop()
Returns the shouldStop.

Returns:
boolean

setDestination

public void setDestination(javax.jms.Destination destination)
Sets the destination.

Parameters:
destination - The destination to set

setListener

public void setListener(javax.jms.MessageListener listener)
Sets the listener.

Parameters:
listener - The listener to set

setMessenger

public void setMessenger(Messenger messenger)
Sets the messenger.

Parameters:
messenger - The messenger to set

setSelector

public void setSelector(java.lang.String selector)
Sets the selector.

Parameters:
selector - The selector to set

setShouldStop

public void setShouldStop(boolean shouldStop)
Sets the shouldStop.

Parameters:
shouldStop - The shouldStop to set

startConsumer

protected void startConsumer()
                      throws javax.jms.JMSException
Starts consuming messages

Throws:
javax.jms.JMSException

stopConsumer

protected void stopConsumer()
                     throws javax.jms.JMSException
Stops consuming messages

Throws:
javax.jms.JMSException

createConsumer

protected javax.jms.MessageConsumer createConsumer()
                                            throws javax.jms.JMSException
Factory method to create a new MessageConsumer

Throws:
javax.jms.JMSException

receive

private javax.jms.Message receive()
                           throws javax.jms.JMSException
Strategy method to consume a message using a receive() kind of method.

Returns:
the message or null if a message could not be found after waiting for some period of time.
Throws:
javax.jms.JMSException

processMessage

protected void processMessage(javax.jms.Message message)
                       throws javax.jms.JMSException
Strategy method to process a given message. By default this will just invoke the MessageListener

Throws:
javax.jms.JMSException

startTransaction

protected void startTransaction()
                         throws java.lang.Exception
Strategy method to represent the code required to start a transaction.

Throws:
java.lang.Exception

commitTransaction

protected void commitTransaction()
                          throws java.lang.Exception
Strategy method to represent the code required to commit a transaction.

Throws:
java.lang.Exception

rollbackTransaction

protected void rollbackTransaction(java.lang.Exception e)
Strategy method to represent the code required to rollback a transaction.


cancelTransaction

protected void cancelTransaction()
                          throws java.lang.Exception
Strategy method to represent the code required to cancel a transaction. This is called when a message is not received.

Throws:
java.lang.Exception

getConsumer

protected javax.jms.MessageConsumer getConsumer()