Search code examples
javaejb

How to pass data to EJBContext on client side?


I have following interceptor on server:

@Resource
EJBContext ejbContext;

@AroundInvoke
public Object onInvocation( InvocationContext aInvocationContext ) throws Exception
{
    final Object myValue = ejbContext.getContextData().get("MyKey");
    ...
    Object proceed = aInvocationContext.proceed();
    ...
    return proceed;
}

How to pass data ("MyKey") to EJBContext on client side? I tried to lookup it but I get javax.naming.InvalidNameException' exception during lookup or EJBCLIENT000409: No more destinations are available when I call getContextData(). I tried several ways I do not know If I do something wrong or EJBContext is some special resource and It is not possible to modify it on client.

How lookup name should look like?I tried java:comp/EJBContext, appName/moduleName/EJBContext.


Solution

  • I created client interceptor.

    EJBClientContext.requireCurrent().registerInterceptor( 0, new MyClientInterceptor() );
    

    There is important note in registerInterceptor method javadoc:

    Note: If an interceptor is added or removed after a proxy is used, this will not affect the proxy interceptor list.

    MyClientInterceptor:

    import org.jboss.ejb.client.EJBClientInterceptor;
    import org.jboss.ejb.client.EJBClientInvocationContext;
    
    public class MyClientInterceptor implements EJBClientInterceptor
    {
    
        @Override
        public void handleInvocation( EJBClientInvocationContext aContext ) throws Exception
        {
            Object myValue =...
            aContext.getContextData().put( "MyKey", myValue );
            aContext.sendRequest();
    
        }
    
        @Override
        public Object handleInvocationResult( EJBClientInvocationContext aContext ) throws Exception
        {
            return aContext.getResult();
        }
    }