10

Our project is to integrate two applications, using rest api of each, using JMS(to provide asynchronous nature) and spring batch to read bulk data from the JMS queue and process it and then post it to the receiving application.

I am a newbie to both JMS and spring batch. I have few basic questions to ask:

  • Which JMS model to ahead with-(PTP or Pub/Sub)
  • Can messages be read in bulk from the JMS queue(using JMSItemReader). If yes, can anyone pls provide with a code.
  • We want to acknowledge messages as 'read' once it is successfully posted (ie. read-process-write) to receiving application and not when it is read by the JMSItemReader. How can we achieve this?

The high level design diagram is below enter image description here

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Janhavee
  • 119
  • 1
  • 3
  • 1
    I can answer about the last point, with writers we do have listeners, see if you can manage to send back the response in afterWrite event. For further assistance I would recommend reading Spring-batch in action, you will also find number of examples in it, You will also find examples related to JMS – Bilbo Baggins Oct 07 '15 at 13:09
  • What are you expecting to happen when things go wrong for this? I ask because I want to confirm that the use of Spring Batch vs Spring Integration is the best option here... – Michael Minella Oct 07 '15 at 14:45
  • @Bilbo thanks will go through spring batch in action. – Janhavee Oct 08 '15 at 08:01
  • @MichaelMinella We are using spring batch for faster processing. We want to read data in bulk instead of reading data one by one from queue. – Janhavee Oct 08 '15 at 08:04
  • 1
    Two things to note: 1. Spring Batch provides the `BatchMessageListenerContainer` that allows for the batching of messages in a single transaction. 2. Check out Pro Spring Batch of which I'm the author of for more details if you're looking for a batch book ;) – Michael Minella Oct 08 '15 at 14:19
  • @MichaelMinella : I am not able to find BatchMessageListenerContainer in spring-batch. Can you please guide me with this? I am not able to find this package org.springframework.batch.container.jms.BatchMessageListenerContainer. I am using org.springframework.batch spring-batch-core 3.0.7.RELEASE – Dipika Dec 25 '16 at 17:45
  • @MichaelMinella, I need to consume multiple messages as group . Can you please check my post If you can help? https://stackoverflow.com/questions/55107244/hibernate-batch-to-insert-bulk-entities-from-queue-topic – Alagammal P Mar 29 '19 at 14:17

1 Answers1

0

PTP vs Pub/sub

The point to point method using a message queue is the most standard method to go. Especially in a batch application I can not see immediate reason to use Publish subscribe which presumes you have multiple consumers of the same messages.

Theoretically If multiple functions need to be executed over the same chunks of data you can organize the different processors as subscribers this way scaling the application, but this is pretty advanced usage scenario.

Can messages be read in bulk from JMS queue:

The JMS specification here only talks (vaguely might be misreading it) about bulk acknowledgment of messages, but it does not set a requirement over bulk delivery of messages.

CLIENT_ACKNOWLEDGE - With this option, a client acknowledges a message by calling the message’s acknowledge method. Acknowledging a consumed message automatically acknowledges the receipt of all messages that have been delivered by its session.

Simply put the answer with respect of the bulk delivery is "If the JMS provider supports it, then yes, otherwise no"

Most providers allow bulk acknowledgment of messages

Here is the Oracles' interface doing that:

public interface com.sun.messaging.jms.Message {
          void acknowledgeThisMessage() throws JMSException;
          void acknowledgeUpThroughThisMessage() throws JMSException;
}

A combination of CLIENT_ACKNOWLEDGE . + invoking the method acknowledgeUpThroughThisMessage on a . message will acknowledge all messages received up to that moment in time.

Manual acknowledgment of messages:

This can be achieved through CLIENT_ACKNOWLEDGE and the acknowledge method on the Message. Here I will quote you again the javadoc of the acknowledge method which is also referencing one more time your second question and it talks about bulk acknowledgment of all messages up to a point.

void acknowledge() throws JMSException Acknowledges all consumed messages of the session of this consumed message. All consumed JMS messages support the acknowledge method for use when a client has specified that its JMS session's consumed messages are to be explicitly acknowledged. By invoking acknowledge on a consumed message, a client acknowledges all messages consumed by the session that the message was delivered to.

Calls to acknowledge are ignored for both transacted sessions and sessions specified to use implicit acknowledgement modes.

A client may individually acknowledge each message as it is consumed, or it may choose to acknowledge messages as an application-defined group (which is done by calling acknowledge on the last received message of the group, thereby acknowledging all messages consumed by the session.)

Messages that have been received but not acknowledged may be redelivered.

Alexander Petrov
  • 9,204
  • 31
  • 70