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

📄 ch08s32.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
📖 第 1 页 / 共 3 页
字号:
   * Help method to send message via JMS
   */
  private void sendMessage(String msg) {

    TopicSession topicSession = null;

    try {

      TopicPublisher topicPublisher = null;
      TextMessage message = null;

      // Create a session
      topicSession = topicConnection.createTopicSession(true,
        Session.AUTO_ACKNOWLEDGE);

      // Create a publisher
      topicPublisher = topicSession.createPublisher(topic);

      // Create a message
      message = topicSession.createTextMessage();
      message.setText(msg);

      // Publish it
      System.out.println("Publishing message " + msg);
      topicPublisher.publish(message);

    } catch (JMSException ex) {

      ex.printStackTrace();
      ctx.setRollbackOnly();
      throw new EJBException(ex.toString());

    } finally {

      // ALWAYS close the session. It's pooled, so do not worry.

      if (topicSession != null) {

         try {

           topicSession.close();

         } catch (Exception e) {

           e.printStackTrace();

         }

       }

     }

  }

}

</pre></div><p>Here there are also the remote and home interfaces:</p><div class="figure"><p><a name="d0e5645"></a><b>Figure 8.83. [<span class="bold">2.4.0</span>] Remote interface for JMS resource example, from <tt>Hello.java</tt> in directory <tt>org/jboss/docs/jms/ra/interfaces</tt></b></p><pre class="programlisting">
package  org.jboss.docs.jms.ra.interfaces;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

/**
 * Remote interface for Hello bean.
 *
 * @author Peter Antman
 * @version $Revision: 3.1 $
 */

public interface Hello extends EJBObject {

  /**
   * Send a message to the configured topic
   * (jms/TopicName in deployment descriptor)
   */
  public void hello(String msg) throws RemoteException;

}

</pre></div><div class="figure"><p><a name="d0e5658"></a><b>Figure 8.84. [<span class="bold">2.4.0</span>] Home interface for JMS resource example, from <tt>HelloHome.java</tt> in directory <tt>org/jboss/docs/jms/ra/interfaces</tt></b></p><pre class="programlisting">
package  org.jboss.docs.jms.ra.interfaces;

import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;

/**
 * Home interface for Hello bean.
 *
 * @author Peter Antman
 * @version $Revision: 3.1 $
 */
public interface HelloHome extends EJBHome {

  Hello create() throws RemoteException, CreateException;

}

</pre></div><p>We also need a client to send messages to the session bean, which is standard client EJB programming.</p><div class="figure"><p><a name="d0e5673"></a><b>Figure 8.85. [<span class="bold">2.4.0</span>] Client for JMS resource example, from <tt>HelloClient.java</tt> in directory <tt>org/jboss/docs/jms/ra</tt></b></p><pre class="programlisting">
package org.jboss.docs.jms.ra;

import org.jboss.docs.jms.ra.interfaces.Hello;
import org.jboss.docs.jms.ra.interfaces.HelloHome;

import javax.naming.InitialContext;
import javax.naming.Context;

/**
 * Client that send hello messages to a session bean
 * (which does JMS through the resource adapter).
 *
 * @author Peter Antman
 * @version $Revision: 3.1 $
 */

public class HelloClient
{

  /**
   * The bean JNDI name
   */
  private String beanJNDI;

  /**
   * The Hello bean remote reference
   */
  private Hello hello;

  /**
   * @param beanJNDI bean where to send the message
   */
  public HelloClient(String beanJNDI) throws Exception {

    this.beanJNDI = beanJNDI;
    setUp();

  }

  /**
   * Helper to get the remote reference to the bean
   */
  protected void setUp() throws Exception {

    // Get Hello bean
    Context context = new InitialContext();
    HelloHome helloH = (HelloHome)context.lookup(beanJNDI);

    hello = helloH.create();

  }

  /**
   * Say hallo to the bean
   */
  public void hello(String msg) throws Exception {

    System.out.println("Saying hello");
    hello.hello(msg);

  }

  public static void main(String[] args) {

    try {

      HelloClient c = new HelloClient("TopicHello");
      c.hello("Hello topic");

    } catch(Exception ex) {

      System.out.println("Error: " + ex);
      ex.printStackTrace();

    }

  }

} // HelloClient

</pre></div><p>The next step is to write the deployment descriptors for the bean. In the standard <tt>ejb-jar.xml</tt> we must add stanzas for the resources. The <tt>res-ref-name</tt> element must contain the name used in the bean to lookup the resource:</p><div class="figure"><p><a name="d0e5694"></a><b>Figure 8.86. [<span class="bold">2.4.0</span>] <tt>resource-ref</tt> stanzas in <tt>ejb-jar.xml</tt> for JMS resource example</b></p><pre class="programlisting">
&lt;resource-ref&gt;
  &lt;description&gt;A Topic ConnectionFactory&lt;/description&gt;
  &lt;res-ref-name&gt;jms/MyTopicConnection&lt;/res-ref-name&gt;
  &lt;res-type&gt;javax.jms.TopicConnectionFactory&lt;/res-type&gt;
  &lt;res-auth&gt;Container&lt;/res-auth&gt;
&lt;/resource-ref&gt;
&lt;resource-ref&gt;
  &lt;description&gt;A Topic &lt;/description&gt;
  &lt;res-ref-name&gt;jms/TopicName&lt;/res-ref-name&gt;
  &lt;res-type&gt;javax.jms.Topic&lt;/res-type&gt;
  &lt;res-auth&gt;Container&lt;/res-auth&gt;
&lt;/resource-ref&gt;

</pre></div><p>In <tt>jboss.xml</tt> we must also add <tt>resource-manager</tt> stanzas to point out the real resources:</p><div class="figure"><p><a name="d0e5716"></a><b>Figure 8.87. [<span class="bold">2.4.0</span>] <tt>resource-managers</tt> stanza in <tt>jboss.xml</tt> for JMS resource example</b></p><pre class="programlisting">
&lt;resource-managers&gt;
  &lt;resource-manager&gt;
    &lt;res-name&gt;topicfactoryref&lt;/res-name&gt;
    &lt;res-jndi-name&gt;java:/JmsXA&lt;/res-jndi-name&gt;
  &lt;/resource-manager&gt;
  &lt;resource-manager&gt;
    &lt;res-name&gt;topicref&lt;/res-name&gt;
    &lt;res-jndi-name&gt;topic/testTopic&lt;/res-jndi-name&gt;
  &lt;/resource-manager&gt;
&lt;/resource-managers&gt;

</pre></div><p>The most magical line here is the <tt>java:/JmsXA</tt> contents of the <tt>res-jndi-name</tt> element, which points to the J2EE Connector-compliant JMS resource adapter. Also in <tt>jboss.xml</tt>, we must add the definition of the session EJB, as always:</p><div class="figure"><p><a name="d0e5741"></a><b>Figure 8.88. [<span class="bold">2.4.0</span>] Bean configuration in <tt>jboss.xml</tt> for JMS resource example</b></p><pre class="programlisting">
&lt;session&gt;
  &lt;ejb-name&gt;TopicHello&lt;/ejb-name&gt;
  &lt;jndi-name&gt;TopicHello&lt;/jndi-name&gt;
  &lt;configuration-name&gt;Standard Stateless SessionBean&lt;/configuration-name&gt;
  &lt;resource-ref&gt;
    &lt;res-ref-name&gt;jms/MyTopicConnection&lt;/res-ref-name&gt;

⌨️ 快捷键说明

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