0

I am trying to figure what could be the flow since I am using JMS for the first time. Locally, I have ApacheMQ installed on my Windows Machine. And using this simple Spring JMS example mentioned here, I was able to see how to send and receive message thing works in Spring JMS and my ApacheMQ looks like the following after running the producer and consumer:

screenshot of ApacheMQ

Here is my sender class :

public class Sender {

  private static final Logger LOGGER =
      LoggerFactory.getLogger(Sender.class);

  @Autowired
  private JmsTemplate jmsTemplate;

  public void send(String message) {
    LOGGER.info("sending message='{}'", message);
    jmsTemplate.convertAndSend("Testing Spring JMS", message);
  }
}

Here is my Receiver class :

public class Receiver {

  private static final Logger LOGGER =
      LoggerFactory.getLogger(Receiver.class);

  private CountDownLatch latch = new CountDownLatch(1);

  public CountDownLatch getLatch() {
    return latch;
  }

  @JmsListener(destination = "Testing Spring JMS")
  public void receive(String message) {
    LOGGER.info("received message='{}'", message);
    latch.countDown();
  }
}

So, now in my User Interface, I have a "Download" button somewhere and when a user clicks on it, it is supposed to call a stored procedure which is going to take time.

  1. I am trying to understand, how would I call this JMS application when a user clicks on the download button so that I could send the stored procedure call to the Sender. I mean there is no endpoint defined in the code above just like if we were to use a REST, the request would be hitting Controller.

  2. And how would sender be sending it to the destination queue, in the form of a string message just like shown in the example above?

  3. I guess if I would have a clear idea about sender processing this, I might get some idea about how the Receiver is going to handle this.

1

Typically, for truly asynchronous events, you need a mechanism to check on it's progress. In this scenario you need a processing token that you can use to request updates. The way this would work in practice is something like this:

  UI      |  Service         |  Async Processor
----------+--------------=---+-------------------
Request  -->Push 2 Q        --> Pop Q
         <--Return Token(T ) |
          |                  |
Update(T)-->Query Processor --> Find Process(T)
         <--Return Status(T)<-- Return Status(T)
          |                  |
Result(T)-->Get Result      --> Find Process(T)
         <--Return Result(T)<-- Return Result(T)

The middle query can be done multiple times until the process is complete.

Your statuses can be something like the following:

  • Pending
  • Processing
  • Complete

That's the general concept at least. You can refine from there.

  • Thanks. 1) Could you elaborate more on processing token? 2) Is this how client would communicate with the JMS (as mentioned here : activemq.apache.org/ajax.html) – John Feb 11 at 14:24
  • The processing token can be as simple as a unique identifier to look up that particular request. JMS is just the Q (i.e. only involved until the job is popped off the queue). However, from the UI perspective, it will need something unique to identify its request. The Server can either query state in the database using the token (id), or query the processor itself. – Berin Loritsch Feb 11 at 14:26
  • Ok. Thanks. As far as client communicating with the JMS is concerned, do I still need to implement a REST end point on my back end so that client could send parameters via Ajax call and then I would put my request to JMS queue? Not sure because I read JMS and REST are two different things and based on what I am thinking I am combining these two. Please clarify. Thanks – John Feb 11 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.