3 Replies Latest reply on May 7, 2007 7:22 PM by matt.drees

    Seam component as a listener

    zzzz8

      I would like to have a Seam component used as a listener. Here's what I mean:

      @Name("myBean")
      @Stateful
      public class MyClassBean implements MyClass, PacketListener {
      @In
      Connection connection = null;
      
      @In
      List<MyMessageList> messageList = null;
      
      @Create
      public void createBean() {
       connection.addPacketListener(this);
      }
      
      public void processPacket(Packet packet) {
       messageList.add(Packet);
      }
      }


      The processPacket method has its interface defined by the PacketListener interface and is thus implemented in my bean. So I want the Seam component to be a listener - this can be seen in the statement:

      connection.addPacketListener(this);


      This seems to work... up to a point. So when the processPacket method is invoked, I notice messageList doesn't seem to be injected. What am I doing wrong and is there any way around this?

        • 1. Re: Seam component as a listener
          matt.drees

          I ran into something like that. The problem is that in

          connection.addPacketListener(this);
          

          "this" is a reference to the actual bean, and not a reference to the bean's proxy. Interceptors (which are used for injection) are only called if you invoke a method on the proxy, not the bean itself.

          I don't know the best way to solve the problem. In your case you could do something like
          connection.addPacketListener(Component.getInstance(this.getClass());
          

          I don't really like it, but it should work.

          If anyone knows a nicer way for a bean to get a reference to its own proxy, please speak up.

          • 2. Re: Seam component as a listener
            zzzz8

            Hi Matt,

            Thanks for the reply. I actually tried something similar to that:

            connection.addPacketListener(Component.getInstance("myBean"));


            and

            connection.addPacketListener(Component.getInstance("myBean", false));


            Both didn't work for me though...

            • 3. Re: Seam component as a listener
              matt.drees

              Odd. Well, I'm not sure what's up. Sorry.