Is it possible to make the container inject the same stateful session bean instance into multiple other stateful session beans?
Given the following classes:
@Stateful
public class StatefulTwoBean implements StatefulTwo {
@EJB
private StatefulOne statefulOne;
}
@Stateful
public class StatefulThreeBean implements StatefulThree {
@EJB
private StatefulOne statefulOne;
}
In the above example, StatefulTwoBean and StatefulThreeBean each get injected their own instance of StatefulOneBean.
Is it possible to make the container inject the same instance of StatefulOneBean into both StatefulTwoBean and StatefulThreeBean?
The problem is this – Stateful beans’ isntances are allocated by differentiating the clients that call them. Glassfish (and perhaps others) don’t propagate this difference on injected beans. The EJB specification, as far as I remember, isn’t clear about this.
So your solution is to implement the differentiation yourself. How to achieve this. I’m not pretending this is the most beautiful solution, but it worked. – we did it by putting a Facade (an EJB itself) (I’m calling it a facade, although it does not entirely cover the facade pattern) in front of all our EJBs, with the following code:
The important parameter is
sessionId– this is something both the client and the server know about, and identifies the current seesion between them.On the client we used a dynamic proxy to call this facade. So the calls look like this:
getBean(MyConcreteEJB.class).someMethod(), an the getBean method created the proxy, so that callers didn’t have to know about the facade bean.The
SessionRegistryhadAnd the
SessionContextwas simply a Map providingset(key, value)andget(key)So now, instead of using
@Statefulbeans to store your state, you could use theSessionContext.