ejb3: message drive bean(MDB)示例

时间:2023-01-16 05:56:02

上一篇已经知道了JMS的基本操作,今天来看一下ejb3中的一种重要bean:Message Drive Bean(mdb)

如果要不断监听一个队列中的消息,通常我们需要写一个监听程序,这需要一定的开发量,而且如果要实现高并发处理,也不易扩展,而MDB则自动实现了该功能,简单点讲,MDB的应用部署到jboss后,能自动监听目标队列,一旦有消息接收,会触发onMessage事件,开发人员可以在该事件处理中扩展自己的业务逻辑.

一、定义一个MDB

 package mdb;

 import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; import util.LoggerUtil; @MessageDriven(name = "HelloWorldQueueMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/queue/mytest"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class HelloWorldMDB implements MessageListener { @Override
public void onMessage(Message msg) {
TextMessage txtMsg = null;
try {
if (msg instanceof TextMessage) {
txtMsg = (TextMessage) msg;
String msgContent = txtMsg.getText();
LoggerUtil.info("Received Message from queue: " + msgContent);
} else {
LoggerUtil.warning("Message of wrong type: "
+ txtMsg.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
} } }

HelloWorldMDB

注意该类上的注解,它表明了要监听哪个Queue(可以参考上一篇的内容,先在jboss中建好该queue),其它没什么特别的,把它放一个dynamic web中,打成war包部署到jboss上,为演示效果,部署后,先不启动该应用

附:pom.xml文件的内容

 <?xml version="1.0" encoding="UTF-8"?>

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cnblogs</groupId>
<artifactId>helloworld-mdb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>helloworld-mdb</name> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.bom</groupId>
<artifactId>jboss-javaee-6.0-with-tools</artifactId>
<version>1.0.7.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.jboss.spec.javax.jms</groupId>
<artifactId>jboss-jms-api_1.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </project>

pom.xml

二、测试验证

a) 可以参考上一篇JMS的内容,另建一个常规的project,向该队列发送消息(注意:仅发送,不要接收,否则消息被收走了,MDB就收不到消息了)

 package jms;

 import java.util.Hashtable;

 import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException; public class App { public static void main(String[] args) throws NamingException, JMSException { final String lOOKUP_CONNECTION_FACTORY_NAME = "lookup.connectionfactory.name";
final String lOOKUP_DESTINATION_NAME = "lookup.destination.name"; ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
Destination destination = null;
TextMessage message = null;
Context context = null; try {
// 创建上下文(默认会从应用的classpath下加载jndi.properties做为环境参数)
context = new InitialContext(); // 把环境参数取出来,后面会用到
Hashtable<String, String> env = (Hashtable<String, String>) context
.getEnvironment(); // 查找连接工厂
connectionFactory = (ConnectionFactory) context.lookup(env
.get(lOOKUP_CONNECTION_FACTORY_NAME)); // 查找目标队列
destination = (Destination) context.lookup(env
.get(lOOKUP_DESTINATION_NAME)); // 创建连接
connection = connectionFactory.createConnection(
env.get(Context.SECURITY_PRINCIPAL),
env.get(Context.SECURITY_CREDENTIALS)); // 创建会话
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建生产者(即发送者)
producer = session.createProducer(destination); // 创建消费者(即接收者)
consumer = session.createConsumer(destination); // 开始连接
connection.start(); // 发送消息 message = session.createTextMessage("HELLO,I AM GLAD TO SEE YOU!"); producer.send(message); System.out.println("发送成功!"); } catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
} finally {
// 释放资源
if (context != null) {
context.close();
} if (connection != null) {
connection.close();
} }
} }

Send Message

b) 然后在jboss中,再把该应用启用起来,观察console窗口的输出:

ejb3: message drive bean(MDB)示例

三、xml方式配置MDB

刚才我们是用注解方式来配置MDB的,这种方式不需要xml配置文件,十分方便,但是也有缺点,配置与代码紧耦合,如果以后要修改queue名称,就得改代码,重新编译,所以jboss也提供了xml配置方式

方法:在META-INF(非web项目)或WEB-INF(web项目)放置一个名为jboss-ejb3.xml(这是固定名称,不要修改!)

内容参考下面这样:

 <?xml version="1.0" encoding="UTF-8"?>
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="urn:clustering:1.0"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
version="3.1" impl-version="2.0">
<enterprise-beans>
<message-driven>
<ejb-name>HelloWorldQueueMDB</ejb-name>
<ejb-class>mdb.HelloWorldMDB</ejb-class>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>jms/queue/mytest</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>acknowledgeMode</activation-config-property-name>
<activation-config-property-value>Auto-acknowledge</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
<assembly-descriptor>
<c:clustering>
<ejb-name>DDBasedClusteredSFSB</ejb-name>
<c:clustered>true</c:clustered>
</c:clustering>
</assembly-descriptor>
</jboss:ejb-jar>

jboss-ejb3.xml

然后把HelloWorldQueueMDB类上的那一堆注解全注释掉,再跑下,顺利的话,也同样可以接收消息

示例源代码下载:mdb-sample.zip