1.5. Build and Deploy the helloworld-mdb Example
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
In this tutorial, you will customize the
helloworld-mdb
quickstart example so that it works with the ActiveMQ resource adapter. You can then build and deploy the helloworld-mdb
example into a standalone instance of JBoss Enterprise Application Platform (which already has an ActiveMQ resource adapter installed).
The
helloworld-mdb
example illustrates two kinds of integration with messaging: integration of message-driven beans; and integration of a servlet (which gets access to a JMS queue and a JMS topic using the standard JMS administered objects mechanism).
Prerequisites Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following prerequisites must be satisfied, before you can build and deploy the
helloworld-mdb
example:
- The ActiveMQ resource adapter is installed in the JBoss Enterprise Application Platform (as described in Section 1.2, “Install the ActiveMQ Resource Adapter”), and the installation has been verified.
- The JBoss EAP Maven repository and the JBoss AS Quickstart examples have been installed (as described in Section 1.4, “Install JBoss AS Quickstarts”).
- You have Internet access (for the Maven build).
Customizations Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The version of the
helloworld-mdb
demonstration provided in the quickstarts distribution is integrated with the HornetQ messaging platform by default. To refactor the demonstration so that it integrates with Apache ActiveMQ, it is necessary to customize the following aspects of the helloworld-mdb
code:
- Delete the HornetQ XML configuration file (located in
helloworld-mdb/src/webapp/WEB-INF/hornetq-jms.xml
). - In
HelloWorldQueueMDB.java
, customize the annotations on the message driven bean to integrate with the ActiveMQ resource adapter. - In
HelloWorldTopicMDB.java
, customize the annotations on the message driven bean to integrate with the ActiveMQ resource adapter. - Add additional Maven dependencies.
These customizations are described in more detail in the rest of this section.
Steps to build and deploy the example Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To build and deploy the quickstart
helloworld-mdb
example, perform the following steps:
- Delete the following HornetQ XML configuration file from the
helloworld-mdb
project:helloworld-mdb/src/webapp/WEB-INF/hornetq-jms.xml
helloworld-mdb/src/webapp/WEB-INF/hornetq-jms.xml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Edit the annotations on the
HelloWorldQueueMDB
message driven bean class, so that it integrates with the ActiveMQ resource adapter (instead of HornetQ). Edit theHelloWorldQueueMDB.java
file at the following location:helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldQueueMDB.java
helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldQueueMDB.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Open theHelloWorldQueueMDB.java
file using a text editor and make the modifications highlighted in the following extract:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where the following changes are made to the code:- The
@ResourceAdapter
annotation explicitly associates the message driven bean with the ActiveMQ resource adapter. You must include this annotation, if you want to use the ActiveMQ resource adapter. - You need to add an
import
statement for the@ResourceAdapter
annotation. - The value of the
destination
property is changed toHELLOWORLDMDBQueue
, which is the physical name of the corresponding ActiveMQ queue that this message driven bean subscribes to. The physical name of the queue is the queue identifier used by the JBoss A-MQ broker.NoteYou must specify the queue's physical name here. In contrast to the case of HornetQ, the ActiveMQ messaging integration does not allow you to use a JNDI name for thedestination
value.
- Edit the annotations on the
HelloWorldTopicMDB
message driven bean class, so that it integrates with the ActiveMQ resource adapter (instead of HornetQ). Edit theHelloWorldTopicMDB.java
file at the following location:helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldTopicMDB.java
helloworld-mdb/src/main/java/org/jboss/as/quickstarts/mdb/HelloWorldTopicMDB.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Open theHelloWorldTopicMDB.java
file using a text editor and make the modifications highlighted in the following extract:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the Maven dependency for the
jboss-ejb3-ext-api
artifact, which is needed for the@ResourceAdapter
annotation. Open thehelloworld-mdb/pom.xml
file using a text editor and add the followingdependency
element as a child of thedependencies
element in the POM file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Theprovided
scope value implies there is no need to embed thejboss-ejb3-ext-api
JAR file in the generated WAR, because this library is already provided by the JBoss Enterprise Application Platform container.NoteIf you ever need to update the version of thejboss-ejb3-ext-api
artifact, you can discover which version to use by drilling down to theorg/jboss/ejb3/jboss-ejb3-ext-api
sub-directory of the Maven repository you downloaded and installed in Section 1.4, “Install JBoss AS Quickstarts”. - Build the
helloworld-mdb
example as follows. Open a new command prompt, change directory to thehelloworld-mdb
directory, and enter the following Maven command:mvn clean package
mvn clean package
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the build is successful, you should find thejboss-as-helloworld-mdb.war
WAR file in thehelloworld-mdb/target
directory. - If you have not already done so, register the administered objects for the queues and topics used by the example, by editing the JBoss Enterprise Application Platform configuration.In your JBoss Enterprise Application Platform installation, open the
standalone/configuration/standalone.xml
configuration file with a text editor, and add the following highlighted administered objects to theactivemq-rar.rar
resource adapter:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where the preceding configuration adds the following entries to the JNDI registry:java:/queue/HELLOWORLDMDBQueue
- References a
javax.jms.Queue
object that connects to theHELLOWORLDMDBQueue
ActiveMQ queue (where the queue name on the JBoss A-MQ broker is specified by thePhysicalName
config property). java:/queue/HELLOWORLDMDBTopic
- References a
javax.jms.Topic
object that connects to theHELLOWORLDMDBTopic
ActiveMQ topic (where the topic name on the JBoss A-MQ broker is specified by thePhysicalName
config property).
In thehelloworld-mdb
example, these administered objects are accessed by the servlet class,HelloWorldMDBServletClient
(but not by the message driven bean classes). For example, theHelloWorldMDBServletClient
class injects these JNDI entries, using the@Resource
annotation, as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Deploy the
helloworld-mdb
example to the running Web server. Manually copy thejboss-as-helloworld-mdb.war
WAR file from thehelloworld-mdb/target
directory to the Web server's deployment directory,standalone/deployments
.
Access the helloworld-mdb service Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can now test the
helloworld-mdb
service, as follows:
- If you have not already started the JBoss Enterprise Application Platform standalone container, do so by entering the following commands at the command line:
cd EAPInstallDir/bin ./standalone.sh
cd EAPInstallDir/bin ./standalone.sh
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - You should now be able to access the
helloworld-mdb
service from your browser, by navigating to the following URL:http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClient
http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClient
Copy to Clipboard Copied! Toggle word wrap Toggle overflow When you navigate to the preceding URL, the servlet sends five messages to theHelloWorldQueueMDB
message-driven bean. If you look at the container console window, you should see some output like the following:14:41:20,739 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 7) Received Message from queue: This is message 5 14:41:20,739 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 5) Received Message from queue: This is message 3 14:41:20,739 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 4) Received Message from queue: This is message 2 14:41:20,741 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 6) Received Message from queue: This is message 4 14:41:20,742 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 3) Received Message from queue: This is message 1
14:41:20,739 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 7) Received Message from queue: This is message 5 14:41:20,739 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 5) Received Message from queue: This is message 3 14:41:20,739 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 4) Received Message from queue: This is message 2 14:41:20,741 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 6) Received Message from queue: This is message 4 14:41:20,742 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldQueueMDB] (default-threads - 3) Received Message from queue: This is message 1
Copy to Clipboard Copied! Toggle word wrap Toggle overflow These console messages are written by theHelloWorldQueueMDB
message-driven bean, thus demonstrating that the messages have successfully propagated from the servlet, through the JBoss A-MQ broker, to the message-driven bean. - To send messages to the
HelloWorldTopicMDB
message-driven bean, navigate to the following URL:http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClient?topic
http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClient?topic
Copy to Clipboard Copied! Toggle word wrap Toggle overflow When you navigate to the preceding URL, the servlet sends five messages to theHelloWorldTopicMDB
message-driven bean. If you look at the container console window, you should see some output like the following:14:53:02,464 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 9) Received Message from topic: This is message 2 14:53:02,466 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 10) Received Message from topic: This is message 3 14:53:02,468 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 8) Received Message from topic: This is message 1 14:53:02,471 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 11) Received Message from topic: This is message 4 14:53:02,472 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 12) Received Message from topic: This is message 5
14:53:02,464 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 9) Received Message from topic: This is message 2 14:53:02,466 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 10) Received Message from topic: This is message 3 14:53:02,468 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 8) Received Message from topic: This is message 1 14:53:02,471 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 11) Received Message from topic: This is message 4 14:53:02,472 INFO [class org.jboss.as.quickstarts.mdb.HelloWorldTopicMDB] (default-threads - 12) Received Message from topic: This is message 5
Copy to Clipboard Copied! Toggle word wrap Toggle overflow These console messages are written by theHelloWorldTopicMDB
message-driven bean, thus demonstrating that the messages have successfully propagated from the servlet, throught the JBoss A-MQ broker, to the message-driven bean.