org.springframework.batch.item.jms
Class JmsItemReader

java.lang.Object
  extended by org.springframework.batch.item.support.AbstractItemReader
      extended by org.springframework.batch.item.jms.JmsItemReader
All Implemented Interfaces:
ItemKeyGenerator, ItemReader, ItemRecoverer, NewItemIdentifier

public class JmsItemReader
extends AbstractItemReader
implements ItemRecoverer, ItemKeyGenerator, NewItemIdentifier

An ItemReader for JMS using a JmsTemplate. The template should have a default destination, which will be used to provide items in read(). If a recovery step is needed, set the error destination and the item will be sent there if processing fails in a stateful retry. The implementation is thread safe after its properties are set (normal singleton behaviour).

Author:
Dave Syer

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
JmsItemReader()
           
 
Method Summary
 Object getKey(Object item)
          If the message is a Message then returns the JMS message ID.
 boolean isNew(Object item)
          If the item is a message, check the JMS re-delivered flag, otherwise return false to be on the safe side.
 Object read()
          Reads a piece of input data and advance to the next one.
 Object recover(Object item, Throwable cause)
          Send the message back to the provider using the specified error destination property of this reader.
 void setErrorDestination(Destination errorDestination)
          Set the error destination.
 void setErrorDestinationName(String errorDestinationName)
          Set the error destination by name.
 void setItemType(Class itemType)
          Set the expected type of incoming message payloads.
 void setJmsTemplate(JmsOperations jmsTemplate)
          Setter for jms template.
 
Methods inherited from class org.springframework.batch.item.support.AbstractItemReader
mark, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected org.apache.commons.logging.Log logger
Constructor Detail

JmsItemReader

public JmsItemReader()
Method Detail

setErrorDestination

public void setErrorDestination(Destination errorDestination)
Set the error destination. Should not be the same as the default destination of the jms template.

Parameters:
errorDestination - a JMS Destination

setErrorDestinationName

public void setErrorDestinationName(String errorDestinationName)
Set the error destination by name. Will be resolved by the destination resolver in the jms template.

Parameters:
errorDestinationName - the name of a JMS Destination

setJmsTemplate

public void setJmsTemplate(JmsOperations jmsTemplate)
Setter for jms template.

Parameters:
jmsTemplate - a JmsOperations instance

setItemType

public void setItemType(Class itemType)
Set the expected type of incoming message payloads. Set this to Message to receive the raw underlying message.

Parameters:
itemType - the java class of the items to be delivered.
Throws:
IllegalStateException - if the message payload is of the wrong type.

read

public Object read()
Description copied from interface: ItemReader
Reads a piece of input data and advance to the next one. Implementations must return null at the end of the input data set. In a transactional setting, caller might get the same item twice from successive calls (or otherwise), if the first call was in a transaction that rolled back.

Specified by:
read in interface ItemReader

recover

public Object recover(Object item,
                      Throwable cause)
Send the message back to the provider using the specified error destination property of this reader. If the recovery is successful the item itself is returned, otherwise null.

Specified by:
recover in interface ItemRecoverer
Parameters:
item - the item that failed.
cause - the cause of the failure that led to this recovery.
Returns:
true if recovery was successful.
See Also:
ItemRecoverer.recover(Object, Throwable)

getKey

public Object getKey(Object item)
If the message is a Message then returns the JMS message ID. Otherwise just delegate to parent class.

Specified by:
getKey in interface ItemKeyGenerator
Parameters:
item - the current item.
Returns:
a unique identifier.
Throws:
UnexpectedInputException - if the JMS id cannot be determined from a JMS Message
See Also:
ItemKeyGenerator.getKey(java.lang.Object)

isNew

public boolean isNew(Object item)
If the item is a message, check the JMS re-delivered flag, otherwise return false to be on the safe side.

Specified by:
isNew in interface NewItemIdentifier
Parameters:
item - the current item.
Returns:
true if the item is known to have never been processed before.
See Also:
NewItemIdentifier.isNew(java.lang.Object)


Copyright © 2009 SpringSource. All Rights Reserved.