开发者

Spring batch 2.1 and JmsReader

I have one spring batch application which can be run from command line. The flow of the batch job is when it starts , it reads the message from queue and transform then and write to db. Now for this i am using JmsItemReader from spring batch and inside read() method I have my logic. Now question is there might be a chance that when this process start, there is no message exist on the queue, in this scenario I want to stop processing whole batch.

How can this be achieved in spring batch? Basically what happens when you use JmsItemReader, it continuously ping jms broker and to read message and then sleep for a period of time. Now if there is no message to process the开发者_开发技巧n why should we keep the batch process running and let it consume CPU. I want it to run again next day as per schedule.

Thanks


this behaviour comes from the default configuration for the jmstemplate which is provided by spring-jms

default value for timeOut is indefinite

private long receiveTimeout = RECEIVE_TIMEOUT_INDEFINITE_WAIT;

with this value, messageconsumer.receive() will be used

Receives the next message produced for this message consumer. This call blocks indefinitely until a message is produced or until this message consumer is closed.

so you need to set a timeout value for the jmstemplate

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
     (...)
     <property name="receiveTimeout" value="someLongValueForMilliseconds" />
     (...)
</bean>

then receive(long timeOut) will be used

This call blocks until a message arrives, the timeout expires, or this message consumer is closed. A timeout of zero never expires, and the call blocks indefinitely.

if you use a value of -1 the call happens immediately

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜