Sharing objects between EJBs


EJB programming & troubleshooting: Sharing objects between EJBs

  1. Sharing objects between EJBs (2 messages)


    I have one runnable object on which start() method is already executed (I.e. thread in this class is already started executing in a continuous loop).

    I want to access this runnable object in all my stateless session beans so that they can pass on some events (through Observer/Observable architecture) to its thread which is continuously waiting for events to act upon.

    First of all can any body tell me whether it is possible to access one common runnable object through EJBs? If the answer is yes will it violate any EJB specification?

    Please let me know your expert suggestions.


    Threaded Messages (2)

  2. Sharing objects between EJBs[ Go to top ]

    I would highly recommend using JMS to perform this task. In your runnable object have it listen for JMS messages to get its information from you beans. This way you will not get yourself in trouble with the container.

  3. Sharing objects between EJBs[ Go to top ]

    First of all, this kind of thing will violate the EJB spec. EJBs are not allowed to share any state, including a reference to a thread.
    It may be possible to share the thread between all the beans running on the same JVM by putting it's reference in a static variable. However, this is illegal and may not work on some App servers (probably will, though). Sharing the thread between different JVMs (and possibly different machines in a clustered environment) is obviously impossible.

    Using JMS (possibly with message-driven beans) will make your code more portable and scalable. A single thread may not be sufficient (or may be too much), and the JMS server can pool the threads to make better use of your resources.