• Post Reply Bookmark Topic Watch Topic
  • New Topic

Web application and cancel a operation that take long time. How do it with EJB 3.1

 
Yeray Santana Borges
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In some project, sometimes I have found a similar requirement, cancel the execution of a process that take long time. For example, the user clicks in a button, this action trigger a process that calculate data an while the process is running, the user see a cancel button with a please wait message.

If the user clicks on cancel button, the transaction that was opened in the first request is marked as rollback and the execution of the process is stopped. With early technologies as EJB 3.0 or without an ejb container, sometime is difficult implement this logic to get the open transaction in a previous request from a new request to cancel it.

How EJB 3.1 can help me in similar situations??
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Yeray!

Did you consider using the new Asynchronous processing?

Please correct me if this solution is not appropriate for you, but I think that you could:

1. create an asynchronous business method (annotated with @Asynchronous) which will return Future<V> object,
2. client invokes the business method which runs in its transaction context,
3. because it's an asynchronous call, the client can still control the UI and can hit the "cancel" button which invokes Future<V>#cancel() operation,
4. the business method upon receiving cancel() operation, rolls the transaction back.

Cheers!
 
Yeray Santana Borges
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmhhh, sure, that's exactly what I need. Now all I need is to have an EJB 3.1 container in the upcoming applications, but that's another thing.

Thanks for your response. Regards
 
Per Lindberg
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is perhaps the answer to my recent prayers. I have a web app that sometimes must do a heavy database conversion. Now, it turns out that it's not possible to just do a POST request from the browser and wait an hour (or ten) for the response, since many browsers re-send the POST request after 15 minutes! (FF, IE, but not Chrome). Gaah! That would start another, parallel thread, with catastrophic result.
So perhaps the best practice in this case is to have the servlet use an @Asynchronous EJB 3.1 method and respond immediately? Of course, the completion can't easily be shown, but logging progress and completion in the application log file is sufficient for me - unless there's a better way?
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that there is also a possibility of using an Asynchronous Servlets (http://download.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html#asyncSupported%28%29). These came with Servlets 3.0 and can also be solution to your problem.

Cheers!
 
Per Lindberg
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oho! Thanks!!

Or perhaps even better: just call request.startAsynch() on the request and respond with the usual response? (I use the PRG pattern, btw).

(It may be that I must use JDBC instead of my usual JPA/EJB stuff for this conversion, since there are zillions of records).

 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I guess you have two approaches. If you already use EJB you can use the EJB asychronousity but if you use just servlets, then asyncSupport should be enough.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!