• Post Reply Bookmark Topic Watch Topic
  • New Topic

Long running task processing in Java EE

 
Milindu Sanoj Kumarage
Greenhorn
Posts: 9
Android Google App Engine jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to develop a Java EE application for the following scenario.

Webapp takes a file from a user and analyze the file. This analysis could take hours. User should be able to check if the analysis is finished via AJAX. When the analysis is finished user should be able to view the analysis report that has been generated by the analyzer.

I checked what are the possibles ways I could achieve this but couldn't get a clear idea. I heard about JMS, Work Manager API and servlet asynchronous processing. But still not sure what to use and how to use.I'm not very much familiar with EJB. Please, enlighten me.
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have an asynchronous message processing engine that receives JMS updates from your analyzer. The analyzer should utilize a callback function to generate the JMS status messages. The Request-Reply pattern should utilize CorrelId to keep the sessions straight.
 
Milindu Sanoj Kumarage
Greenhorn
Posts: 9
Android Google App Engine jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply.

Roger Sterling wrote:You should have an asynchronous message processing engine that receives JMS updates from your analyzer.


How can I implement an asynchronous message processing engine that receives JMS updates from my analyzer? I'm not quite familiar with JMS. I just read some posts about it.
And what about Work Manager API? I found it much simpler to understand. Can't I use it? ( But I still have no clear idea how to implement it )
And also servlet asynchronous processing. http://java.dzone.com/articles/design-flexible-and-scalable It says it uses a Thread Pool, is it okay?
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do anything you want, you are the developer. Like as in any possible problem, there can be twelve different solutions. If you do not want to use JMS, you don't have to. You can use any of the solutions you mentioned.
 
Milindu Sanoj Kumarage
Greenhorn
Posts: 9
Android Google App Engine jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Sterling wrote:You can do anything you want, you are the developer. Like as in any possible problem, there can be twelve different solutions. If you do not want to use JMS, you don't have to. You can use any of the solutions you mentioned.




What is the best way, what will be the easiest way me to learn and implement? What are the pros and cons of these methods?
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this look easy or hard to you ? http://www.eaipatterns.com/RequestReplyJmsExample.html
 
Milindu Sanoj Kumarage
Greenhorn
Posts: 9
Android Google App Engine jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Sterling wrote:Does this look easy or hard to you ? http://www.eaipatterns.com/RequestReplyJmsExample.html


When the user uploads the file, the '/analizer' servlet will invoke the send() method of Requester instance with the file's path as a parameter, then the Replier's onMessage() method will be called and this methods should have the codes for this long running analyzing task, right? That's how understood it, is it correct? And how can I check if the process if finished with AJAX?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how can I check if the process if finished with AJAX?

AJAX has nothing to do with this. Maybe the page on which you can check the status uses AJAX to update itself, but that is a different question best asked in the HTML/JavaScript forum, and has nothing to do with how the processing happens on the server.

I don't see servlet asynch processing as a good fit for this. After some hours the web page where the task was started is likely to have been closed. JMS wouldn't strike me as the first choice here, but that depends on the details of your situation (about which we know very little). What I would recommend is a separate page where the user can go to see the status of all the jobs he has started.
 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define a hashmap where you have in-memory status of the analyzer.

(I am guessing there can be more than one users.)

1. In you analyzer component, you can set the status as "running" for "Analyzer1" and so on, when the user takes action on it.

2. Then whenever user wants to query the status you can get it from the map.

3. Update the status as complete when the report is generated (i.e. the analyzer component /method call ends)

 
Milindu Sanoj Kumarage
Greenhorn
Posts: 9
Android Google App Engine jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:AJAX has nothing to do with this. Maybe the page on which you can check the status uses AJAX to update itself, but that is a different question best asked in the HTML/JavaScript forum, and has nothing to do with how the processing happens on the server.


No, I know how to make the AJAX request from client side, but I'm asking how the sevelt that going to respond to this AJAX request know if the analyzer is still doing this job or has finished it? Can a servlet ask the Replier if it's done that task given to it? or I have to use a workaround like updating a database record once the analyzing is done and where the AJAX request responding servlet checks the database records to know the process's status?


I don't see servlet async processing as a good fit for this. After some hours the web page where the task was started is likely to have been closed. JMS wouldn't strike me as the first choice here, but that depends on the details of your situation (about which we know very little). What I would recommend is a separate page where the user can go to see the status of all the jobs he has started.


What I need is, when the user uploads a file, the web app should analyze this file. This analyzing task might task hours. We know we can't hold the initial request of the user for 3,4 hours without a response timeout, right? and I want a way where this servlet that gets the file hands this long running process to some one else and sends a messages saying the file has been queued to be processed. And this someone else who gets the task run on its own doing its long running processes. I'm asking how to implement this someone. What should I use? I know I can't start a separate thread and assign this task to it.
 
Milindu Sanoj Kumarage
Greenhorn
Posts: 9
Android Google App Engine jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit chavan wrote:You can define a hashmap where you have in-memory status of the analyzer.

(I am guessing there can be more than one users.)

1. In you analyzer component, you can set the status as "running" for "Analyzer1" and so on, when the user takes action on it.

2. Then whenever user wants to query the status you can get it from the map.

3. Update the status as complete when the report is generated (i.e. the analyzer component /method call ends)



Now things are getting clearer to me, thanks for your help. I'm going to implement it this way, hope you will help me in future also Thanks a lot!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Milindu Sanoj Kumarage wrote:Can a servlet ask the Replier if it's done that task given to it? or I have to use a workaround like updating a database record once the analyzing is done and where the AJAX request responding servlet checks the database records to know the process's status?

Yes, the servlet somehow needs to know the status of those tasks, but you should not muddy the waters by even mentioning AJAX, because the details of how the servlet is accessed are irrelevant to this question. How to get the status depends a whole lot on how the task processing is done.

I'm asking how to implement this someone. What should I use? I know I can't start a separate thread and assign this task to it.

You can't? Yes, some people will tell you that you should not start new threads in a servlet container or JEE container, but the fact is, it's not an uncommon thing to do, and it generally works fine. There are some interesting classes in the java.util.concurrent package that might help, like ExecutorService.

If there are a lot of these tasks, or they're running in parallel, then you may want to come up with a scheme of how to send the tasks to some other server, maybe via some kind of queue, as was mentioned before.
 
Milindu Sanoj Kumarage
Greenhorn
Posts: 9
Android Google App Engine jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
You can't? Yes, some people will tell you that you should not start new threads in a servlet container or JEE container, but the fact is, it's not an uncommon thing to do, and it generally works fine. There are some interesting classes in the java.util.concurrent package that might help, like ExecutorService.

If there are a lot of these tasks, or they're running in parallel, then you may want to come up with a scheme of how to send the tasks to some other server, maybe via some kind of queue, as was mentioned before.


It is nice if I can start a separate thread in JEE, hmm, ExecutorService, it sounds promising, I'll look in to it Thank you!
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't really use ExecutorService in EJB containers, as it bypasses the container's thread pools. JEE7 adds ManagedExecutorService and ManagedScheduledExecutorService that can be looked up through JNDI or injected.

If you're stuck with JEE6, there are other ways. Using TimerService can be used to create delayed or recurring tasks, optionally in combination with ScheduleExpression. To run something in the background you can use JMS or @Asynchronous. For the latter, simply annotate a bean with that, get a reference to the bean*, and call the @Asynchronous method.


* If you want to call a method of the bean itself as an @Asynchronous method, you can use SessionContext's getBusinessObject method to get a bean reference from the bean itself.
 
Milindu Sanoj Kumarage
Greenhorn
Posts: 9
Android Google App Engine jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:If you're stuck with JEE6, there are other ways. Using TimerService can be used to create delayed or recurring tasks, optionally in combination with ScheduleExpression. To run something in the background you can use JMS or @Asynchronous. For the latter, simply annotate a bean with that, get a reference to the bean*, and call the @Asynchronous method.


* If you want to call a method of the bean itself as an @Asynchronous method, you can use SessionContext's getBusinessObject method to get a bean reference from the bean itself.


I too like to use either JMS or @Asynchronous as you mentioned. I want this to be implemented with all the best practices because at the end this will be an open source project. I'm thinking what to use from JMS or @Asynchronous. Both looks promising, studying what are the pros and cons of them. And thank you very much for the kind advice
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!