📄 mdb4.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>The Message-Driven Bean Class</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="MDB3.html" /> <link rel="Next" href="MDB5.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="MDB3.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="MDB5.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="wp79739"> </a><h2 class="pHeading1">The Message-Driven Bean Class</h2><a name="wp79741"> </a><p class="pBody">The code for the <code class="cCode">SimpleMessageEJB</code> class illustrates the requirements of a message-driven bean class:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79742"> </a><div class="pSmartList1"><li>It implements the <code class="cCode">MessageDrivenBean</code> and <code class="cCode">MessageListener</code> interfaces.</li></div><a name="wp79743"> </a><div class="pSmartList1"><li>The class is defined as <code class="cCode">public</code>.</li></div><a name="wp79744"> </a><div class="pSmartList1"><li>The class cannot be defined as <code class="cCode">abstract</code> or <code class="cCode">final</code>.</li></div><a name="wp79745"> </a><div class="pSmartList1"><li>It implements one <code class="cCode">onMessage</code> method.</li></div><a name="wp79746"> </a><div class="pSmartList1"><li>It implements one <code class="cCode">ejbCreate</code> method and one <code class="cCode">ejbRemove</code> method.</li></div><a name="wp79747"> </a><div class="pSmartList1"><li>It contains a public constructor with no arguments.</li></div><a name="wp79748"> </a><div class="pSmartList1"><li>It must not define the <code class="cCode">finalize</code> method.</li></div></ul></div><a name="wp79749"> </a><p class="pBody">Unlike session and entity beans, message-driven beans do not have the remote or local interfaces that define client access. Client components do not locate message-driven beans and invoke methods on them. Although message-driven beans do not have business methods, they may contain helper methods that are invoked internally by the <code class="cCode">onMessage</code> method.</p><a name="wp79752"> </a><h3 class="pHeading2">The onMessage Method</h3><a name="wp79755"> </a><p class="pBody">When the queue receives a message, the EJB container invokes the <code class="cCode">onMessage</code> method of the message-driven bean. </p><a name="wp80617"> </a><p class="pBody">The <code class="cCode">onMessage</code> method is called by the bean's container when a message has arrived for the bean to service. This method contains the business logic that handles the processing of the message. It is the message-driven bean's responsibility to parse the message and perform the necessary business logic. </p><a name="wp80743"> </a><p class="pBody">The <code class="cCode">onMessage</code> method has a single argument: the incoming message. </p><a name="wp80621"> </a><p class="pBody">The message-driven bean class defines one <code class="cCode">onMessage</code> method, whose signature must follow these rules: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80623"> </a><div class="pSmartList1"><li>The method must be declared as <code class="cCode">public</code> and must not be declared as <code class="cCode">final</code> or <code class="cCode">static</code>. </li></div><a name="wp80624"> </a><div class="pSmartList1"><li>The return type must be <code class="cCode">void</code>. </li></div><a name="wp80625"> </a><div class="pSmartList1"><li>The method must have a single argument of type <code class="cCode">javax.jms.Message</code>. </li></div><a name="wp80626"> </a><div class="pSmartList1"><li>The <code class="cCode">throws</code> clause must not define any application exceptions.</li></div><a name="wp80629"> </a><div class="pSmartList1"><li>The <code class="cCode">onMessage</code> method is invoked in the scope of a transaction that is determined by the transaction attribute specified in the deployment descriptor. </li></div></ul></div><a name="wp80612"> </a><p class="pBody">In the <code class="cCode">SimpleMessageBean</code> class, the <code class="cCode">onMessage</code> method casts the incoming message to a <code class="cCode">TextMessage</code> and displays the text:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void onMessage(Message inMessage) { TextMessage msg = null; try { if (inMessage instanceof TextMessage) { msg = (TextMessage) inMessage; System.out.println ("MESSAGE BEAN: Message received: " + msg.getText()); } else { System.out.println ("Message of wrong type: " + inMessage.getClass().getName()); } } catch (JMSException e) { e.printStackTrace(); mdc.setRollbackOnly(); } catch (Throwable te) { te.printStackTrace(); }}<a name="wp81714"> </a></pre></div><a name="wp79759"> </a><h3 class="pHeading2">The ejbCreate and ejbRemove Methods</h3><a name="wp79760"> </a><p class="pBody">The signatures of these methods have the following requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79761"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79762"> </a><div class="pSmartList1"><li>The return type must be <code class="cCode">void</code>.</li></div><a name="wp79763"> </a><div class="pSmartList1"><li>The modifier cannot be <code class="cCode">static</code> or <code class="cCode">final</code>.</li></div><a name="wp79764"> </a><div class="pSmartList1"><li>The <code class="cCode">throws</code> clause must not define any application exceptions.</li></div><a name="wp79765"> </a><div class="pSmartList1"><li>The method has no arguments.</li></div></ul></div><a name="wp79766"> </a><p class="pBody">In the <code class="cCode">SimpleMessageBean</code> class, the <code class="cCode">ejbCreate</code> and <code class="cCode">ejbRemove</code> methods are empty.</p> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <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="MDB3.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="MDB5.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"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -