General J2EE: How to load/start a class with a main method inside an EAR file

  1. Hi. I have a J2EE application deployed in a JBoss 3.2.5 server. As part of this application, I have a piece of code that needs to constantly run in the background waiting for some sort of "signal" to do some processing. This signal comes from an EJB inside my J2EE application. After analyzing my choices, I concluded that this code is not well-suited to be an EJB or a servlet. Instead, it makes more sense to have it running as a standard Java "main" application, waiting for either a socket or JMS message as the signal. However, I don't want to have this piece of code running in a separate JVM window, since it's part of my J2EE application anyway, having both of them exactly the same life cycle.

    The question is: does J2EE/JBoss provide something to make this kind of Java "main" applications to be loaded and started automatically as part of a J2EE application? If I deploy both the Java "main" application and the J2EE components inside the same EAR file, how can I tell J2EE/JBoss to also load and start the class or classes having a main method? Thank you very much in advance.
  2. Why not have that signal just invoke a message bean? That is the primary way of doing asynch stuff in j2ee app server, afaik.

    -- J. Betancourt
  3. Oops. Reread your message, JMS won't help, and you mentioned it already. :)

    There are some systems that run inside a j2ee 'app'. For example, Quartz, see http://www.opensymphony.com/quartz/. Quartz in response to an event does scheduling stuff, yours will do other stuff. Note, I'm not indicating to use Quartz for this, but as an analogous scenario perhaps.
  4. The problem is that I need to process a file in the local filesystem (previously uploaded by an Internet client). AFAIK it's not possible to access the filesystem within a EJB, so I have to implement the process as a standard Java application.
  5. Can you not create a servlet that calls your main method.

    Override the init() method in the servlet and set the servlet to auto-start. Not sure if JBoss can autostart servlets assume it does as WebSphere does.
  6. Try JMX[ Go to top ]

    You should try JMX MBean. Perhaps you can do something in start() method of MBean (start listening thread or something else...)