-
Language:
English
-
Language:
English
1.5. Build and Deploy the helloworld-mdb Example
Overview
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
The following prerequisites must be satisfied, before you can build and deploy the
helloworld-mdb
example:
- The ActiveMQ resource adapter is installed in JBoss EAP (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
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/main/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
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/main/webapp/WEB-INF/hornetq-jms.xml
- 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
Open theHelloWorldQueueMDB.java
file using a text editor and make the modifications highlighted in the following extract:import org.jboss.ejb3.annotation.ResourceAdapter; ... @MessageDriven(name = "HelloWorldQueueMDB", activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "HELLOWORLDMDBQueue"), @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") }) @ResourceAdapter(value="activemq-rar.rar") public class HelloWorldQueueMDB implements MessageListener { ...
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
Open theHelloWorldTopicMDB.java
file using a text editor and make the modifications highlighted in the following extract:import org.jboss.ejb3.annotation.ResourceAdapter; ... @MessageDriven(name = "HelloWorldQTopicMDB", activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "HELLOWORLDMDBTopic"), @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") }) @ResourceAdapter(value="activemq-rar.rar") public class HelloWorldTopicMDB implements MessageListener { ...
- 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:<project ...> ... <dependencies> ... <dependency> <groupId>org.jboss.ejb3</groupId> <artifactId>jboss-ejb3-ext-api</artifactId> <!-- to get the right version, look in the EAP offline Maven repo --> <version>2.1.0.redhat-1</version> <scope>provided</scope> </dependency> </dependencies> ... </project>
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 EAP 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
subdirectory 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
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 EAP configuration.In your JBoss EAP installation, open the
standalone/configuration/standalone-full.xml
configuration file with a text editor, and add the following highlighted administered objects to theactivemq-rar.rar
resource adapter:<server xmlns="urn:jboss:domain:1.4"> ... <profile> ... <subsystem xmlns="urn:jboss:domain:resource-adapters:1.1"> <resource-adapters> <resource-adapter id="activemq-rar.rar"> ... <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> ... </profile> ... </server>
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:import javax.annotation.Resource; ... import javax.jms.ConnectionFactory; ... import javax.jms.Queue; import javax.jms.Topic; ... public class HelloWorldMDBServletClient extends HttpServlet { ... @Resource(mappedName = "java:/ConnectionFactory") private ConnectionFactory connectionFactory; @Resource(mappedName = "java:/queue/HELLOWORLDMDBQueue") private Queue queue; @Resource(mappedName = "java:/topic/HELLOWORLDMDBTopic") private Topic topic; ...
- 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
You can now test the
helloworld-mdb
service, as follows:
- If you have not already started the JBoss EAP standalone container, do so by entering the following commands at the command line:
cd EAPInstallDir/bin ./standalone.sh
- You should now be able to access the
helloworld-mdb
service from your browser, by navigating to the following URL:http://localhost:8080/jboss-helloworld-mdb/HelloWorldMDBServletClient
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
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-helloworld-mdb/HelloWorldMDBServletClient?topic
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
These console messages are written by theHelloWorldTopicMDB
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.