Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE Submit a form and leave background process working?

I need to generate a report with java (exporting data from oracle) that can take several minutes, by the amount of data involved, so when I submit my form the screen freezes until it ends.

How can I generate the report in a background process, so that the user can continue browsing?

JMS? How can i do this with JMS? (Any example or explanation is welcome)

Is there another way? What would be the best option according to the Java EE specification?

like image 832
ErVeY Avatar asked Feb 11 '26 22:02

ErVeY


1 Answers

Yes, JMS might be an option. You simply send a message to a queue, and a Message Driven Bean -- a special kind of EJB -- will then receive and process the message. The message acts in this case like a command. You can use JMS without Message Driven Bean though, but it's a bit more complicated.

If you are in a EJB 3.1 environment, you could give a try to asynchronous EJB.

Otherwise, while it's not recommended by the spec, you could start a thread in the web container. Either you start one thread per job, or you could start/stop one background thread in a ServletContextListener, which processes commands that you store, say, in a table in a database.

Using asynchronous jobs is superficially very easy, but for production you will need to think about how to manage error, monitor progress, retry failed job, ensure that no two same job are run at the same time, etc. Each approach has its own strength and weakness. Pay also attention to the strategy you need for the transactions (JMS is transacted, I don't know exactly for async EJB 3.1., and custom thread and database table can be transacted if do things correctly with JDBC transaction or UserTransaction).

Hope it helps.

like image 65
ewernli Avatar answered Feb 14 '26 11:02

ewernli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!