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

📄 ch08s20.html

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

</pre></div><p>Now we have to compile the beans. You will have to add several JAR files to your classpath to succeed at this. Among the most important, especially for MDB, is <tt>ejb2.0.jar</tt> in the <tt>lib/ext</tt> directory of the standard JBoss installation.</p><p>[<span class="bold"><b>2.4.0</b></span>. It is <tt>jboss-j2ee.jar</tt> in directory <tt>client</tt>.]</p><p>Afterwards, you will need to pack the beans into a JAR file and deploy them by copying the JAR file to the JBoss directory <tt>deploy</tt>. You may use the sample publisher HelloPublisher from <a href="ch08s07.html#jms-jms-examples" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s07.html#jms-jms-examples" title="Examples">the section called &#8220;Examples&#8221;</a> to publish messages to the bean.</p><p>The example code is found in the files <tt>HelloListener.java</tt> and <tt>HelloWorkerBean.java</tt> in directory <tt>org/jboss/docs/jms/mdb/bean</tt> and <tt>HelloWorker.java</tt> and <tt>HelloWorkerHome.java</tt> in directory <tt>org/jboss/docs/jms/mdb/interfaces</tt>. The deployment descriptors are in files <tt>HelloListener-ejb-jar.xml</tt> and <tt>HelloListener-jboss.xml</tt> in directory <tt>org/jboss/docs/jms/resources</tt>.</p><p>It is possible to run the example through the Ant build file:</p><div class="figure"><p><a name="d0e5098"></a><b>Figure 8.62. Running the <tt>HelloListener</tt> example</b></p><pre class="programlisting">
ant jms-hello-listener

</pre></div><p>The output should look somewhat like this:</p><div class="figure"><p><a name="d0e5108"></a><b>Figure 8.63. Output of running the <tt>HelloListener</tt> example</b></p><pre class="programlisting">
[Default] WorkerBean doing work: Hello World no. 3
[Default] WorkerBean doing work: Hello World no. 5
[Default] WorkerBean doing work: Hello World no. 2
[Default] WorkerBean doing work: Hello World no. 6
[Default] WorkerBean doing work: Hello World no. 4
[Default] WorkerBean doing work: Hello World no. 1
[Default] HelloListen got messageTextMessage@Hello World no. 7
[Default] WorkerBean doing work: Hello World no. 7
[Default] HelloListen got messageTextMessage@Hello World no. 8
[Default] WorkerBean doing work: Hello World no. 8
[Default] HelloListen got messageTextMessage@Hello World no. 9
[Default] WorkerBean doing work: Hello World no. 9

</pre></div><p>The example may not always work, since it depends on the deployment of the beans (done by simple copying) being finished before the client starts. If it does not work, run it several times.</p></div><div class="section"><a name="jms-mdb-examples-adaptor"></a><div class="titlepage"><div><h4 class="title"><a name="jms-mdb-examples-adaptor"></a>The adapter pattern</h4></div></div><p>Another way to use an MDB is to employ it as an adapter, for example, between different messaging systems. This is rewarding, especially if you have a J2EE Connector resource adapter for the other system, since you will get then a very effective, multithreaded and pooled system, without having to write any advanced programming. Since I have written such a resource adapter for the messaging server <a href="javascript:if(confirm('http://www.xmlblaster.org/  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.xmlblaster.org/'" tppabs="http://www.xmlblaster.org/" target="_top"><i>XmlBlaster</i></a>, we will look here at one way to adapt JMS to it. To get the source code, check out the XmlBlaster source.</p><p>The adapter uses an MDB to subscribe to a topic. It then republishes the messages to an XmlBlaster server through the XmlBlaster K2 J2EE Connector adapter. The code is fairly simple. A J2EE Connector resource adapter must be deployed in the JBoss server. It is then referenced from within the bean in the same manner you would reference a JDBC resource. You would look it up this way:</p><pre class="programlisting">
factory = (BlasterConnectionFactory)
  new InitialContext().lookup("java:comp/env/xmlBlaster");

</pre><p>And use it this way:</p><pre class="programlisting">
con = factory.getConnection();

// Construct Blaster Headers
String key = "&lt;key oid=\"" + message.getJMSMessageID() +
  "\" contentMime=\"text/xml\"&gt;&lt;/key&gt;";
String qos = "&lt;qos&gt;&lt;/qos&gt;";

con.publish(new MessageUnit(key,msg.getBytes(),qos));

</pre><p>The complete bean is pretty straightforward (almost the same code could be used to write directly to a database, for example):</p><pre class="programlisting">
package javaclients.j2ee.k2;

import javax.naming.InitialContext;
import javax.naming.NamingException;

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

import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.JMSException;

import javax.resource.ResourceException;

import org.xmlBlaster.j2ee.k2.client.*;

import org.xmlBlaster.util.XmlBlasterException;
import org.xmlBlaster.engine.helper.MessageUnit;

public class JmsAdapter implements MessageDrivenBean, MessageListener{

  private MessageDrivenContext ctx = null;
  private BlasterConnectionFactory factory = null;

  public JmsAdapter() {

  }

  public void setMessageDrivenContext(MessageDrivenContext ctx)
    throws EJBException {

    this.ctx = ctx;

    try {

      factory = (BlasterConnectionFactory)
        new InitialContext().lookup("java:comp/env/xmlBlaster");

    } catch (NamingException ex) {

      throw new EJBException ("XmlBlaster not found: " +
        ex.getMessage());

    } catch(Throwable th) {

      System.err.println("Throwable: " + th);
      th.printStackTrace();
      throw new EJBException("Throwable in setContext: " + th);

    }

  }

  public void ejbCreate() {}

  public void ejbRemove() {ctx=null;}

  public void onMessage(Message message) {

    BlasterConnection con = null;

    try {

      // Get message to handle
      System.err.println("Got message: " + message);

      if (message instanceof TextMessage) {

        String msg = ((TextMessage)message).getText();

        // Get connection
        con = factory.getConnection();

        // Construct Blaster Headers - how to handle key here?
        String key = "&lt;key oid=\"" + message.getJMSMessageID() +
          "\" contentMime=\"text/xml\"&gt;&lt;/key&gt;";

        String qos = "&lt;qos&gt;&lt;/qos&gt;";
        con.publish(new MessageUnit(key,msg.getBytes(),qos));

      } else {

        System.err.println("Got message type I cant handle");

      }

    } catch(ResourceException re) {

      System.err.println("Resource ex: " + re);
      re.printStackTrace();

    } catch(XmlBlasterException be) {

      System.err.println("Blaster ex: " + be);
      be.printStackTrace();

    } catch(JMSException je) {

      System.err.println("JMSException ex: " + je);
      je.printStackTrace();

    } catch(Throwable th) {

      System.err.println("Throwable: " + th);
      th.printStackTrace();

    } finally {

      try {

        if (con != null)
          con.close ();

      }
      catch (Exception ex) {}

    }

  }

} // MessageBeanImpl

</pre><p>The deployment descriptors for this bean are typical, except that you will have to add entries for the <tt>resource-ref</tt> element. Here it is the standard EJB deployment descriptor:</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;JmsAdapter&lt;/ejb-name&gt;
      &lt;ejb-class&gt;javaclients.j2ee.k2.JmsAdapter&lt;/ejb-class&gt;
      &lt;message-selector&gt;&lt;/message-selector&gt;
      &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
      &lt;resource-ref&gt;
        &lt;res-ref-name&gt;xmlBlaster&lt;/res-ref-name&gt;
        &lt;res-type&gt;org.xmlBlaster.j2ee.k2.client.BlasterConnectionFactory&lt;/res-type&gt;
        &lt;res-auth&gt;Container&lt;/res-auth&gt;
      &lt;/resource-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;/enterprise-beans&gt;
  &lt;assembly-descriptor&gt;
    &lt;container-transaction&gt;
      &lt;method&gt;
        &lt;ejb-name&gt;JmsAdapter&lt;/ejb-name&gt;

⌨️ 快捷键说明

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