⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08s20.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Message Driven Beans and JBoss</title><link rel="stylesheet" href="styles.css" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/styles.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets Vimages/callouts/"><link rel="home" href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html" title="JBoss 3.0 Documentation"><link rel="up" href="ch08.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08.html" title="Chapter 8. JBoss and JMS"><link rel="previous" href="ch08s07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s07.html" title="JMS Provider"><link rel="next" href="ch08s32.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s32.html" title="JMS as a managed resource"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="jboss.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jboss.gif" border="0"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch08.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch08s07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s07.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch08s32.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s32.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table><div class="section"><a name="mdb.config"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="mdb.config"></a>Message Driven Beans and JBoss</h2></div></div><p>Message Driven Beans (MDBs) are a new type of bean added in the EJB 2.0 specification. The reason why these beans were added is that there was no way in EJB 1.1 to handle asynchronous invocation. The primary reason for this is that an EJB bean could never be invoked from other objects through anything other than its remote interface. Therefore a bean could never set itself up as a listener for asynchronous invocation.</p><p>This limitation is overcome by Message Drive Beans. An MDB is a bean without a remote interface; the container sets itself up as a listener for asynchronous invocation of the MDB and handles the invocation of the concrete bean. Otherwise, MDBs follow all the usual roles for EJB beans.</p><p>Message Driven Beans are primarily focused on JMS. An MDB is either a topic or a queue subscriber. One nice feature with MDBs is that one gets multithreaded subscribers (even for topics) without having to deal with the subtle difficulties of writing multithreaded code for JMS message consumers.</p><p>For what purpose should you use an MDB? Basically, you would use an MDB any time you are about to create a JMS subscriber. Typical conditions for doing this are:</p><div class="itemizedlist"><ul><li><p><a name="d0e4650"></a>Decouple an invoker from the invoked code.</p></li><li><p><a name="d0e4653"></a>Make it possible for multiple parties to get your messages.</p></li><li><p><a name="d0e4656"></a>Get asynchronous behavior, i.e. start long-running code without the need to wait for the called code to finish.</p></li></ul></div><p>If you already know how to code a Message Driven Bean, it is quite easy to deploy it in JBoss. The only thing you will have to do is to add a JBoss-specific deployment descriptor, or, if you already have a <tt>jboss.xml</tt> deployment descriptor for you module, add the MDB part to it.</p><p>[<span class="bold"><b>2.4.0</b></span>. It should actually be possible to deploy an MDB without the JBoss-specific deployment descriptor, although this is something that I do not recommend; for more on this, read ahead.]</p><div class="section"><a name="jms-mdb-nirvana"></a><div class="titlepage"><div><h3 class="title"><a name="jms-mdb-nirvana"></a>Five steps to MDB nirvana in JBoss</h3></div></div><p>If you don't need to do any special configuration and already know how to code a Message Driven Bean, here there are five easy steps to get it up and running with JBoss.</p><div class="orderedlist"><ol type="1"><li><p><a name="d0e4675"></a>Write the source code for a Message Driven Bean.</p></li><li><p><a name="d0e4678"></a>Write the <tt>ejb-jar.xml</tt> descriptor.</p><p>Here it is an example for a bean listening on a queue using bean-managed transactions:</p><pre class="programlisting">
&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE ejb-jar&gt;
&lt;ejb-jar&gt;
  &lt;enterprise-beans&gt;
    &lt;message-driven&gt;
      &lt;ejb-name&gt;HelloQueueBMTMDB&lt;/ejb-name&gt;
      &lt;ejb-class&gt;org.jboss.docs.jms.mdb.bean.HelloMDB&lt;/ejb-class&gt;
      &lt;message-selector&gt;&lt;/message-selector&gt;
      &lt;transaction-type&gt;Bean&lt;/transaction-type&gt;
      &lt;acknowledge-mode&gt;Auto-acknowledge&lt;/acknowledge-mode&gt;
      &lt;message-driven-destination&gt;
        &lt;destination-type&gt;javax.jms.Queue&lt;/destination-type&gt;
      &lt;/message-driven-destination&gt;
    &lt;/message-driven&gt;
  &lt;/enterprise-beans&gt;
&lt;/ejb-jar&gt;
</pre><p>And here it is one for a durable topic using container-managed transactions:</p><pre class="programlisting">
&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE ejb-jar&gt;
&lt;ejb-jar&gt;
  &lt;enterprise-beans&gt;
    &lt;message-driven&gt;
      &lt;ejb-name&gt;HelloTopicDurableMDB&lt;/ejb-name&gt;
      &lt;ejb-class&gt;org.jboss.docs.jms.mdb.bean.HelloMDB&lt;/ejb-class&gt;
      &lt;message-selector&gt;&lt;/message-selector&gt;
      &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
      &lt;message-driven-destination&gt;
        &lt;destination-type&gt;javax.jms.Topic&lt;/destination-type&gt;
        &lt;subscription-durability&gt;Durable&lt;/subscription-durability&gt;
      &lt;/message-driven-destination&gt;
    &lt;/message-driven&gt;
  &lt;/enterprise-beans&gt;
  &lt;assembly-descriptor&gt;
    &lt;container-transaction&gt;
      &lt;method&gt;
        &lt;ejb-name&gt;HelloTopicDurableMDB&lt;/ejb-name&gt;
        &lt;method-name&gt;*&lt;/method-name&gt;
      &lt;/method&gt;
      &lt;trans-attribute&gt;Required&lt;/trans-attribute&gt;
    &lt;/container-transaction&gt;
  &lt;/assembly-descriptor&gt;
&lt;/ejb-jar&gt;
</pre></li><li><p><a name="d0e4692"></a>Write the <tt>jboss.xml</tt> deployment descriptor; note that one does not need to fill in the container configuration for an MDB that has no special requirements.</p><p>[<span class="bold"><b>2.4.0</b></span>. It is possible to avoid doing this, in which case the destination will then be automatically created and named after the bean; this may lead to quite surprising results, so it's not anything I recommend.]</p><p>The <tt>destination-jndi-name</tt> element points to the queue.</p><p>Here it is one the for the queue bean:</p><pre class="programlisting">
&lt;?xml version="1.0" encoding="Cp1252"?&gt;
&lt;jboss&gt;
  &lt;enterprise-beans&gt;
    &lt;message-driven&gt;
      &lt;ejb-name&gt;HelloQueueMDB&lt;/ejb-name&gt;
      &lt;configuration-name&gt;Standard Message Driven Bean&lt;/configuration-name&gt;
      &lt;destination-jndi-name&gt;queue/testQueue&lt;/destination-jndi-name&gt;
    &lt;/message-driven&gt;
  &lt;/enterprise-beans&gt;
&lt;/jboss&gt;
</pre><p>And here it is the one for the durable topic:</p><pre class="programlisting">
&lt;?xml version="1.0" encoding="Cp1252"?&gt;
&lt;jboss&gt;
  &lt;enterprise-beans&gt;
    &lt;message-driven&gt;
      &lt;ejb-name&gt;HelloTopicDurableMDB&lt;/ejb-name&gt;
      &lt;configuration-name&gt;Standard Message Driven Bean&lt;/configuration-name&gt;
      &lt;destination-jndi-name&gt;topic/testTopic&lt;/destination-jndi-name&gt;
      &lt;mdb-user&gt;john&lt;/mdb-user&gt;
      &lt;mdb-passwd&gt;needle&lt;/mdb-passwd&gt;
      &lt;mdb-subscriber-id&gt;DurableSubscriberExample&lt;/mdb-subscriber-id&gt;
    &lt;/message-driven&gt;
  &lt;/enterprise-beans&gt;
&lt;/jboss&gt;
</pre></li><li><p><a name="d0e4716"></a>Edit the file <tt>jbossmq.xml</tt> in directory <tt>conf/default</tt> and add the queue or topic. For example:</p><pre class="programlisting">
&lt;Queue&gt;
  &lt;Name&gt;testQueue&lt;/Name&gt;
&lt;/Queue&gt;
</pre><p>for a queue and</p><pre class="programlisting">
&lt;Topic&gt;
  &lt;Name&gt;testDurableTopic&lt;/Name&gt;
&lt;/Topic&gt;
</pre><p>plus</p><pre class="programlisting">
&lt;User&gt;
  &lt;Name&gt;john&lt;/Name&gt;
  &lt;Password&gt;needle&lt;/Password&gt;
  &lt;Id&gt;DurableSubscriberExample&lt;/Id&gt;
  &lt;DurableSubscription&gt;
    &lt;Name&gt;DurableSubscriberExample&lt;/Name&gt;
    &lt;TopicName&gt;testDurableTopic&lt;/TopicName&gt;
  &lt;/DurableSubscription&gt;
&lt;/User&gt;
</pre><p>for a durable topic.</p><p>[<span class="bold"><b>2.4.1</b></span>. The addition of destinations is done in the <tt>jboss.jcml</tt> file, instead:</p><pre class="programlisting">
&lt;mbean code="org.jbossmq.server.TopicManager"
  name="JBossMQ:service=Topic,name=testTopic"/&gt;
</pre><p>Users are instead configured in the file <tt>jbossmq-state.xml</tt>, also found in directory <tt>conf/default</tt>. The entries look the same as those above.]</p></li><li><p><a name="d0e4755"></a>Deploy the bean, for example, by packing it in a JAR file and copying it into the JBoss <tt>deploy</tt> directory.</p></li></ol></div><p>At this point, your MDB is ready to be used, so you may start sending messages to it.</p></div><div class="section"><a name="jms-mdb-examples"></a><div class="titlepage"><div><h3 class="title"><a name="jms-mdb-examples"></a>Writing Message Driven Beans</h3></div></div><p> Since MDBs are still a recent addition to the EJB portfolio, they are still somewhat unknown and therefore not widely used yet. We will therefore begin by giving you a couple of simple examples of how to write and use Message Driven Beans.</p><div class="section"><a name="jms-mdb-examples-hello"></a><div class="titlepage"><div><h4 class="title"><a name="jms-mdb-examples-hello"></a>Hello World MDB</h4></div></div><p>An MDB follows a typical EJB contract. It must implement the following two interfaces:</p><div class="itemizedlist"><ul><li><p><a name="d0e4774"></a><tt>javax.ejb.MessageDrivenBean</tt></p></li><li><p><a name="d0e4778"></a><tt>javax.jms.MessageListener</tt></p></li></ul></div><p>An MDB must therefore typically contain the following four methods:</p><div class="figure"><p><a name="d0e4784"></a><b>Figure 8.44. Message Driven Bean required methods</b></p><pre class="programlisting">
public void setMessageDrivenContext(MessageDrivenContext ctx);
public void ejbCreate();
public void ejbRemove();
public void onMessage(Message message);

</pre></div><p>The full program listing of a simple &#8220;Hello World&#8221; bean could look like this:</p><div class="figure"><p><a name="d0e4794"></a><b>Figure 8.45. Hello World Message Driven Bean example, from <tt>HelloMDB.java</tt> in directory <tt>org/jboss/docs/jms/mdb/bean</tt>.</b></p><pre class="programlisting">
package org.jboss.docs.jms.mdb.bean;

import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.EJBException;

import javax.jms.MessageListener;
import javax.jms.Message;

/**
 * Simple HelloWorld Message Driven Bean. May be bound to both a Topic
 * or a Queue through the deployment descriptor.
 *
 * Created: Thu Jul 26 13:20:32 2001
 *
 * @author Peter Antman
 * @version $Revision: 3.1 $ $Date: 2002/01/09 00:29:47 $
 */
public class HelloMDB implements MessageDrivenBean, MessageListener {

  private MessageDrivenContext ctx = null;

  public HelloMDB() {

  }

  //--- MessageDrivenBean
  public void setMessageDrivenContext(MessageDrivenContext ctx)
    throws EJBException {

    this.ctx = ctx;

  }

  public void ejbCreate() {}

  public void ejbRemove() {ctx=null;}

  //--- MessageListener
  public void onMessage(Message message) {

    System.err.println("Bean got message" + message.toString());

  }

} // HelloMDB

</pre></div><p>To deploy this MDB into JBoss we will have to write two deployment descriptors. One standard (<tt>ejb-jar.xml</tt>) and one that is specific to JBoss (<tt>jboss.xml</tt>). We will make this bean a Topic subscriber and, since it does not do anything important, we will use container-managed transactions with the <tt>NotSupported</tt> attribute (although this would not be the best thing to do, in most cases).</p><div class="figure"><p><a name="d0e4816"></a><b>Figure 8.46. <tt>ejb-jar.xml</tt> deployment descriptor for topic Hello World MDB, from <tt>HelloMDB-Topic-ejb-jar.xml</tt> in directory <tt>org/jboss/docs/jms/resources</tt></b></p><pre class="programlisting">
&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE ejb-jar&gt;
&lt;ejb-jar&gt;
  &lt;enterprise-beans&gt;
    &lt;message-driven&gt;
      &lt;ejb-name&gt;HelloTopicMDB&lt;/ejb-name&gt;
      &lt;ejb-class&gt;org.jboss.docs.jms.mdb.bean.HelloMDB&lt;/ejb-class&gt;
      &lt;message-selector&gt;&lt;/message-selector&gt;
      &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
      &lt;message-driven-destination&gt;
        &lt;destination-type&gt;javax.jms.Topic&lt;/destination-type&gt;
        &lt;subscription-durability&gt;NonDurable&lt;/subscription-durability&gt;
      &lt;/message-driven-destination&gt;
    &lt;/message-driven&gt;
  &lt;/enterprise-beans&gt;
  &lt;assembly-descriptor&gt;
    &lt;container-transaction&gt;
      &lt;method&gt;
        &lt;ejb-name&gt;HelloTopicMDB&lt;/ejb-name&gt;
        &lt;method-name&gt;*&lt;/method-name&gt;
      &lt;/method&gt;
      &lt;trans-attribute&gt;NotSupported&lt;/trans-attribute&gt;
    &lt;/container-transaction&gt;
  &lt;/assembly-descriptor&gt;
&lt;/ejb-jar&gt;

</pre></div><p>We also need to write a small deployment descriptor (<tt>jboss.xml</tt>) that is specific to JBoss. The full-blown version of this is quite big, as it allows to configure the MDB container quite a bit. For most users this is not necessary, so we will use the standard configuration. The most important part of the descriptor is the specification of the destination. We choose the testTopic because it is always available in JBoss.</p><div class="figure"><p><a name="d0e4833"></a><b>Figure 8.47. JBoss-specific deployment descriptor for topic Hello World MDB, from <tt>HelloMDB-Topic-jboss.xml</tt> in directory <tt>org/jboss/docs/jms/resources</tt></b></p><pre class="programlisting">
&lt;?xml version="1.0" encoding="Cp1252"?&gt;
&lt;jboss&gt;
  &lt;enterprise-beans&gt;
    &lt;message-driven&gt;
      &lt;ejb-name&gt;HelloTopicMDB&lt;/ejb-name&gt;
      &lt;configuration-name&gt;Standard Message Driven Bean&lt;/configuration-name&gt;
      &lt;destination-jndi-name&gt;topic/testTopic&lt;/destination-jndi-name&gt;
    &lt;/message-driven&gt;
  &lt;/enterprise-beans&gt;
&lt;/jboss&gt;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -