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

📄 ch08s20.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
📖 第 1 页 / 共 5 页
字号:

import org.jboss.docs.jms.mdb.interfaces.HelloWorker;
import org.jboss.docs.jms.mdb.interfaces.HelloWorkerHome;

/**
 * Simple MDB showing the listener pattern.
 * Gets messages from a destination and invokes a worker session bean
 * (actually it could as well do that work itself).
 *
 * Created: Thu Jul 26 13:20:32 2001
 *
 * @author Peter Antman
 * @version $Revision: 3.1 $ $Date: 2002/01/09 00:29:47 $
 */

public class HelloListener implements MessageDrivenBean, MessageListener {

  /**
   * Session worker bean
   */
  private HelloWorkerHome workerHome = null;

  private MessageDrivenContext ctx = null;

  public HelloListener() {

  }

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

    this.ctx = ctx;

  }

  public void ejbCreate() throws CreateException{

    // Get the worker bean home
    try {

      Context initCtx = new InitialContext();
      workerHome =
        (HelloWorkerHome)initCtx.lookup("java:comp/env/ejb/worker");

    } catch(Exception ex) {

      throw new CreateException("Could not get worker: " + ex);

    }

  }

  public void ejbRemove() {ctx=null;}

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

    System.out.println("HelloListen got message " + message.toString());

    try {

      // Get the worker
      HelloWorker worker = workerHome.create();

      // Get the message, here we could have an ObjectMessage containing
      // an object of a known class. We use Text here for simplicity
      if (message instanceof TextMessage) {

        TextMessage m = (TextMessage)message;

        // invoke the worker bean
        worker.doWork(m.getText());

      }

    } catch(Exception ex) {

      // If a container-managed transaction with required, this will
      // roll back the message received and also all beans and resource
      // calls from this one that inherits the transactional context.
      throw new EJBException("Could not call worker " + ex);

    }

  }

} // HelloListener

</pre></div><p>To deploy this MDB in JBoss we need to write two deployment descriptors, one standard and one specific for JBoss. Lets begin with the standard one. For ease of use we will include both beans into one JAR file. For the Message Driven Bean we have to decide whether it is a topic or not and what kind of transaction mode it should use. In this case, we choose to use a topic and container-managed transactions. We also have to specify an <tt>ejb-ref</tt> element so that the listener can look up the home of the worker bean:</p><div class="figure"><p><a name="d0e4994"></a><b>Figure 8.58. Message Driven Bean part in <tt>ejb-jar.xml</tt> for listener</b></p><pre class="programlisting">
&lt;message-driven&gt;
  &lt;ejb-name&gt;HelloListener&lt;/ejb-name&gt;
  &lt;ejb-class&gt;org.jboss.docs.jms.mdb.bean.HelloListener&lt;/ejb-class&gt;
  &lt;message-selector&gt;&lt;/message-selector&gt;
  &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
  &lt;ejb-ref&gt;
    &lt;description&gt;The Workers home&lt;/description&gt;
    &lt;ejb-ref-name&gt;ejb/worker&lt;/ejb-ref-name&gt;
    &lt;ejb-ref-type&gt;Session&lt;/ejb-ref-type&gt;
    &lt;ejb-link&gt;HelloWorkerBean&lt;/ejb-link&gt;
    &lt;home&gt;org.jboss.docs.jms.mdb.interfaces.HelloWorkerHome&lt;/home&gt;
    &lt;remote&gt;org.jboss.docs.jms.mdb.interfaces.HelloWorker&lt;/remote&gt;
  &lt;/ejb-ref&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;

</pre></div><p>We also have to add an entry for the worker bean:</p><div class="figure"><p><a name="d0e5004"></a><b>Figure 8.59. Session part in <tt>ejb-jar.xml</tt> for listener</b></p><pre class="programlisting">
&lt;session&gt;
  &lt;description&gt;Worker bean&lt;/description&gt;
  &lt;display-name&gt;HelloWorkerBean&lt;/display-name&gt;
  &lt;ejb-name&gt;HelloWorkerBean&lt;/ejb-name&gt;
  &lt;home&gt;org.jboss.docs.jms.mdb.interfaces.HelloWorkerHome&lt;/home&gt;
  &lt;remote&gt;org.jboss.docs.jms.mdb.interfaces.HelloWorker&lt;/remote&gt;
  &lt;ejb-class&gt;org.jboss.docs.jms.mdb.bean.HelloWorkerBean&lt;/ejb-class&gt;
  &lt;session-type&gt;Stateless&lt;/session-type&gt;
  &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
&lt;/session&gt;

</pre></div><p>Here it is the complete deployment descriptor, including definitions of the transaction type.</p><div class="figure"><p><a name="d0e5014"></a><b>Figure 8.60. <tt>ejb-jar.xml</tt> for listener bean example, from <tt>HelloListener-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;HelloListener&lt;/ejb-name&gt;
      &lt;ejb-class&gt;org.jboss.docs.jms.mdb.bean.HelloListener&lt;/ejb-class&gt;
      &lt;message-selector&gt;&lt;/message-selector&gt;
      &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
      &lt;ejb-ref&gt;
        &lt;description&gt;The Workers home&lt;/description&gt;
        &lt;ejb-ref-name&gt;ejb/worker&lt;/ejb-ref-name&gt;
        &lt;ejb-ref-type&gt;Session&lt;/ejb-ref-type&gt;
        &lt;ejb-link&gt;HelloWorkerBean&lt;/ejb-link&gt;
        &lt;home&gt;org.jboss.docs.jms.mdb.interfaces.HelloWorkerHome&lt;/home&gt;
        &lt;remote&gt;org.jboss.docs.jms.mdb.interfaces.HelloWorker&lt;/remote&gt;
      &lt;/ejb-ref&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;session&gt;
      &lt;description&gt;Worker bean&lt;/description&gt;
      &lt;display-name&gt;HelloWorkerBean&lt;/display-name&gt;
      &lt;ejb-name&gt;HelloWorkerBean&lt;/ejb-name&gt;
      &lt;home&gt;org.jboss.docs.jms.mdb.interfaces.HelloWorkerHome&lt;/home&gt;
      &lt;remote&gt;org.jboss.docs.jms.mdb.interfaces.HelloWorker&lt;/remote&gt;
      &lt;ejb-class&gt;org.jboss.docs.jms.mdb.bean.HelloWorkerBean&lt;/ejb-class&gt;
      &lt;session-type&gt;Stateless&lt;/session-type&gt;
      &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
    &lt;/session&gt;
  &lt;/enterprise-beans&gt;
  &lt;assembly-descriptor&gt;
    &lt;container-transaction&gt;
      &lt;method&gt;
        &lt;ejb-name&gt;HelloListener&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;container-transaction&gt;
      &lt;method&gt;
        &lt;ejb-name&gt;HelloWorkerBean&lt;/ejb-name&gt;
        &lt;method-intf&gt;Remote&lt;/method-intf&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></div><p>We also need to write a <tt>jboss.xml</tt> deployment descriptor, specific for JBoss. This is needed because the destination JNDI name must be defined somewhere: we cannot simply use a default.</p><div class="figure"><p><a name="d0e5031"></a><b>Figure 8.61. JBoss deployment descriptor for listener bean, from <tt>HelloListener-jboss.xml</tt> in directory <tt>org/jboss/docs/jms/resources</tt></b></p><pre class="programlisting">
&lt;?xml version="1.0"?&gt;
&lt;jboss&gt;
  &lt;enterprise-beans&gt;
    &lt;message-driven&gt;
      &lt;ejb-name&gt;HelloListener&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;secure&gt;false&lt;/secure&gt;

⌨️ 快捷键说明

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