to send messages to the topic
If the application has run succesfully you should see some output in the browser.
-now you can look at the output of the server by running the following command:
+Now you can look at the output of the server by running the following command:
rhc app status -a helloworldmdb
This will show the tail of the servers log which should show something like the following.
- 2012/03/02 05:52:33,065 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-0 (HornetQ-client-global-threads-1772719)) Received Message: This is message 4
- 2012/03/02 05:52:33,065 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-1 (HornetQ-client-global-threads-1772719)) Received Message: This is message 1
- 2012/03/02 05:52:33,067 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-6 (HornetQ-client-global-threads-1772719)) Received Message: This is message 5
- 2012/03/02 05:52:33,065 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-3 (HornetQ-client-global-threads-1772719)) Received Message: This is message 3
- 2012/03/02 05:52:33,065 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-2 (HornetQ-client-global-threads-1772719)) Received Message: This is message 2
+ 2012/03/02 05:52:33,065 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-0 (HornetQ-client-global-threads-1772719)) Received Message from queue: This is message 4
+ 2012/03/02 05:52:33,065 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-1 (HornetQ-client-global-threads-1772719)) Received Message from queue: This is message 1
+ 2012/03/02 05:52:33,067 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-6 (HornetQ-client-global-threads-1772719)) Received Message from queue: This is message 5
+ 2012/03/02 05:52:33,065 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-3 (HornetQ-client-global-threads-1772719)) Received Message from queue: This is message 3
+ 2012/03/02 05:52:33,065 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-2 (HornetQ-client-global-threads-1772719)) Received Message from queue: This is message 2
You can use the OpenShift command line tools or the OpenShift web console to discover and control the application.
diff --git a/helloworld-mdb/cheatsheets/helloworld-mdb.xml b/helloworld-mdb/cheatsheets/helloworld-mdb.xml
index e4f4bef083..7ba8d47058 100644
--- a/helloworld-mdb/cheatsheets/helloworld-mdb.xml
+++ b/helloworld-mdb/cheatsheets/helloworld-mdb.xml
@@ -1,71 +1,79 @@
-
-
-
-
- This quickstart shows you how to deploy a simple EJB 3.1 Message-Driven Bean to JBoss AS. We use here a Servlet 3.0 as Client to send several messages to the queue.
-
- EJB 3.1 Message-Driven Bean
-
-Message-Driven Bean (MDB) is an enterprise bean that can be used in an Java EE 6 application to process asynchronously messages coming from a specific queue. it acts as a JMS message Listener.
-
-
-
-
- -
-
- The helloworld mdb is very simple - all it does is send several messages by a sevlet 3.0 component.
-The helloworld mdb example is comprised a servlet 3.0 as client HelloWorldMDBServletClient with a context @WebServlet("/HelloWorldMDBServletClient") that send several messages to the queue "queue/HELLOWORLDMDBQueue" and a message-driven bean, that processes asynchronously messages from the specific queue.
-The servlet client injects a connection factory @@Resource(mappedName = "java:/ConnectionFactory") private static ConnectionFactory connectionFactory; to create a connection and a queue @Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue") private static Queue queue; where messages need to be send.
-The message-driven bean class HelloWorldMDB achieves the requirements of a typical JMS listener through it annotation with @MessageDriven(name = "HelloWorldMDB", activationConfig = {
- @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
- @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/HELLOWORLDMDBQueue"),
- @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
-The message-driven bean processes the messages and print and print (Thread-3 (group:HornetQ-client-global-threads-343422187)) Received Message: This is message 3
(Thread-4 (group:HornetQ-client-global-threads-343422187)) Received Message: This is message 4
(Thread-2 (group:HornetQ-client-global-threads-343422187)) Received Message: This is message 2
(Thread-0 (group:HornetQ-client-global-threads-343422187)) Received Message: This is message 0
on the log console.
-For sending message uses this url http://localhost:8080//jboss-as-helloworld-mdb/HelloWorldMDBServletClient.
-
-
- -
-
- Hello World MDB Servlet Client- Servlet 3.0
-
-
-
-
-
-
-
-
- -
-
- Right-click the jboss-as-helloworld-mdb project and select Run As > Run On Server or click on the "Click to Perform" link below.
-
-
-
-
+
+
+ This quickstart shows you how to deploy a simple EJB 3.1 Message-Driven Bean to JBoss AS. We use here a Servlet 3.0 as Client to send several messages to the queue.
+
+ EJB 3.1 Message-Driven Bean
+
+Message-Driven Bean (MDB) is an enterprise bean that can be used in an Java EE 6 application to process asynchronously messages coming from a specific queue. it acts as a JMS message Listener.
+
+
+
+
+ -
+
+ The helloworld mdb is very simple - all it does is send several messages by a sevlet 3.0 component.
+The helloworld mdb example is comprised a servlet 3.0 as client HelloWorldMDBServletClient with a context @WebServlet("/HelloWorldMDBServletClient") that send several messages to a queue "queue/HELLOWORLDMDBQueue" or a topic "topic/HELLOWORLDMDBTopic" and a message-driven bean, that processes asynchronously messages from the specific queue.
+The servlet client injects a connection factory @@Resource(mappedName = "java:/ConnectionFactory") private ConnectionFactory connectionFactory; to create a connection and a queue @Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue") private Queue queue; and a topic @Resource(mappedName = "java:/topic/HELLOWORLDMDBTopic") private Topic topic;where messages need to be send.
+The message-driven bean class HelloWorldMDB achieves the requirements of a typical JMS listener through it annotation with @MessageDriven(name = "HelloWorldMDB", activationConfig = {
+ @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
+ @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/HELLOWORLDMDBQueue"),
+ @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
+The message-driven bean processes the messages and print and print (Thread-3 (group:HornetQ-client-global-threads-343422187)) Received Message from queue: This is message 3
(Thread-4 (group:HornetQ-client-global-threads-343422187)) Received Message from queue: This is message 4
(Thread-2 (group:HornetQ-client-global-threads-343422187)) Received Message from queue: This is message 2
(Thread-0 (group:HornetQ-client-global-threads-343422187)) Received Message from queue: This is message 0
on the log console.
+For sending message uses this url http://localhost:8080//jboss-as-helloworld-mdb/HelloWorldMDBServletClient.
+
+
+ -
+
+ Hello World MDB Servlet Client- Servlet 3.0
+
+
+
+
+
+
+
+
+
+
+
+ -
+
+ Right-click the jboss-as-helloworld-mdb project and select Run As > Run On Server or click on the "Click to Perform" link below.
+
+
+
+
diff --git a/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldMDB.java b/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldQueueMDB.java
similarity index 90%
rename from helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldMDB.java
rename to helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldQueueMDB.java
index 07e0f8fe8b..db676d0655 100644
--- a/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldMDB.java
+++ b/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldQueueMDB.java
@@ -34,13 +34,13 @@
* @author Serge Pagop (spagop@redhat.com)
*
*/
-@MessageDriven(name = "HelloWorldMDB", activationConfig = {
+@MessageDriven(name = "HelloWorldQueueMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/HELLOWORLDMDBQueue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
-public class HelloWorldMDB implements MessageListener {
+public class HelloWorldQueueMDB implements MessageListener {
- private final static Logger LOGGER = Logger.getLogger(HelloWorldMDB.class
+ private final static Logger LOGGER = Logger.getLogger(HelloWorldQueueMDB.class
.toString());
/**
@@ -51,7 +51,7 @@ public void onMessage(Message rcvMessage) {
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
- LOGGER.info("Received Message: " + msg.getText());
+ LOGGER.info("Received Message from queue: " + msg.getText());
} else {
LOGGER.warning("Message of wrong type: "
+ rcvMessage.getClass().getName());
diff --git a/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldTopicMDB.java b/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldTopicMDB.java
new file mode 100644
index 0000000000..8d444b5e99
--- /dev/null
+++ b/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldTopicMDB.java
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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;
+
+
+/**
+ *
+ * A simple Message Driven Bean that asynchronously receives and processes the
+ * messages that are sent to the topic.
+ *
+ *
+ * @author Serge Pagop (spagop@redhat.com)
+ *
+ */
+@MessageDriven(name = "HelloWorldQTopicMDB", activationConfig = {
+ @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
+ @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/HELLOWORLDMDBTopic"),
+ @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
+public class HelloWorldTopicMDB implements MessageListener {
+
+ private final static Logger LOGGER = Logger.getLogger(HelloWorldTopicMDB.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 topic: " + msg.getText());
+ } else {
+ LOGGER.warning("Message of wrong type: "
+ + rcvMessage.getClass().getName());
+ }
+ } catch (JMSException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/servlet/HelloWorldMDBServletClient.java b/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/servlet/HelloWorldMDBServletClient.java
index 862270c834..bd13e76c7c 100644
--- a/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/servlet/HelloWorldMDBServletClient.java
+++ b/helloworld-mdb/src/main/java/org/jboss/as/quickstarts/servlet/HelloWorldMDBServletClient.java
@@ -22,11 +22,13 @@
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;
@@ -35,7 +37,7 @@
/**
*
- * A simple servlet 3 as client that sends several messages to a queue.
+ * A simple servlet 3 as client that sends several messages to a queue or a topic.
*
*
*
@@ -59,20 +61,30 @@ public class HelloWorldMDBServletClient extends HttpServlet {
@Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue")
private Queue queue;
+ @Resource(mappedName = "java:/topic/HELLOWORLDMDBTopic")
+ private Topic topic;
+
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
Connection connection = null;
- out.write("
Quickstart: Example demonstrates the use of *JMS 1.1* and *EJB 3.1 Message-Driven Bean* in JBoss AS 7.1.0.
");
+ out.write("Quickstart: Example demonstrates the use of JMS 1.1 and EJB 3.1 Message-Driven Bean in JBoss Enterprise Application 6 or JBoss AS 7.1.0.
");
try {
+ Destination destination;
+ if (req.getParameterMap().keySet().contains("topic")) {
+ destination = topic;
+ } else {
+ destination = queue;
+ }
+ out.write("Sending messages to " + destination + "
");
connection = connectionFactory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
- MessageProducer messageProducer = session.createProducer(queue);
+ MessageProducer messageProducer = session.createProducer(destination);
connection.start();
- out.write("Following messages will be send to the queue:
");
+ out.write("Following messages will be send to the destination:
");
TextMessage message = session.createTextMessage();
for (int i = 0; i < MSG_COUNT; i++) {
message.setText("This is message " + (i + 1));
diff --git a/helloworld-mdb/src/main/webapp/WEB-INF/hornetq-jms.xml b/helloworld-mdb/src/main/webapp/WEB-INF/hornetq-jms.xml
index 166bdf3bf6..cba932a1d3 100644
--- a/helloworld-mdb/src/main/webapp/WEB-INF/hornetq-jms.xml
+++ b/helloworld-mdb/src/main/webapp/WEB-INF/hornetq-jms.xml
@@ -15,6 +15,9 @@
+
+
+