📄 jmsj2eeex2.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>A J2EE Application that Uses the JMS API with a Session Bean</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="JMSJ2EEex.html" /> <link rel="Next" href="JMSJ2EEex3.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="JMSJ2EEex.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JMSJ2EEex3.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp95588"> </a><h2 class="pHeading1">A J2EE Application that Uses the JMS API with a Session Bean</h2><a name="wp95594"> </a><p class="pBody">This section explains how to write, compile, package, deploy, and run a J2EE application that uses the JMS API in conjunction with a session bean. The application contains the following components:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79150"> </a><div class="pSmartList1"><li>An application client that invokes an enterprise bean</li></div><a name="wp79151"> </a><div class="pSmartList1"><li>A session bean that publishes several messages to a topic</li></div><a name="wp79152"> </a><div class="pSmartList1"><li>A message-driven bean that receives and processes the messages, using a durable topic subscriber and a message selector</li></div></ul></div><a name="wp79153"> </a><p class="pBody">The section covers the following topics:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79154"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp79160">Writing the Application Components</a></li></div><a name="wp79155"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp89357">Creating and Packaging the Application</a></li></div><a name="wp89985"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp89818">Deploying the Application</a></li></div><a name="wp89990"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp89825">Running the Application Client</a></li></div></ul></div><a name="wp79157"> </a><p class="pBody">You will find the source files for this section in the directory <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/jms/clientsessionmdb/</code>. Path names in this section are relative to this directory.</p><a name="wp79160"> </a><h3 class="pHeading2">Writing the Application Components</h3><a name="wp79161"> </a><p class="pBody">This application demonstrates how to send messages from an enterprise bean--in this case, a session bean--rather than from an application client, as in the example in Chapter <a href="MDB.html#wp79663">23</a>. <a href="JMSJ2EEex2.html#wp79173">Figure 30-1</a> illustrates the structure of this application.</p><a name="wp79171"> </a><p class="pBody"></p><div align="left"><img src="images/JMS113.gif" height="212" width="401" alt="A J2EE Application: Client to Session Bean to Message-Driven Bean" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="79173"> </a><strong><font >Figure 30-1 A J2EE Application: Client to Session Bean to Message-Driven Bean</font></strong></p><a name="wp79174"> </a><p class="pBody">The Publisher enterprise bean in this example is the enterprise-application equivalent of a wire-service news feed that categorizes news events into six news categories. The message-driven bean could represent a newsroom, where the Sports desk, for example, would set up a subscription for all news events pertaining to sports. </p><a name="wp79175"> </a><p class="pBody">The application client in the example obtains a handle to the Publisher enterprise bean's remote home interface and calls the enterprise bean's business method. The enterprise bean creates 18 text messages. For each message, it sets a <code class="cCode">String</code> property randomly to one of six values representing the news categories and then publishes the message to a topic. The message-driven bean uses a message selector for the property to limit which of the published messages it receives. </p><a name="wp79176"> </a><p class="pBody">Writing the components of the application involves</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79177"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp79184">Coding the Application Client: MyAppClient.java</a></li></div><a name="wp79178"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp79242">Coding the Publisher Session Bean</a></li></div><a name="wp79179"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp79421">Coding the Message-Driven Bean: MessageBean.java</a></li></div></ul></div><a name="wp79184"> </a><h4 class="pHeading3">Coding the Application Client: MyAppClient.java</h4><a name="wp79185"> </a><p class="pBody">The application client program, <code class="cCode"><a href="../examples/jms/clientsessionmdb/src/MyAppClient.java" target="_blank">src/MyAppClient.java,</a></code> performs no JMS API operations and so is simpler than the client program in Chapter <a href="MDB.html#wp79663">23</a>. The program obtains a handle to the Publisher enterprise bean's remote home interface, using the JNDI naming context <code class="cCode">java:comp/env</code>. The program then creates an instance of the bean and calls the bean's business method twice.</p><a name="wp79242"> </a><h4 class="pHeading3">Coding the Publisher Session Bean</h4><a name="wp79243"> </a><p class="pBody">The Publisher bean is a stateless session bean with one <code class="cCode">create</code> method and one business method. The Publisher bean uses remote interfaces rather than local interfaces because it is accessed from outside the EJB container.</p><a name="wp79246"> </a><p class="pBody">The remote home interface source file is <code class="cCode"><a href="../examples/jms/clientsessionmdb/src/PublisherHome.java" target="_blank">src/PublisherHome.java</a></code>.</p><a name="wp79262"> </a><p class="pBody">The remote interface, <code class="cCode"><a href="../examples/jms/clientsessionmdb/src/PublisherRemote.java" target="_blank">src/PublisherRemote.java</a></code>, declares a single business method, <code class="cCode">publishNews</code>.</p><a name="wp79279"> </a><p class="pBody">The bean class, <code class="cCode"><a href="../examples/jms/clientsessionmdb/src/PublisherBean.java" target="_blank">src/PublisherBean.java,</a></code> implements the <code class="cCode">publishNews</code> method and its helper method <code class="cCode">chooseType</code>. The bean class also implements the required methods <code class="cCode">ejbCreate</code>, <code class="cCode">setSessionContext</code>, <code class="cCode">ejbRemove</code>, <code class="cCode">ejbActivate</code>, and <code class="cCode">ejbPassivate</code>.</p><a name="wp79281"> </a><p class="pBody">The <code class="cCode">ejbCreate</code> method of the bean class allocates resources--in this case, by looking up the <code class="cCode">ConnectionFactory</code> and the topic and creating the <code class="cCode">Connection</code>. The business method <code class="cCode">publishNews</code> creates a <code class="cCode">Session</code> and a <code class="cCode">MessageProducer</code> and publishes the messages.</p><a name="wp79282"> </a><p class="pBody">The <code class="cCode">ejbRemove</code> method must deallocate the resources that were allocated by the <code class="cCode">ejbCreate</code> method. In this case, the <code class="cCode">ejbRemove</code> method closes the <code class="cCode">Connection</code>.</p><a name="wp79421"> </a><h4 class="pHeading3">Coding the Message-Driven Bean: MessageBean.java</h4><a name="wp79422"> </a><p class="pBody">The message-driven bean class, <code class="cCode"><a href="../examples/jms/clientsessionmdb/src/MessageBean.java" target="_blank">src/MessageBean.java</a></code>, is identical to the one in Chapter <a href="MDB.html#wp79663">23</a>. However, the deployment descriptor will be different, because the bean is using a topic with a durable subscription, instead of a queue. </p><a name="wp89357"> </a><h3 class="pHeading2">Creating and Packaging the Application</h3><a name="wp89393"> </a><p class="pBody">This example uses the topic named <code class="cCode">jms/Topic</code> and the connection factory <code class="cCode">jms/TopicConnectionFactory,</code> which you created in <a href="JMS5.html#wp80290">Creating JMS Administered Objects</a>. It also uses the connection factory <code class="cCode">jms/DurableTopicConnectionFactory</code>, which you created in <a href="JMS6.html#wp83602">A Message Acknowledgment Example</a> and <a href="JMS6.html#wp83706">A Durable Subscription Example</a>. If you deleted any of these objects, create them again.</p><a name="wp89362"> </a><p class="pBody">Creating and packaging this application involve several steps: </p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp89366"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp89384">Compiling the Source Files and Starting the J2EE Application Server</a></li></div><a name="wp89370"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp89411">Starting deploytool and Creating the Application</a></li></div><a name="wp90001"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp89353">Packaging the Session Bean</a></li></div><a name="wp89378"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp89520">Packaging the Message-Driven Bean</a></li></div><a name="wp92773"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp92743">Packaging the Application Client</a></li></div><a name="wp89382"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex2.html#wp89717">Updating the JNDI Names</a></li></div></ol></div><a name="wp89384"> </a><h4 class="pHeading3">Compiling the Source Files and Starting the J2EE Application Server</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp94373"> </a><div class="pSmartList1"><li>In the directory <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/jms/clientsessionmdb</code>, use the <code class="cCode">build</code> target to compile the source files:</li></div><a name="wp89386"> </a><p class="pBodyRelative"><code class="cCode">asant build</code></p><a name="wp89387"> </a><div class="pSmartList1"><li>Start the J2EE Application Server, if it is not already running.</li></div></ol></div><a name="wp89411"> </a><h4 class="pHeading3">Starting deploytool and Creating the Application</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp89412"> </a><div class="pSmartList1"><li>Start <code class="cCode">deploytool</code>.</li></div><a name="wp89414"> </a><div class="pSmartList1"><li>Choose File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>New<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Application.</li></div><a name="wp89415"> </a><div class="pSmartList1"><li>Click Browse next to the Application File Name field and use the file chooser to locate the directory <code class="cCode">clientsessionmdb</code>. </li></div><a name="wp89416"> </a><div class="pSmartList1"><li>In the File Name field, type <code class="cCode">ClientSessionMDBApp</code>.</li></div><a name="wp89417"> </a><div class="pSmartList1"><li>Click New Application.</li></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -