此内容没有您所选择的语言版本。
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 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 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
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 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To build and deploy the quickstart
helloworld-mdb example, perform the following steps:
- Delete the following HornetQ XML configuration file from the
helloworld-mdbproject:helloworld-mdb/src/webapp/WEB-INF/hornetq-jms.xml
helloworld-mdb/src/webapp/WEB-INF/hornetq-jms.xmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Edit the annotations on the
HelloWorldQueueMDBmessage driven bean class, so that it integrates with the ActiveMQ resource adapter (instead of HornetQ). Edit theHelloWorldQueueMDB.javafile 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.javaCopy to Clipboard Copied! Toggle word wrap Toggle overflow Open theHelloWorldQueueMDB.javafile 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
@ResourceAdapterannotation 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
importstatement for the@ResourceAdapterannotation. - The value of the
destinationproperty 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 thedestinationvalue.
- Edit the annotations on the
HelloWorldTopicMDBmessage driven bean class, so that it integrates with the ActiveMQ resource adapter (instead of HornetQ). Edit theHelloWorldTopicMDB.javafile 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.javaCopy to Clipboard Copied! Toggle word wrap Toggle overflow Open theHelloWorldTopicMDB.javafile 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-apiartifact, which is needed for the@ResourceAdapterannotation. Open thehelloworld-mdb/pom.xmlfile using a text editor and add the followingdependencyelement as a child of thedependencieselement in the POM file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Theprovidedscope value implies there is no need to embed thejboss-ejb3-ext-apiJAR 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-apiartifact, you can discover which version to use by drilling down to theorg/jboss/ejb3/jboss-ejb3-ext-apisub-directory of the Maven repository you downloaded and installed in Section 1.4, “Install JBoss AS Quickstarts”. - Build the
helloworld-mdbexample as follows. Open a new command prompt, change directory to thehelloworld-mdbdirectory, and enter the following Maven command:mvn clean package
mvn clean packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow If the build is successful, you should find thejboss-as-helloworld-mdb.warWAR file in thehelloworld-mdb/targetdirectory. - 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.xmlconfiguration file with a text editor, and add the following highlighted administered objects to theactivemq-rar.rarresource 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.Queueobject that connects to theHELLOWORLDMDBQueueActiveMQ queue (where the queue name on the JBoss A-MQ broker is specified by thePhysicalNameconfig property). java:/queue/HELLOWORLDMDBTopic- References a
javax.jms.Topicobject that connects to theHELLOWORLDMDBTopicActiveMQ topic (where the topic name on the JBoss A-MQ broker is specified by thePhysicalNameconfig property).
In thehelloworld-mdbexample, these administered objects are accessed by the servlet class,HelloWorldMDBServletClient(but not by the message driven bean classes). For example, theHelloWorldMDBServletClientclass injects these JNDI entries, using the@Resourceannotation, as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Deploy the
helloworld-mdbexample to the running Web server. Manually copy thejboss-as-helloworld-mdb.warWAR file from thehelloworld-mdb/targetdirectory 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 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.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow - You should now be able to access the
helloworld-mdbservice from your browser, by navigating to the following URL:http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClient
http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClientCopy to Clipboard Copied! Toggle word wrap Toggle overflow When you navigate to the preceding URL, the servlet sends five messages to theHelloWorldQueueMDBmessage-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 1Copy to Clipboard Copied! Toggle word wrap Toggle overflow These console messages are written by theHelloWorldQueueMDBmessage-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
HelloWorldTopicMDBmessage-driven bean, navigate to the following URL:http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClient?topic
http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClient?topicCopy to Clipboard Copied! Toggle word wrap Toggle overflow When you navigate to the preceding URL, the servlet sends five messages to theHelloWorldTopicMDBmessage-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 5Copy to Clipboard Copied! Toggle word wrap Toggle overflow These console messages are written by theHelloWorldTopicMDBmessage-driven bean, thus demonstrating that the messages have successfully propagated from the servlet, throught the JBoss A-MQ broker, to the message-driven bean.