2 Replies Latest reply on Jul 24, 2007 1:07 AM by gurubbc

    Call Back Listeners not working - Please help

    gurubbc

      EJB 3.0 Call Back methods are working, but the listeners are not working.

      ListenerHelloCallBack.java (Interface)
      /**
      * This is the ListenerHelloCallBack business interface.
      */

      public interface ListenerHelloCallBack {

      /**
      * @return a greeting to the client.
      */
      public String listenerHelloCallBack();
      }


      Bean class:
      import javax.ejb.*;
      import javax.ejb.*;

      /**
      * Demonstration stateless session bean.
      */
      @Stateful
      @Remote(ListenerHelloCallBack.class)
      @CallbackListener(CallBackListener.class)
      public class ListenerHelloCallBackBean implements ListenerHelloCallBack {
      public String listenerHelloCallBack() {
      System.out.println("listenerHelloCallBack()");
      return "Hello, Call Back Listener World!";
      }
      }

      Call Back Listener class:

      import javax.ejb.*;
      import javax.ejb.*;

      public class CallBackListener {

      @Init
      public void sayInit(Object obj)
      {
      System.out.println("This would be called upon Init from listener class");
      }

      @PostConstruct
      public void sayPostConstruct(Object obj)
      {
      System.out.println("This would be called upon post construct from listener class");
      }


      @PreDestroy
      public void sayPreDestroy(Object obj)
      {
      System.out.println("This would be called upon pre destroy from listener class");
      }

      @PrePassivate
      public void sayPrePassivate(Object obj)
      {
      System.out.println("This would be called upon pre passivate from listener class");
      }

      @PostActivate
      public void sayPostActivate(Object obj)
      {
      System.out.println("This would be called upon post activate from listener class");
      }

      public void finalize()
      {
      System.out.println("Finalize method is getting called...");
      }
      }


      Client:

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import java.util.*;
      import javax.naming.*;


      /**
      * This class is an example of client code which invokes
      * methods on a simple, remote stateless session bean.
      */
      public class ListenerHelloCallBackClient {

      public static void main(String[] args) throws Exception {
      /*
      * Obtain the JNDI initial context.
      *
      * The initial context is a starting point for
      * connecting to a JNDI tree. We choose our JNDI
      * driver, the network location of the server, etc
      * by passing in the environment properties.
      */

      Properties p = new Properties();

      //The JNDI properties you set depend
      //on which server you are using.
      //These properties are for the Remote Server.
      p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      p.put("java.naming.provider.url", "jnp://localhost:1099");
      p.put("java.naming.factory.url.pkgs", "org.jboss.naming rg.jnp.interfaces");

      Context ctx = new InitialContext(p);


      /*
      * Get a reference to a bean instance, looked up by class name
      */
      ListenerHelloCallBack listenerHelloCallBack = (ListenerHelloCallBack) ctx.lookup("ListenerHelloCallBack");


      /*
      * Call the helloCallBack() method on the bean.
      * We then print the result to the screen.
      */
      System.out.println(listenerHelloCallBack.listenerHelloCallBack());
      listenerHelloCallBack=null;

      }
      }


      But, none of the call back methods like @init, @postconstruct are working.

      Please help.

      Thanks,
      Guru

        • 1. Re: Call Back Listeners not working - Please help
          sankarreddy123

          there are some mistakes in u r code... there is nothing like @callbackfilter annotation present in api. we have to use @Intercepters annotation which takes u r lister class file as i/p. and also the life cycle lister methods shoud be given with InvocationContext object as perameter to the methods which are using annoations like postcreate,..etc . And also one thing, we should not use @Init in the context of ejb3.0 session beans(message also) bcos they dont have any home objects to call ejbcreate method. ur code works fine after these modications in my machine.

          • 2. Re: Call Back Listeners not working - Please help
            gurubbc

            I have done as you said. I have include @Interceptors instead of @CallbackListener annotation.

            I have removed @Init also.

            This time also, I did not find any call back methods getting fired. Why? This is a stateful session bean.

            Thanks,
            Guru