我是 J2EE - MDB 的新手,但我正在尝试创建一个消息驱动 Bean (MDB),它只侦听一个队列(读取消息),然后处理该消息并将其推送到另一个队列。我在 Google 上找到了几个工作示例,以单独的方式完成这两项任务,但我一直在尝试在同一个 MDB 上完成这两项任务时遇到问题。
这是我的 MDB 的代码
@MessageDriven(mappedName = "jms/propuestasQ")
public class ObtenerNumPolizaBean implements MessageListener {
@Resource(name="jms/polizasQCF")
private QueueConnectionFactory connectionFactory;
private Connection connection;
@Resource(name = "jms/polizasQ")
private Destination targetQueue;
@PostConstruct
private void initJMS() {
try {
connection = connectionFactory.createConnection();
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
@PreDestroy
private void closeJMS() {
try {
connection.close();
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
/**
* @see MessageListener#onMessage(Message)
*/
@Override
public void onMessage(Message message) {
//validate the received message type
if (message instanceof FolioEntity) {
try {
//generate Web Service proxy
GenerarFoliosImplService serviceGenerarFolios = new GenerarFoliosImplService();
GenerarFoliosImplDelegate delGenerarFolios = serviceGenerarFolios.getGenerarFoliosImplPort();
//call the method with the object
FolioEntity responseFolio = delGenerarFolios.generarFolios((FolioEntity)message);
System.out.println("Bean generated the following FolioNumber: " + responseFolio.getNumeroFolio());
//put the message on the next queue
putMessage(responseFolio);
}
catch (JMSException e) {
throw new RuntimeException(e);
}
}
else {
throw new IllegalArgumentException("Message must be of type FolioEntity");
}
}
private void putMessage(final FolioEntity folio) throws JMSException {
final Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
final MessageProducer producer = session.createProducer(targetQueue);
final ObjectMessage objectMessage = session.createObjectMessage();
producer.send(objectMessage);
session.close();
}
这是我的 ejb-jar.xml 文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<display-name>MDBRenovarPolizaEJB </display-name>
<enterprise-beans >
<message-driven>
<ejb-name>ObtenerNumPolizaBean</ejb-name>
<message-destination-ref>
<description />
<message-destination-ref-name>
jms/polizasQ
</message-destination-ref-name>
<message-destination-type>
javax.jms.Queue
</message-destination-type>
<message-destination-usage>
ConsumesProduces
</message-destination-usage>
<message-destination-link>
jms/polizasQ
</message-destination-link>
</message-destination-ref>
<message-destination-ref>
<description />
<message-destination-ref-name>
jms/polizasQCF
</message-destination-ref-name>
<message-destination-type>
javax.jms.QueueConnectionFactory
</message-destination-type>
<message-destination-usage>
ConsumesProduces
</message-destination-usage>
<message-destination-link>
jms/polizasQCF
</message-destination-link>
</message-destination-ref>
我遇到的问题是我无法在 WAS 控制台 8.5.5 上设置“消息驱动的 Bean 侦听器绑定”,当我尝试设置激活规范时出现错误:
MDBRenovarPolizaModelEJB.jar\META-INF\ejb-jar_merged.xml (The system cannot find the file specified.)
我不知道这个例外是什么意思。我一直以这种方式设置“激活规范”来监听特定队列,所以我不知道这个文件是什么:“ejb-jar_merged.xml”。
有什么线索吗?提前致谢。
或者,如果有人有一个工作示例来逐步实现这一点,以使其在 WebSphere 下工作,那将很有用。