消息驱动的bean(MDB)为实现异步通信提供了一种比使用直接的Java消息服务(JMS)更简单的方法。MDB用于接收异步JMS消 息。容器处理JMS队列和主题所需的大部分设置进程。它把所有消息发送给相关的MDB。MDB允许J2EE应用程序发送异步消息,随后这些消息由应用程序 来处理。要把bean指定为MDB,需要实现javax.jms.MessageListener接口,并且用@MessageDriven注释该 bean。
概念须知:
●队列(Queue),用于保存从一个JMS客户端发往另一个JMS客户端的消息,这种消息模型被称为点对点(point-to-point);
●主题(Topic),用于保存从许多潜在的JMS客户端发往多个潜在的JMS客户端的消息,这种消息模型被称为发布/订阅(publish/subscribe)模式;
●连接工厂(connection factory),用于创建一个到JMS提供者的连接
●目的地(destination),可以是javax.jsm.Queue和javax.jsm.Topic类型
驱动bean实例:
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
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/MyDB", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
public class MyDB implements MessageListener {
@Resource
private MessageDrivenContext mdc;
@SuppressWarnings("null")
public void onMessage(Message message) {
TextMessage msg = null;
try {
if(message instanceof TextMessage){
System.out.println("消息驱动bean接收到的消息"+msg.getText());
}else{
System.out.println("消息类型不正确"+message.getClass().getName());
}
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();
}
}
}
打成jar包后部署到weblogic上
然后配置好JMS,打开jndi如果配置好可以看到如下:
消息发送实例:
package myMDBClient;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MSGQueueSender {
private QueueSender sender;
private TextMessage msg;
private QueueConnection qCon;
public MSGQueueSender(String[] argv) throws NamingException, JMSException{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx = new InitialContext(p);
QueueConnectionFactory qConFactory = (QueueConnectionFactory) ctx.lookup("mConnectionFactory");
Queue messageQueue = (Queue) ctx.lookup("mqueue");
qCon = qConFactory.createQueueConnection();
QueueSession session = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
sender = session.createSender(messageQueue);
msg = session.createTextMessage();
}
public void runClient(String str) throws JMSException{
msg.setText("hello every one");
sender.send(msg);
System.out.println("消息1:hello every one");
msg.setText("welocme to JMS");
sender.send(msg);
System.out.println("消息2:welocme to JMS");
msg.setText("hello every one");
sender.send(msg);
System.out.println("消息3:"+str);
}
public static void main(String[] argv) {
try {
MSGQueueSender mqs = new MSGQueueSender(argv);
mqs.runClient("thank you!");
System.out.println("success");
mqs.qCon.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
weblogic控制台可以监控到消息来到:
myeclipse控制台输出如下: