日期:2014-05-19 浏览次数:20553 次
In the past, I posted a few examples of implementing Messaging using
J2EE and Spring. In this post, I will give an example of how to
implement Message Driven beans using Java EE 5. I used Eclipse 3.2 and
Glassfish for this example. Follow these steps to run the example:
java -Xmx256m -jar glassfish-installer-version-build.jar
ant -f setup-cluster.xml
package jms;
import javax.annotation.Resource;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(mappedName = "jms/testQueue")
public class Messaging3Mdb implements MessageListener {
@Resource
private MessageDrivenContext mdc
;
public Messaging3Mdb() {
}
public void onMessage(Message inMessage) {
TextMessage msg = null;
try {
msg = (TextMessage) inMessage;
System.out.println("Message received : " + msg.getText());
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();
}
}
}
Messaging3Mdb.java
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Messaging3Web</display-name>
<servlet>
<description></description>
<display-name>MessagingClient</display-name>
<servlet-name>MessagingClient</servlet-name>
<servlet-class>servlets.MessagingClient</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessagingClient</servlet-name>
<url-pattern>/MessagingClient</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file