listening to 2 JMS queues from same MDB
jagdip Jun 12, 2014 6:51 PMI want to see if same MDB can listen to two JMS queues. I was playing with jboss quickstarts. I deployed helloworld-mdb and played with it. I tried annotations with two separate names and attributes but I got error while compiling that duplicate annotations are not allowed. then, i tried ejb-jar.xml. that also did not work. i got error while deploying that destination is mandatory. i am using jboss as 7.1. does anyone know if it is possible and share the details.
code details
mdb
-----------------------------------------------------------------------------------
package org.jboss.as.quickstarts.mdb;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven
public class HelloWorldQueueMDB implements MessageListener {
private final static 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);
}
}
}
-------------------------------------------------------------------------------------------
ejb-jar.xml
-------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
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"
version="3.0">
<description>JBoss MDB</description>
<display-name>JBoss MDB</display-name>
<enterprise-beans>
<message-driven>
<ejb-name>HelloWorldQueue1MDB</ejb-name>
<ejb-class>org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB</ejb-class>
<!-- timeout-method>ejbTimeout</timeout-method> -->
<transaction-type>Container</transaction-type>
<message-destination-type>javax.jms.Queue</message-destination-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>queue/HELLOWORLDMDBQueue1</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
<message-driven>
<ejb-name>HelloWorldQueue2MDB</ejb-name>
<ejb-class>org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB</ejb-class>
<!-- timeout-method>ejbTimeout</timeout-method> -->
<transaction-type>Container</transaction-type>
<message-destination-type>javax.jms.Queue</message-destination-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>queue/HELLOWORLDMDBQueue2</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
</ejb-jar>
------------------------------------------------------------------------------------------
hornetq-jms.xml for deploying queues
------------------------------------------------------------------------------------------
<messaging-deployment xmlns="urn:jboss:messaging-deployment:1.0">
<hornetq-server>
<jms-destinations>
<jms-queue name="HELLOWORLDMDBQueue1">
<entry name="/queue/HELLOWORLDMDBQueue1"/>
</jms-queue>
<jms-queue name="HELLOWORLDMDBQueue2">
<entry name="/queue/HELLOWORLDMDBQueue2"/>
</jms-queue>
</jms-destinations>
</hornetq-server>
</messaging-deployment>
-----------------------------------------------------------------------------------
servlet which is inserting the messages
-----------------------------------------------------------------------------------
package org.jboss.as.quickstarts.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HelloWorldMDBServletClient")
public class HelloWorldMDBServletClient extends HttpServlet {
private static final long serialVersionUID = -8314035702649252239L;
private static final int MSG_COUNT = 5;
@Resource(mappedName = "java:/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue1")
private Queue queue1;
@Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue2")
private Queue queue2;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
Connection connection = null;
out.write("<h1>Quickstart: Example demonstrates the use of <strong>JMS 1.1</strong> and <strong>EJB 3.1 Message-Driven Bean</strong> in JBoss Enterprise Application 6 or JBoss AS 7.</h1>");
try {
Destination destination;
if (req.getParameterMap().keySet().contains("queue2")) {
destination = queue2;
} else {
destination = queue1;
}
out.write("<p>Sending messages to <em>" + destination + "</em></p>");
connection = connectionFactory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(destination);
connection.start();
out.write("<h2>Following messages will be send to the destination:</h2>");
TextMessage message = session.createTextMessage();
for (int i = 0; i < MSG_COUNT; i++) {
message.setText("This is message " + (i + 1));
messageProducer.send(message);
out.write("Message ("+i+"): " + message.getText() +"</br>");
}
out.write("<p><i>Go to your JBoss Application Server console or Server log to see the result of messages processing</i></p>");
} catch (JMSException e) {
e.printStackTrace();
out.write("<h2>A problem occurred during the delivery of this message</h2>");
out.write("</br>");
out.write("<p><i>Go your the JBoss Application Server console or Server log to see the error stack trace</i></p>");
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if(out != null) {
out.close();
}
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
-------------------------------------------------------------------
i am getting this error while deploying the jar
17:21:32,979 INFO [org.jboss.as.repository] (HttpManagementService-threads - 3) JBAS014900: Content added at location E:\Users\Jagdip\Downloads\jboss-as-7.1.1.Final\jboss-as-7.1.1.Final\standalone\data\content\59\7c927b0524bc9bbb6741a6bee3953c01483e31\content
17:22:24,804 INFO [org.jboss.as.server.deployment] (MSC service thread 1-10) JBAS015876: Starting deployment of "jboss-as-helloworld-mdb.war"
17:22:24,894 INFO [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-12) trying to deploy queue jms.queue.HELLOWORLDMDBQueue1
17:22:24,895 INFO [org.jboss.as.messaging] (MSC service thread 1-12) JBAS011601: Bound messaging object to jndi name java:/queue/HELLOWORLDMDBQueue1
17:22:24,895 INFO [org.jboss.as.ejb3] (MSC service thread 1-7) JBAS014142: Started message driven bean 'HelloWorldQueueMDB' with 'hornetq-ra' resource adapter
17:22:24,895 INFO [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-10) trying to deploy queue jms.queue.HELLOWORLDMDBQueue2
17:22:24,896 INFO [org.jboss.as.ejb3] (MSC service thread 1-5) JBAS014142: Started message driven bean 'HelloWorldQTopicMDB' with 'hornetq-ra' resource adapter
17:22:24,898 INFO [org.jboss.as.messaging] (MSC service thread 1-10) JBAS011601: Bound messaging object to jndi name java:/queue/HELLOWORLDMDBQueue2
17:22:24,900 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-13) MSC00001: Failed to start service jboss.deployment.unit."jboss-as-helloworld-mdb.war".component.HelloWorldQueueMDB.START: org.jboss.msc.service.StartException in service jboss.deployment.unit."jboss-as-helloworld-mdb.war".component.HelloWorldQueueMDB.START: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_55]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_55]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_55]
Caused by: java.lang.RuntimeException: javax.resource.spi.InvalidPropertyException: Destination is mandatory
at org.jboss.as.ejb3.component.messagedriven.MessageDrivenComponent.start(MessageDrivenComponent.java:171)
at org.jboss.as.ee.component.ComponentStartService.start(ComponentStartService.java:44)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
... 3 more
Caused by: javax.resource.spi.InvalidPropertyException: Destination is mandatory
at org.hornetq.ra.inflow.HornetQActivationSpec.validate(HornetQActivationSpec.java:643)
at org.jboss.jca.core.rar.EndpointImpl.activate(EndpointImpl.java:157)
at org.jboss.as.ejb3.component.messagedriven.MessageDrivenComponent.start(MessageDrivenComponent.java:169)
... 6 more
17:22:25,108 INFO [org.jboss.as.server] (HttpManagementService-threads - 3) JBAS015870: Deploy of deployment "jboss-as-helloworld-mdb.war" was rolled back with failure message {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"jboss-as-helloworld-mdb.war\".component.HelloWorldQueueMDB.START" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"jboss-as-helloworld-mdb.war\".component.HelloWorldQueueMDB.START: Failed to start service"}}
17:22:25,111 INFO [org.jboss.as.messaging] (MSC service thread 1-8) JBAS011605: Unbound messaging object to jndi name java:/queue/HELLOWORLDMDBQueue1
17:22:25,114 INFO [org.jboss.as.messaging] (MSC service thread 1-4) JBAS011605: Unbound messaging object to jndi name java:/queue/HELLOWORLDMDBQueue2
17:22:25,136 INFO [org.jboss.as.server.deployment] (MSC service thread 1-9) JBAS015877: Stopped deployment jboss-as-helloworld-mdb.war in 29ms
17:22:25,138 INFO [org.jboss.as.controller] (HttpManagementService-threads - 3) JBAS014774: Service status report
JBAS014777: Services which failed to start: service jboss.deployment.unit."jboss-as-helloworld-mdb.war".component.HelloWorldQueueMDB.START: org.jboss.msc.service.StartException in service jboss.deployment.unit."jboss-as-helloworld-mdb.war".component.HelloWorldQueueMDB.START: Failed to start service