📄 ch08s32.html
字号:
* 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">
<resource-ref>
<description>A Topic ConnectionFactory</description>
<res-ref-name>jms/MyTopicConnection</res-ref-name>
<res-type>javax.jms.TopicConnectionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>A Topic </description>
<res-ref-name>jms/TopicName</res-ref-name>
<res-type>javax.jms.Topic</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</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">
<resource-managers>
<resource-manager>
<res-name>topicfactoryref</res-name>
<res-jndi-name>java:/JmsXA</res-jndi-name>
</resource-manager>
<resource-manager>
<res-name>topicref</res-name>
<res-jndi-name>topic/testTopic</res-jndi-name>
</resource-manager>
</resource-managers>
</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">
<session>
<ejb-name>TopicHello</ejb-name>
<jndi-name>TopicHello</jndi-name>
<configuration-name>Standard Stateless SessionBean</configuration-name>
<resource-ref>
<res-ref-name>jms/MyTopicConnection</res-ref-name>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -