📄 ch08s20.html
字号:
</pre></div><p>Then we will have to pack the three files into a JAR file. Here it is a snippet from an Ant build file that shows how to pack the bean:</p><div class="figure"><p><a name="d0e4845"></a><b>Figure 8.48. Ant build example for packing MDB in a JAR file</b></p><pre class="programlisting">
<target name="hello-topic-jar" depends="compile">
<delete dir="${build.jms.dir}/META-INF"/>
<mkdir dir="${build.jms.dir}/META-INF"/>
<copy file="${jms.resource.dir}/HelloMDB-Topic-ejb-jar.xml"
tofile="${build.jms.dir}/META-INF/ejb-jar.xml" />
<copy file="${jms.resource.dir}/HelloMDB-Topic-jboss.xml"
tofile="${build.jms.dir}/META-INF/jboss.xml" />
<jar jarfile="${build.jms.dir}/HelloTopicMDB.jar">
<fileset dir="${build.classes.dir}">
<include name="org/jboss/docs/jms/mdb/bean/HelloMDB.class" />
</fileset>
<fileset dir="${build.jms.dir}">
<include name="META-INF/ejb-jar.xml" />
<include name="META-INF/jboss.xml" />
</fileset>
</jar>
</target>
</pre></div><p>We will need to copy the JAR file to the <tt>deploy</tt> directory of JBoss to get the MDB installed.</p><p>To send messages to the bean you need a JMS publisher. This involves standard JMS programming, so you may use the HelloPublisher example 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 “Examples”</a> to do that.</p><p>The example code for the bean is found in file <tt>HelloMDB.java</tt> in directory <tt>org/jboss/docs/jms/mdb/bean</tt>. Example deployment descriptors are available in files <tt>HelloMDB-Topic-ejb-jar.xml</tt> and <tt>HelloMDB-Topic-jboss.xml</tt> in directory <tt>org/jboss/docs/jms/resources</tt>. You may also run the example via the Ant build file:</p><div class="figure"><p><a name="d0e4876"></a><b>Figure 8.49. Running the topic Hello World MDB</b></p><pre class="programlisting">
ant jms-hello-topic
</pre></div><p>The output in the window where you started JBoss will typically look something similar to this:</p><div class="figure"><p><a name="d0e4883"></a><b>Figure 8.50. Output of running the topic Hello World MDB</b></p><pre class="programlisting">
[Auto deploy] Auto deploy of file:/home/pra/jboss/deploy/HelloTopicMDB.jar
[J2EE Deployer Default] Deploy J2EE application: file:/home/pra/jboss/deploy/HelloTopicMDB.jar
[J2EE Deployer Default] Create application HelloTopicMDB.jar
[J2EE Deployer Default] install module HelloTopicMDB.jar
[Container factory] Deploying:file:/home/pra/jboss/tmp/deploy/Default/HelloTopicMDB.jar
[Verifier] Verifying file:/home/pra/jboss/tmp/deploy/Default/HelloTopicMDB.jar/ejb1028.jar
[Container factory] Deploying HelloTopicMDB
[Container factory] Deployed application: file:/home/pra/jboss/tmp/deploy/Default/HelloTopicMDB.jar
[J2EE Deployer Default] J2EE application: file:/home/pra/jboss/deploy/HelloTopicMDB.jar is deployed.
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 1
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 2
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 4
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 5
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 3
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 8
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 9
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 7
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 6
[HelloTopicMDB] Bean got messageTextMessage@Hello World no. 10
[Auto deploy] Auto undeploy of file:/home/pra/jboss/deploy/HelloTopicMDB.jar
[J2EE Deployer Default] Stopping module HelloTopicMDB.jar
[Container factory] Undeploying:file:/home/pra/jboss/tmp/deploy/Default/HelloTopicMDB.jar
[Container factory] Undeployed application: file:/home/pra/jboss/tmp/deploy/Default/HelloTopicMDB.jar
[J2EE Deployer Default] Destroying application HelloTopicMDB.jar
</pre></div><p>There are several other examples available with alternative configurations. All the deployment files begin with the prefix <tt>HelloMDB-</tt>. The following Ant targets are available to run the examples based on HelloMDB:</p><div class="figure"><p><a name="d0e4893"></a><b>Figure 8.51. Ant targets for the HelloMDB examples</b></p><pre class="programlisting">
jms-hello-topic
jms-hello-topic-durable
jms-hello-topic-fullconf
jms-hello-queue
jms-hello-queue-bmt
</pre></div></div><div class="section"><a name="jms-mdb-examples-listener"></a><div class="titlepage"><div><h4 class="title"><a name="jms-mdb-examples-listener"></a>MDB as a listener</h4></div></div><p>Here we will look at an example that does something more real. One nice way to use MDBs is to have them perform the listener pattern. Normally, listeners register themselves with the object emitting events; but, this is not possible for an MDB, since the bean itself may only do some work when it receives an event (a message). The set-up of an MDB as a listener will therefore have to be done outside of the MDB. This could involve something as simple as defining a topic or queue and hardwire it into the event generator to get it to publish its events/messages to that destination. It's also possible to create a more generic framework for message-driven callbacks, something I have done with JMS and JMX. But that is for another document. Let's instead look at things on the MDB side.</p><p>One way to partition the logic with EJBs is to have one bean that does the real work (contains the logic), a stateless session bean or an entity bean, for example, and another bean acting as a listener. Let's write a working, but simplified, version of this pattern. We start with a very simple stateless session bean containing just one method: <tt>doWork</tt>. To make it easy we let it take a <tt>String</tt> as its payload. This is straightforward; first, we need a home interface:</p><div class="figure"><p><a name="d0e4912"></a><b>Figure 8.52. Home interface for worker bean, from <tt>HelloWorkerHome.java</tt> in directory <tt>org/jboss/docs/jms/mdb/interfaces</tt></b></p><pre class="programlisting">
package org.jboss.docs.jms.mdb.interfaces;
import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
/**
* Home for HelloWorker.
*
* Created: Thu Jul 26 16:02:46 2001
*
* @author Peter Antman
* @version $Revision: 3.1 $ $Date: 2002/01/09 00:29:47 $
*/
public interface HelloWorkerHome extends EJBHome {
public HelloWorker create() throws RemoteException, CreateException;
} // HelloWorkerHome
</pre></div><p>We also need a remote interface:</p><div class="figure"><p><a name="d0e4924"></a><b>Figure 8.53. Remote interface for worker bean, from <tt>HelloWorker.java</tt> in directory <tt>org/jboss/docs/jms/mdb/interfaces</tt></b></p><pre class="programlisting">
package org.jboss.docs.jms.mdb.interfaces;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
/**
* Interface for HelloWorker.
*
* Created: Thu Jul 26 15:50:06 2001
*
* @author Peter Antman
* @version $Revision: 3.1 $ $Date: 2002/01/09 00:29:47 $
*/
public interface HelloWorker extends EJBObject {
/**
* Just a demo work method.
*/
public void doWork(String work) throws RemoteException;
} // HelloWorker
</pre></div><p>And, finally, we need the actual implementation bean class:</p><div class="figure"><p><a name="d0e4936"></a><b>Figure 8.54. Listener pattern worker bean, from <tt>HelloWorkerBean.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.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
/**
* A worker session bean. When invoked through an MDB listener, this
* bean will be invoked asynchronously by sending messages to the JMS
* destination to which the MDB is configured as a subscriber.
*
* @author Peter Antman
* @version $Revision: 3.1 $
*/
public class HelloWorkerBean implements SessionBean {
private SessionContext ctx;
public HelloWorkerBean() {
}
// The usual sessions ejb methods.
public void setSessionContext(javax.ejb.SessionContext ctx)
throws RemoteException { this.ctx = ctx; }
public void unsetSessionContext() throws RemoteException {
this.ctx = null;
}
public void ejbActivate() throws RemoteException {}
public void ejbPassivate() throws RemoteException {}
public void ejbRemove() throws RemoteException {}
public void ejbCreate() throws CreateException {}
/**
* Do the work, invoked from MDB listener. I.e, this bean will be
* invoked asynchronously by sending messages to the MDB listener.
*/
public void doWork(String work) {
System.out.println("WorkerBean doing work: " + work);
}
} // HelloWorkerBean
</pre></div><p>We will write the deployment descriptor for the bean later, since we will pack it with the listener.</p><p>The next step is to write the listener bean. Here we will add basically one thing: the ability to look up the worker bean and invoke it. We look up the bean through a JNDI reference defined via a <tt>ejb-ref</tt> element. This might be done in <tt>ejbCreate()</tt>:</p><div class="figure"><p><a name="d0e4956"></a><b>Figure 8.55. Looking up the worker bean</b></p><pre class="programlisting">
Context initCtx = new InitialContext();
workerHome =
(HelloWorkerHome)initCtx.lookup("java:comp/env/ejb/worker");
</pre></div><p>In the <tt>onMessage()</tt> method, we process the message. For example, here we could have sent an object of some kind, known to the listener. For simplicity, we choose to send the content of the <tt>TextMessage</tt> to the worker bean:</p><div class="figure"><p><a name="d0e4969"></a><b>Figure 8.56. Invoke worker bean from the <tt>onMessage()</tt> method</b></p><pre class="programlisting">
// 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());
}
</pre></div><p>Here it is the complete listing of the class:</p><div class="figure"><p><a name="d0e4979"></a><b>Figure 8.57. Listener bean, from <tt>HelloListener.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.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.TextMessage;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -