Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a background process in Java EE

I want to start a background process in a Java EE (OC4J 10) environment. It seems wrong to just start a Thread with "new Thread" But I can't find a good way for this.

Using a JMS queue is difficult in my special case, since my parameters for this method call are not serializable.

I also thought about using an onTimeout Timer Method on a session bean but this does not allow me to pass parameters (as far as I know).

Is there any "canon" way to handle such a task, or do I just have to revert to "new Thread" or a java.concurrent.ThreadPool.

like image 294
ptriller Avatar asked Jun 25 '09 20:06

ptriller


4 Answers

Java EE usually attempts to removing threading from the developers concerns. (It's success at this is a completely different topic).

JMS is clearly the preferred approach to handle this.

With most parameters, you have the option of forcing or faking serialization, even if they aren't serializable by default. Depending on the data, consider wrapping it in a serializable object that can reload the data. This will clearly depend on the parameter and application.

like image 61
lief79 Avatar answered Nov 03 '22 21:11

lief79


JMS is the Java EE way of doing this. You can start your own threads if the container lets you, but that does violate the Java EE spec (you may or may not care about this).

If you don't care about Java EE generic compliance (if you would in fact resort to threads rather than deal with JMS), the Oracle container will for sure have proprietary ways of doing this (such as the OracleAS Job Scheduler).

like image 31
Yishai Avatar answered Nov 03 '22 20:11

Yishai


Don't know OCJ4 in detail but I used the Thread approach and a java.util.Timer approach to perform some task in a Tomcat based application. In Java 5+ there is an option to use one of the Executor services (Sheduled, Priority).

I don't know about the onTimeout but you could pass parameters around in the session itself, the app context or in a static variable (discouraged would some say). But the name tells me it is invoked when the user's session times out and you want to do some cleanup.

like image 1
akarnokd Avatar answered Nov 03 '22 20:11

akarnokd


Using the JMS is the right way to do it, but it's heavier weight.

The advantage you get is that if you need multiple servers, one server or whatever, once the servers are configured, your "Threading" can now be distributed to multiple machines.

It also means you don't want to send a message for a truly trivial amount of work or with a massive amount of data. Choose your interface points well.

like image 1
Bill K Avatar answered Nov 03 '22 21:11

Bill K