我无法在另一个 SLSB 中注入 SLSB。实际创建了 3 个项目 1) 创建了一个带有 MDB 的 EJB 项目 2) 创建了一个带有无状态会话 bean 的 EJB 项目以发布消息 3) 创建了一个带有无状态会话 bean 的 EJB 项目以注入上述会话 bean 但是在注入时我是无法注入它返回 null 的 EJB
代码如下 1) MDB:
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "activemq/queue/TestQueue"),
@ActivationConfigProperty(propertyName="acknowledgeMode", propertyValue="Auto-acknowledge")
})
@ResourceAdapter("activemq-ra.rar")
public class ConsumerMDB implements MessageListener {
public void onMessage(Message message) {
try {
System.out.println("Queue: Received a TextMessage at " + new Date());
TextMessage msg = (TextMessage) message;
System.out.println("Message is : " + msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
2) 会话 Bean 1
package com.springboard.session;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
@Stateless
@LocalBean
public class ProducerSession implements ProducerSessionLocal {
@Resource(mappedName="java:jboss/activemq/QueueConnectionFactory")
public static QueueConnectionFactory factory;
@Resource(mappedName = "java:jboss/activemq/queue/TestQueue")
public static Queue queue;
@Override
public void sendMessage(String msg) {
System.out.println("****************Entering into method********************");
try {
System.out.println(queue.getQueueName());
QueueConnection qConnection = factory.createQueueConnection();
QueueSession qSession = qConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
TextMessage message = qSession.createTextMessage();
message.setText(msg);
QueueSender qSender = qSession.createSender(queue);
qSender.send(message);
qSender.close();
qSession.close();
qConnection.close();
} catch (JMSException e) {
e.printStackTrace();
}
System.out.println("****************Exiting into method********************");
}
}
接口是包com.springboard.session;
import javax.ejb.Local;
@Local
public interface ProducerSessionLocal {
public void sendMessage(String msg);
}
3)第二个会话bean注入第一个会话
@Stateless
public class TestProducerLocalBean implements TestProducerLocalBeanLocal {
@EJB(mappedName = "java:global/ProducerSessionActiveMQ/ProducerSession!com.springboard.session.ProducerSessionLocal")
public ProducerSessionLocal producer;
public TestProducerLocalBean() {
System.out.println("*************Testing Producer****************");
if(producer!=null){
producer.sendMessage("This Message is from SessionBean to Session Bean to MDB");
}
else{
System.out.println("EJB is null");
}
System.out.println("**********End************************");
}
@Override
public void messageSend(String msg) {
// TODO Auto-generated method stub
}
并且出于测试目的使用了一个类 import javax.ejb.EJB;
import com.springboard.session.test.TestProducerLocalBean;
public class testEJB {
@EJB
public static TestProducerLocalBean local =new TestProducerLocalBean();
public static void main(String[] args) {
}
}
在生产者处,EJB 总是返回 null。通过使用 servlet 注入 ProducerSession 我可以做到。但是注入另一个 EJB 我无法得到它。有人可以帮我解决我所缺少的吗
提前致谢