如何将一个会话Bean注入消息驱动Bean? [英] How to inject a Session Bean into a Message Driven Bean?

查看:161
本文介绍了如何将一个会话Bean注入消息驱动Bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将一个无状态会话bean注入到Java EE中,因此这可能是愚蠢的。一个消息驱动的bean。基本上,MDB获取JMS消息,然后使用会话bean执行工作。会话Bean拥有业务逻辑。



这是我的Session Bean:

  @Stateless 
public class TestBean implements TestBeanRemote {

public void doSomething(){
//商业逻辑转到这里
}
}

匹配界面:

  @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)
})
公共类TestController实现MessageListener {

@EJB
私人TestBean testBean;
$ b $ public TestController(){
}

public void onMessage(Message message){
testBean.doSomething();


$ / code>

到目前为止,并非火箭科学,对吗? p>

不幸的是,在将这部署到glassfish v3并向相应的JMS队列发送消息时,出现glassfish无法找到TestBean EJB的错误:

  java.lang.IllegalStateException:尝试注入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 =会话到类mvs.test.TestController 
引起:com.sun.enterprise .container.common.spi.util.InjectionException:异常尝试注入远程ejb-ref名称= mvs.test.TestController / testBean,远程3.x接口= mvs.test.TestBean,ejb-link = null,lookup = null ,mappedName =,jndi-name = mvs.test.TestBean,refType = Session到类mvs.test.TestController
导致:javax.naming.NamingException:查找失败的'java:comp / env / mvs.test .TestControll er / testBean'in SerialContext [根异常是javax.naming.NamingException:异常解析Ejb for'远程ejb-ref名称= mvs.test.TestController / testBean,远程3.x接口= mvs.test.TestBean,ejb-link = null,lookup = null,mappedName =,jndi-name = mvs.test.TestBean,refType = Session'。实际(可能内部)用于查找的远程JNDI名称是'mvs.test.TestBean#mvs.test.TestBean'[根异常是javax.naming.NamingException:'mvs.test.TestBean#mvs.test.TestBean查找失败'in SerialContext [根异常是javax.naming.NameNotFoundException:mvs.test.TestBean#mvs.test.TestBean not found]]]

所以我的问题是:


  • 这是将会话bean注入另一个bean的正确方法(特别是一个消息驱动的bean)?

  • 为什么命名查找失败?

解决方案

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


I'm reasonably new to Java EE, so this might be stupid.. bear with me pls :D

I would like to inject a stateless session bean into a message-driven bean. Basically, the MDB gets a JMS message, then uses a session bean to perform the work. The session bean holds the business logic.

Here's my Session Bean:

@Stateless
public class TestBean implements TestBeanRemote {

  public void doSomething() {
    // business logic goes here
  }
}

The matching interface:

@Remote
public interface TestBeanRemote {

  public void doSomething();
}

Here's my 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();
    }
}

So far, not rocket science, right?

Unfortunately, when deploying this to glassfish v3, and sending a message to the appropriate JMS Queue, I get errors that glassfish is unable to locate the 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]]]

So my questions are:

  • is this the correct way of injecting a session bean into another bean (particularly a message driven bean)?
  • why is the naming lookup failing?

解决方案

Ok, I found out that if I add the annotation @LocalBean to the session bean, it works. What the ...?

这篇关于如何将一个会话Bean注入消息驱动Bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆