代码之家  ›  专栏  ›  技术社区  ›  Hank Jesus M C

如何将会话Bean注入到消息驱动Bean中?

  •  5
  • Hank Jesus M C  · 技术社区  · 16 年前

    @Stateless
    public class TestBean implements TestBeanRemote {
    
      public void doSomething() {
        // business logic goes here
      }
    }
    

    匹配接口:

    @Remote
    public interface TestBeanRemote {
    
      public void doSomething();
    }
    

    这是我的MDB:

    @MessageDriven(mappedName = "jms/mvs.TestController", activationConfig =  {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
        })
    public class TestController implements MessageListener {
    
     @EJB
     private TestBean testBean;
    
        public TestController() {
        }
    
        public void onMessage(Message message) {
          testBean.doSomething();
        }
    }
    

    不幸的是,在将其部署到glassfish v3并将消息发送到相应的JMS队列时,我发现了以下错误:glassfish无法定位TestBean EJB:

    java.lang.IllegalStateException: Exception attempting to inject Remote ejb-ref name=mvs.test.TestController/testBean,Remote 3.x interface =mvs.test.TestBean,ejb-link=null,lookup=null,mappedName=,jndi-name=mvs.test.TestBean,refType=Session into class mvs.test.TestController
    Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=mvs.test.TestController/testBean,Remote 3.x interface =mvs.test.TestBean,ejb-link=null,lookup=null,mappedName=,jndi-name=mvs.test.TestBean,refType=Session into class mvs.test.TestController
    Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/mvs.test.TestController/testBean' in SerialContext  [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=mvs.test.TestController/testBean,Remote 3.x interface =mvs.test.TestBean,ejb-link=null,lookup=null,mappedName=,jndi-name=mvs.test.TestBean,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'mvs.test.TestBean#mvs.test.TestBean' [Root exception is javax.naming.NamingException: Lookup failed for 'mvs.test.TestBean#mvs.test.TestBean' in SerialContext  [Root exception is javax.naming.NameNotFoundException: mvs.test.TestBean#mvs.test.TestBean not found]]]
    

    • 这是将会话bean注入另一个bean(特别是消息驱动bean)的正确方法吗?
    • 为什么命名查找失败?
    4 回复  |  直到 13 年前
        1
  •  6
  •   Pascal Thivent    16 年前

    你能试着这样定义吗:

    @Remote
    public interface TestBeanRemote {
    
      public void doSomething();
    }
    
    @Stateless(name="TestBeanRemote")
    public class TestBean implements TestBeanRemote {
    
      public void doSomething() {
        // business logic goes here
      }
    }
    

    然后在MDB中:

    @MessageDriven(mappedName = "jms/mvs.TestController", activationConfig =  {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
        })
    public class TestController implements MessageListener {
    
        @EJB(beanName="TestBeanRemote")
        private TestBeanRemote testBean;
    
        public TestController() {
        }
    
        public void onMessage(Message message) {
          testBean.doSomething();
        }
    }
    

        2
  •  6
  •   Roland Tiefenbrunner    14 年前

    我认为第一个例子的问题是您试图注入EJB的实现,而不是它的接口。如果您不定义任何接口,甚至不定义远程接口,那么ejb3.1的本地无接口视图是可能的。因此,将注入点更改为以下值应该是可行的:

     @EJB
     private TestBeanRemote testBean;
    

    希望这有帮助。

        3
  •  4
  •   Hank Jesus M C    16 年前

    我的问题似乎与控制反转有关,是由于我缺乏知识和Netbeans对类/接口名称的建议造成的。

    我发现-为了找到正确的bean和接口-我应该正确地命名它们。以下是有效的方法:

    @Remote
    public interface Test {
    
      public void doSomething();
    }
    
    @Stateless
    public class TestBean implements Test {
    
      public void doSomething() {
        // business logic goes here
      }
    }
    

    在MDB中我访问“测试”

    @MessageDriven(mappedName = "jms/mvs.TestController", activationConfig =  {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
        })
    public class TestController implements MessageListener {
    
        @EJB
        private Test testBean;
    
        public TestController() {
        }
    
        public void onMessage(Message message) {
          testBean.doSomething();
        }
    }
    
        4
  •  3
  •   Hank Jesus M C    16 年前

    好的,我发现如果我将注解@LocalBean添加到会话bean中,它就可以工作了。什么。。。?

    推荐文章