Discussions

General J2EE: forking a background process from a session bean

  1. Is there any way to fork a process from a session bean in the J2EE environment? I have a program that I need to run in the background mode that I need to fire off from my stateful session bean

    Thanks
  2. Forking a process is usually an operating-system dependent operation and so is probably prohibited. May I suggest that you call an object outside of your session bean?
  3. Calling an object outside the session bean in the same runtime environment won't help: it is still not permitted.
    One common way to achieve "background" processing in EJBs is to use JMS and message driven beans. The session bean would place a message in a queue, and the MDBs would pick it up and run it in the background. This solution fits better into the EJB paradigm than, say, using an RMI server to spawn a thread. It is automatically transactional, scalable, etc.

    Gal
  4. Gal Binyamini writes:

    "One common way to achieve "background" processing in EJBs is to use JMS and message driven beans.

    One common way to achieve "background" processing in EJBs is to use JMS and message driven beans. The session bean would place a message in a queue, and the MDBs would pick it up and run it in the background."

    The problem here is that this fellow wants to spin off another process for whatever reason. I'm not sure why. The problem with spinning off a process that it's completely o-system dependent.

    There are two possible approaches to this problem. The first (and most elegant) is Gal's, which depends on the EJB container to fire up the number of instances needed of the MDB. If for whatever reason, you cannot use an MDB (probably because you are using a system utility or something), you may have to create a daemon process which acts as a JMS listener. If the underlying utility or API is threads-safe spawn a thread. If not, create a process to do
    what you need to do.

  5. Don, thanks for pointing that out. I thought he was just trying to some background work, but upon reading the original post again I guess maybe he wants to actually start a seperate process.
    In that case, I suppose the only valid solution would be to go outside the EJB spec, and use a seperate daemon. The daemon can be accessed using JMS, or directly via RMI.

    Gal
  6. Actually the question we should be asking is why you want to fork a new process from a session bean? What are you trying to do?