我對Java EE是比較新的,所以這可能是愚蠢的。請耐心等待我請求:D如何將會話Bean注入消息驅動Bean?
我想將無狀態會話bean注入消息驅動bean。基本上,MDB獲取JMS消息,然後使用會話bean執行工作。會話bean擁有業務邏輯。
這裏是我的會話Bean:
@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的第三版,並且將消息發送到相應的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)的正確方法嗎?
- 爲什麼命名查找失敗?
我現在有點遠了。 @LocalBean標識沒有接口的bean。所以這不是我想要的,即使它有效:D – Hank 2010-03-18 07:34:47
您應該使用「@EJB private TestBeanRemote testBean;」 – Dagvadorj 2013-04-13 11:23:53