I work on a task that requires:
- consuming data from JMS;
- processing it;
- loading it into a database.
As the documentation suggests:
- I start with
<int-jms:message-driven-channel-adapter channel="CHANNEL1" ... />
to send newJMS
messages to theCHANNEL1
channel; - I apply the transformer that converts messages from the
CHANNEL1
channel toJobLaunchRequest
with a job that inserts data to the database and the payload that contains originalJMS
message's payload; - The transformed messages go to the
CHANNEL2
channel; <batch-int:job-launching-gateway request-channel="CHANNEL2"/>
starts a new job execution when a new message appears in the channel;
The problem is that I start a new database transaction each time a new jms messages received.
The question: how should I handle such a flow? What is the common pattern for this?
UPDATE
I start the job for each message. One message contains one piece of data. If I resort in just using spring-batch
then I will have to manage some sort of a poller (correct me if I am wrong), but I would like to apply a message driven approach like (either one):
- Grace period: when a new message appears I wait for
10
more messages or start processing everything I received10
seconds after the first message is received. - I simply read everything the
JMS
queue contains after I got notified that the queue contains a new message.
Of course, I would like the solution to be transnational: the order of message processing does not matter.
I start the job for each message. One message contains one piece of data.
: This means you will have one job for each item, which is not batch processing anymore and does not make sense to me to use Spring Batch. Since a message is an item, I recommend to use a single job that reads messages from the queue and process/write them to the database. For transactions, you need to use a Jta transaction manager to coordinate transactions between the DB and the queue (in case of rollback, the message goes back to the queue). – Mahmoud Ben HassineJmsItemReader
is no different, it reads data from the queue until a timeout expires. – Mahmoud Ben Hassine