diff --git a/helloworld-mdb/README.md b/helloworld-mdb/README.md index 8ba5817172..3fe0b7dcb8 100644 --- a/helloworld-mdb/README.md +++ b/helloworld-mdb/README.md @@ -1,17 +1,20 @@ helloworld-mdb: Helloword Using an MDB (Message-Driven Bean) ============================================================ -Author: Serge Pagop, Andy Taylor -Level: Intermediate -Technologies: JMS, EJB, MDB -Summary: Demonstrates the use of JMS 1.1 and EJB 3.1 Message-Driven Bean +Author: Serge Pagop, Andy Taylor, Jeff Mesnil +Level: Intermediate +Technologies: JMS, EJB, MDB +Summary: Demonstrates the use of JMS 1.1 and EJB 3.1 Message-Driven Bean Target Product: EAP What is it? ----------- -This example demonstrates the use of *JMS 1.1* and *EJB 3.1 Message-Driven Bean* in JBoss AS 7.1.0. +This example demonstrates the use of *JMS 1.1* and *EJB 3.1 Message-Driven Bean* in JBoss Enterprise Application Platform 6 or JBoss AS 7.1.0. -This project creates a queue named `HELLOWORLDMDBQueue` which is bound in JNDI as `java:/queue/HELLOWORLDMDBQueue`. +This project creates two JMS resources: + +* A queue named `HELLOWORLDMDBQueue` bound in JNDI as `java:/queue/HELLOWORLDMDBQueue` +* A topic named `HELLOWORLDMDBTopic` bound in JNDI as `java:/topic/HELLOWORLDMDBTopic` System requirements @@ -49,25 +52,32 @@ _NOTE: The following build command assumes you have configured your Maven user s mvn clean package jboss-as:deploy -4. This will deploy `target/jboss-as-helloworld-mdb.war` to the running instance of the server. +4. This will deploy `target/jboss-as-helloworld-mdb.war` to the running instance of the server. Look at the JBoss Application Server console or Server log and you should see log messages corresponding to the deployment of the message-driven beans and the JMS destinations: + 14:11:01,020 INFO org.hornetq.core.server.impl.HornetQServerImpl trying to deploy queue jms.queue.HELLOWORLDMDBQueue + 14:11:01,029 INFO org.jboss.as.messaging JBAS011601: Bound messaging object to jndi name java:/queue/HELLOWORLDMDBQueue + 14:11:01,030 INFO org.hornetq.core.server.impl.HornetQServerImpl trying to deploy queue jms.topic.HELLOWORLDMDBTopic + 14:11:01,060 INFO org.jboss.as.ejb3 JBAS014142: Started message driven bean 'HelloWorldQueueMDB' with 'hornetq-ra' resource adapter + 14:11:01,060 INFO org.jboss.as.ejb3 JBAS014142: Started message driven bean 'HelloWorldQTopicMDB' with 'hornetq-ra' resource adapter + 14:11:01,070 INFO org.jboss.as.messaging JBAS011601: Bound messaging object to jndi name java:/topic/HELLOWORLDMDBTopic Access the application --------------------- -The application will be running at the following URL: . +The application will be running at the following URL: and will send some messages to the queue. +To send messages to the topic, use the following URL: Investigate the Server Console Output ------------------------- Look at the JBoss Application Server console or Server log and you should see log messages like the following: - 15:42:35,453 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-47 (group:HornetQ-client-global-threads-1267410030)) Received Message: This is message 1 - 15:42:35,455 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-46 (group:HornetQ-client-global-threads-1267410030)) Received Message: This is message 2 - 15:42:35,457 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-50 (group:HornetQ-client-global-threads-1267410030)) Received Message: This is message 3 - 15:42:35,478 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-53 (group:HornetQ-client-global-threads-1267410030)) Received Message: This is message 5 - 15:42:35,481 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldMDB] (Thread-52 (group:HornetQ-client-global-threads-1267410030)) Received Message: This is message 4 + 17:51:52,122 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (Thread-1 (HornetQ-client-global-threads-26912020)) Received Message from queue: This is message 1 + 17:51:52,123 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (Thread-11 (HornetQ-client-global-threads-26912020)) Received Message from queue: This is message 2 + 17:51:52,124 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (Thread-12 (HornetQ-client-global-threads-26912020)) Received Message from queue: This is message 5 + 17:51:52,135 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (Thread-13 (HornetQ-client-global-threads-26912020)) Received Message from queue: This is message 4 + 17:51:52,136 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (Thread-14 (HornetQ-client-global-threads-26912020)) Received Message from queue: This is message 3 Undeploy the Archive @@ -137,71 +147,7 @@ Copy the source for the `helloworld-mdb` quickstart into this new git repository ### Configure the OpenShift Server -Next, you must enable HornetQ messaging provider. Open the `.openshift/config/standalone.xml` file (this file may be hidden) in an editor and make the following changes: - -1. If the following extension does not exist, add it under the `` element: - - -2. If the following `` elements are commented out or missing from the the `ejb3` subsytem, un-comment or add them: - - - - - -3. If the messaging subsystem is not already configured under the `` element, copy the following under the `` element to enable and configure HornetQ: - - - - true - 102400 - 2 - - - - - - - - - jms.queue.DLQ - jms.queue.ExpiryQueue - 0 - 20971520 - PAGE - 10 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - false - - +HornetQ is enabled by default in `.openshift/config/standalone.xml`. There is nothing to do to be able to send and receive messages from OpenShift. ### Deploy the OpenShift Application @@ -219,21 +165,22 @@ Note that the `openshift` profile in the `pom.xml` file is activated by OpenShif When the push command returns you can test the application by getting the following URL either via a browser or using tools such as curl or wget. Be sure to replace the `quickstart` in the URL with your domain name. -* +* to send messages to the queue +* 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 @@ + + +