Discussions

EJB design: How to extend EJB?

  1. How to extend EJB? (1 messages)

    I want to introduce a global variable, state, so that the application behaves differently based on the state. What I do NOT want is change all my ejbs and the calls to my ejbs. Any ideas how to do it?

    Let me walk through some of the alternatives I have already evaluated:

    1. A Singleton: This option has been suggested in several previous threads, but the overall conclustion is that a singleton does not work in clustered environments.

    2. A Stateful session bean combined with a State Machine as in Petstore: This option seems fine. The problem with this option is that I have to send the session-bean/state-machine back and forth in the application. The latter implies that I have to change the signature of every method in the application. As I am a lazy programmer, I really want to avoid it.

    3. Any suggestions???

    Based on alternative 2, I have tried the following:

    1. create a base class and base interface for all my ejbs.
    2. let all ejbs extend the basa class and the base interface
    3. in the base class, store the state variable.
    4. provide getter and setter methods for the state variable in the base class.
    For example bean.setState(myState)

    The above scenario is "almost" fine, except that a call like

    bean = home.create()

    or

    bean = home.findByPrimaryKey(id)

    happens before I can call

    bean.setState(myState)

    What can I do?

    I would appreciate any suggestions.

    Thanks

    Threaded Messages (1)

  2. How to extend EJB?[ Go to top ]

    Look into GoF's State to change your EJB's behaviour dynamically (without elseif chains or switch). You may also want to employ an Abstract Factory to instantiate the State objects, or keep them in a Flyweight pool.

    You may store the state in the JNDI and wrap access to it in a utility class, instead of trying to build a cluster neutral Singleton.