4 Replies Latest reply on Sep 13, 2012 3:46 AM by Tomasz Banas

    Passing data between ClientInterceptor and Interceptor

    Tomasz Banas Newbie

      Hi. I must pass some data between two ears.

      I have two Interceptors. ClientSideInterceptor class:

       

      public class ProxyInterceptor implements EJBClientInterceptor{
      
      
                private static Object testCtx;
        
                @Override
                public void handleInvocation(EJBClientInvocationContext ctx) throws Exception {
                          System.out.println("ProxyInterceptor");
                          ctx.getContextData().put("one", getSessionContext());
                          ctx.sendRequest();
                }
      
      
                @Override
                public Object handleInvocationResult(EJBClientInvocationContext ctx) throws Exception {
                          return ctx.getResult();
                }
      
      
                private Object getSessionContext() {
                          if (testCtx != null)
                                    return testCtx;
                          return "no working";
                }
      
      
                 public static void setTestSessionContext(Object ctx){
                          testCtx = ctx;
                }
       }
      

       

      and ContainerInterceptor, which is getting data from ContextData.

       

      public class ContainerInterceptor{
        
                @AroundInvoke
                public Object invoke(InvocationContext inv) throws Exception {
                          Object ctx = inv.getContextData().get("one");
                          System.out.println("ContainerInterceptor" + "  " + ctx);
                          // put current context
                          return inv.proceed();
                }
      
      
      }
      

       

      I must pass ContextData between two ears. In first ear I have HelloWorldBean in which i lookup ClientBean from another ear. And my problem is that i must pass SessionContext(for simplicity String in this example) between this two ears.

      HelloWorldBean:

       

      public class HelloWorldBean implements HelloWorld {
        
        public String sayHello() throws NamingException{
                          System.out.println("[HelloWorldBean hello]");
        
                          Properties props = new Properties();
                          props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
                          props.put(Context.PROVIDER_URL, "remote://localhost:11101");
                          Context context = new InitialContext(props);
        
                          EJBClientContext.requireCurrent().registerInterceptor(0x99999, new ProxyInterceptor());
                     
                          ProxyInterceptor.setTestSessionContext("it's working");
        
                          Client client = (Client) context.lookup("Interceptor2/Client//ClientBean!client.Client");
                          System.out.println(client.saySomething());
        
                          EJBClientContext.requireCurrent().registerEJBReceiver(receiver)
                          return "HelloWorld";
         }
         
      }
      

       

      In ejb-jar i have:

      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
               version="3.0">
         <interceptors>
              <interceptor>
                  <interceptor-class>interceptor.ContainerInterceptor</interceptor-class>
               </interceptor>
          </interceptors>
          
        <assembly-descriptor>
          <interceptor-binding>
            <ejb-name>*</ejb-name>
            <interceptor-class>interceptor.ContainerInterceptor</interceptor-class>
          </interceptor-binding>
        </assembly-descriptor>
      </ejb-jar>
      

       

      So, I'm registering ClientSideInterceptor and pass to him my String, then i lookup for ClientBean. The problem is when i run client.saySomething() and ContainerInterceptor is start, there is no String in Object ctx = inv.getContextData().get("one"); Where is the problem?