0

My Requirement is to deal with the long running method in a java class , which is called when the user submits a button.

I need to do some comparing logic which would take long time to process and i need a suggestion whether i should go with message queue or i shall go for Timer/Scheduler that would call the code in every "n" minutes/hours and do the processing in different thread.

What would be the best Approach and in what situations i would use either ?

Thanks

Manish Sahni
  • 488
  • 2
  • 7
  • 21
  • Your question is too broad: Define "best". What's your client (browser, stand-alone app)? So the answer is "it depends". – Beryllium Sep 14 '13 at 17:43
  • @Beryllium thanks for the response..i have a web application running on an application server , i have a web form in which the user would click a button that would execute the long running process. – Manish Sahni Sep 14 '13 at 17:56

1 Answers1

1
  • If your frequency is not that high (1min), polling from a browser in a database is OK, especially if you already have a database in your application, a job scheduler like Quartz, and you are using "only" a servlet container.
  • If response times are important, consider using asynchronous servlets. Again on the server side, one thread can poll a database, and handle multiple clients efficiently.

As for JMS

  • As an alternative replace the database and the scheduler with JMS queues and message driven beans (MDB). The main difference is the programming model: Instead of polling, your receivers listen: They get notified. This way the framework can choose the best method to implement message delivery. In addition to a standard servlet container you now need a JMS provider, and possibly an EJB container.
  • You can combine this with asynchronous servlets as well.

If you do not use a database in the first approach: JMS supports persistent messages out-of-the-box, and the queues decouple client and worker by buffering the messages. This simplifies system maintenance: You can update the application with the workers without interrupting the clients.

Update

Message sender and receiver are decoupled by the message (it's asynchronous). So processing in the receiver is not stopped by any action in the user interface.

For example

  1. The servlet sends a message. It returns immediately a HTML response to the user/browser: "Task has been submitted". It's asynchronous, because it does not wait for the result.
  2. The receiver gets the message, and starts processing, for example an MDB in its onMessage method
  3. The HTML page is refreshed: The servlet now checks, if there is a message in the response queue. There is none so far, so it returns "not yet completed".
  4. The receiver has finished its calculations, and puts the result as a message on the result queue.
  5. The HTML page is refreshed: Now there is a message, and it will present the results to the user.

If you implement the checking as an AJAX call, the user does not see that you are actually polling.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
  • since i am new to JMS , please tell me if the listener gets notified and then starts doing the processing , will the processing stop if i click on some other link from my web application and the controls goes to the other controller...or listener will work in a separate thread....and continue processing , Assuming the listener class is in the same WAR. – Manish Sahni Sep 14 '13 at 18:55
  • 1
    @ManishSahni I have added an example to the answer. Think of messaging like a postal service: You have a letter, put it in a letter box, and the next days you are waiting for the postman. Three days later, he gives you the response (another letter). So once you have put the letter in the letter box, you cannot interrupt it, you have to wait. Of course the receiver is free to discard your letter, and is free not to respond at all. – Beryllium Sep 14 '13 at 19:56
  • Wow, that's a crisp and great explanation..It really helped in understanding the concept. – Manish Sahni Sep 15 '13 at 06:59