I'm trying to pass some parameters from an interceptor to ejb context to be used later for logging. In the interceptor I'm doing this:
@AroundInvoke
public Object retrieveAdminData(InvocationContext context) throws Exception {
User simulatingAdmin = ...;
context.getContextData().put(Globals.ADMIN_USER_KEY, simulatingAdmin));
return context.proceed();
}
Then later in lifecycle I'm looking up ejb context and getting this value:
((SessionContext) new InitialContext().lookup("java:comp/EJBContext")).getContextData().get(Globals.ADMIN_USER_KEY);
Usually it works fine, but in some cases when I make a call from one bean to another, I get this exception when trying to get context data:
java.lang.IllegalStateException: Illegal call to EJBContext method. The bean is in running business method state. It cannot perform 'getting the Message Context for Stateless EJB' action(s). Refer to the EJB specification for more details.
at weblogic.ejb.container.internal.BaseEJBContext.checkAllowedMethod(BaseEJBContext.java:93)
at weblogic.ejb.container.internal.BaseEJBContext.checkAllowedToGetMessageContext(BaseEJBContext.java:291)
at weblogic.ejb.container.internal.SessionEJBContextImpl.checkAllowedToGetMessageContext(SessionEJBContextImpl.java:210)
at weblogic.ejb.container.internal.SessionEJBContextImpl.getMessageContextJAXWS(SessionEJBContextImpl.java:65)
at weblogic.ejb.container.internal.SessionEJBContextImpl.getContextData(SessionEJBContextImpl.java:302)
All calls between beans are local calls. Could someone please point me into why it works for some beans, but not for others?
EDIT I've noticed that I stop being able to get ejb context if the call is from beans that are in different jars. Is this not allowed in EJB? Can I use ThreadLocal as a workaround?