JBoss 7 嵌入式 ActiveMQ - MDB 消息侦听器不工作
JBoss 7 Embedded ActiveMQ - MDB message listener not working
我必须使用 JBOSS 4.2.2 和嵌入式 activeMQ 5.3.0 升级现有应用程序
为了尝试 jboss 7.3 使用嵌入式活动 MQ,我执行了以下操作。
按照 https://developer.jboss.org/docs/DOC-18798#jive_content_id_ActiveMQ_as_an_internal_messaging_broker
中的说明进行操作
- 我在 JBoss 7.3 资源适配器
中配置了 activemq-rar-5.6.0.rar
- 在 jboss.
中部署了 jboss 快速启动 hello-world-mdb war 文件
- 尝试使用消息驱动的 bean (MDB) 发送和使用消息
我面临的问题是,我没有看到消息侦听器正在使用消息。
以下是我的配置
1.resource-独立适配器子系统更改-full.xml 文件
<subsystem xmlns="urn:jboss:domain:resource-adapters:5.0">
<resource-adapters>
<resource-adapter id="activemq-rar-5.6.0.rar">
<archive>
activemq-rar-5.6.0.rar
</archive>
<transaction-support>XATransaction</transaction-support>
<!--<config-property name="ServerUrl">tcp://localhost:61616</config-property> -->
<config-property name="ServerUrl">vm://localhost</config-property>
<connection-definitions>
<connection-definition class-name="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" jndi-name="java:/activemq/ConnectionFactory" enabled="true" use-java-context="true" pool-name="ActiveMQConnectionFactoryPool" use-ccm="true">
<xa-pool>
<min-pool-size>1</min-pool-size>
<max-pool-size>20</max-pool-size>
</xa-pool>
</connection-definition>
</connection-definitions>
<admin-objects>
<admin-object class-name="org.apache.activemq.command.ActiveMQQueue" jndi-name="java:/queue/HELLOWORLDMDBQueue" use-java-context="true" pool-name="HELLOWORLDMDBQueue">
<config-property name="PhysicalName">HELLOWORLDMDBQueue</config-property>
</admin-object>
<admin-object class-name="org.apache.activemq.command.ActiveMQTopic" jndi-name="java:/topic/HELLOWORLDMDBTopic" use-java-context="true" pool-name="HELLOWORLDMDBTopic">
<config-property name="PhysicalName">HELLOWORLDMDBTopic</config-property>
</admin-object>
</admin-objects>
</resource-adapter>
</resource-adapters>
</subsystem>
- 独立-full.xml domain:ejb3 子系统 mdb 更改
<mdb>
<resource-adapter-ref resource-adapter-name="activemq-rar-5.6.0.rar"/>
<bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
</mdb>
helloworld-mdb.war包含以下两个类.
- 消息发送者
@WebServlet("/HelloWorldMDBServletClient")
public class HelloWorldMDBServletClient extends HttpServlet {
private static final long serialVersionUID = -1949285948189796311L;
@Resource(mappedName = "java:/activemq/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue")
private Destination queue;
private Connection connection;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Session session = null;
MessageProducer sender = null;
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.write(
"<h1>Quickstart: Example demonstrates the use of <strong>JMS 2.0</strong> and <strong>EJB 3.2 Message-Driven Bean</strong> in JBoss EAP.</h1>");
try {
connection = connectionFactory.createConnection();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
sender = session.createProducer(queue);
sender.setDeliveryMode(DeliveryMode.PERSISTENT);
out.write("<p>Sending messages to <em>" + queue + "</em></p>");
out.write("<h2>The following messages will be sent to the destination:</h2>");
for (int i = 0; i < 3; i++) {
String text = "This is message " + (i + 1);
TextMessage response = session.createTextMessage(text);
sender.send(response);
out.write("Message (" + i + "): " + text + "</br>");
}
out.write(
"<p><i>Go to your JBoss EAP server console or server log to see the result of messages processing.</i></p>");
} catch (JMSException e) {
e.printStackTrace();
}finally {
try {
if (sender != null) {
sender.close();
}
if (session != null) {
session.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
- 消息消费者 MDB
@MessageDriven(name = "HelloWorldQueueMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destination", propertyValue = "HELLOWORLDMDBQueue"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")})
@ResourceAdapter(value="activemq-rar-5.6.0.rar")
public class HelloWorldQueueMDB implements MessageListener {
private static final Logger LOGGER = Logger.getLogger(HelloWorldQueueMDB.class.toString());
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
LOGGER.info("Received Message from queue: " + msg.getText());
} else {
LOGGER.warning("Message of wrong type: " + rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
}
我通过转到 http://localhost:8080/helloworld-mdb/HelloWorldMDBServletClient 从浏览器发送消息。
但是我没有看到消息侦听器正在使用消息,它们没有显示在 jboss 日志中。
JBoss 控制台日志如下所示
在 HelloWorldMDBServletClient
中,您正在创建一个 transacted 会话来发送消息,即:
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
但是,您从来没有打电话给 session.commit()
,所以在我看来,消息从未真正发送过。
您应该调用 session.commit()
来发送消息或将会话创建为非事务处理,例如:
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
我必须使用 JBOSS 4.2.2 和嵌入式 activeMQ 5.3.0 升级现有应用程序
为了尝试 jboss 7.3 使用嵌入式活动 MQ,我执行了以下操作。
按照 https://developer.jboss.org/docs/DOC-18798#jive_content_id_ActiveMQ_as_an_internal_messaging_broker
中的说明进行操作- 我在 JBoss 7.3 资源适配器 中配置了 activemq-rar-5.6.0.rar
- 在 jboss. 中部署了 jboss 快速启动 hello-world-mdb war 文件
- 尝试使用消息驱动的 bean (MDB) 发送和使用消息
我面临的问题是,我没有看到消息侦听器正在使用消息。
以下是我的配置
1.resource-独立适配器子系统更改-full.xml 文件
<subsystem xmlns="urn:jboss:domain:resource-adapters:5.0">
<resource-adapters>
<resource-adapter id="activemq-rar-5.6.0.rar">
<archive>
activemq-rar-5.6.0.rar
</archive>
<transaction-support>XATransaction</transaction-support>
<!--<config-property name="ServerUrl">tcp://localhost:61616</config-property> -->
<config-property name="ServerUrl">vm://localhost</config-property>
<connection-definitions>
<connection-definition class-name="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" jndi-name="java:/activemq/ConnectionFactory" enabled="true" use-java-context="true" pool-name="ActiveMQConnectionFactoryPool" use-ccm="true">
<xa-pool>
<min-pool-size>1</min-pool-size>
<max-pool-size>20</max-pool-size>
</xa-pool>
</connection-definition>
</connection-definitions>
<admin-objects>
<admin-object class-name="org.apache.activemq.command.ActiveMQQueue" jndi-name="java:/queue/HELLOWORLDMDBQueue" use-java-context="true" pool-name="HELLOWORLDMDBQueue">
<config-property name="PhysicalName">HELLOWORLDMDBQueue</config-property>
</admin-object>
<admin-object class-name="org.apache.activemq.command.ActiveMQTopic" jndi-name="java:/topic/HELLOWORLDMDBTopic" use-java-context="true" pool-name="HELLOWORLDMDBTopic">
<config-property name="PhysicalName">HELLOWORLDMDBTopic</config-property>
</admin-object>
</admin-objects>
</resource-adapter>
</resource-adapters>
</subsystem>
- 独立-full.xml domain:ejb3 子系统 mdb 更改
<mdb>
<resource-adapter-ref resource-adapter-name="activemq-rar-5.6.0.rar"/>
<bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
</mdb>
helloworld-mdb.war包含以下两个类.
- 消息发送者
@WebServlet("/HelloWorldMDBServletClient")
public class HelloWorldMDBServletClient extends HttpServlet {
private static final long serialVersionUID = -1949285948189796311L;
@Resource(mappedName = "java:/activemq/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue")
private Destination queue;
private Connection connection;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Session session = null;
MessageProducer sender = null;
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.write(
"<h1>Quickstart: Example demonstrates the use of <strong>JMS 2.0</strong> and <strong>EJB 3.2 Message-Driven Bean</strong> in JBoss EAP.</h1>");
try {
connection = connectionFactory.createConnection();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
sender = session.createProducer(queue);
sender.setDeliveryMode(DeliveryMode.PERSISTENT);
out.write("<p>Sending messages to <em>" + queue + "</em></p>");
out.write("<h2>The following messages will be sent to the destination:</h2>");
for (int i = 0; i < 3; i++) {
String text = "This is message " + (i + 1);
TextMessage response = session.createTextMessage(text);
sender.send(response);
out.write("Message (" + i + "): " + text + "</br>");
}
out.write(
"<p><i>Go to your JBoss EAP server console or server log to see the result of messages processing.</i></p>");
} catch (JMSException e) {
e.printStackTrace();
}finally {
try {
if (sender != null) {
sender.close();
}
if (session != null) {
session.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
- 消息消费者 MDB
@MessageDriven(name = "HelloWorldQueueMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destination", propertyValue = "HELLOWORLDMDBQueue"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")})
@ResourceAdapter(value="activemq-rar-5.6.0.rar")
public class HelloWorldQueueMDB implements MessageListener {
private static final Logger LOGGER = Logger.getLogger(HelloWorldQueueMDB.class.toString());
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
LOGGER.info("Received Message from queue: " + msg.getText());
} else {
LOGGER.warning("Message of wrong type: " + rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
}
我通过转到 http://localhost:8080/helloworld-mdb/HelloWorldMDBServletClient 从浏览器发送消息。 但是我没有看到消息侦听器正在使用消息,它们没有显示在 jboss 日志中。
JBoss 控制台日志如下所示
在 HelloWorldMDBServletClient
中,您正在创建一个 transacted 会话来发送消息,即:
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
但是,您从来没有打电话给 session.commit()
,所以在我看来,消息从未真正发送过。
您应该调用 session.commit()
来发送消息或将会话创建为非事务处理,例如:
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);