MDB其实就是JMS消息监听器,它实现了javax.jms.MessageListener接口。
P2P的消息模型(Queue):每次只有一个MDB bean被调用。
Server:
client:
Pub/Sub模型(Topic):所有destination名字相同的MDB bean都被调用。
Server:
Client:
P2P的消息模型(Queue):每次只有一个MDB bean被调用。
Server:
package com.kevin.ejb;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
// 这个annotation是定义MDB
@MessageDriven(
// 注册一个Queue类型的destination
activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType" ,propertyValue = "javax.jms.Queue"),
// destination名字
@ActivationConfigProperty(propertyName = "destination" ,propertyValue = "queue/myqueue")
}
)
public class MyQueueMDBBean implements MessageListener {
public void onMessage(Message message) {
TextMessage msg = (TextMessage)message;
try {
System.out.println("MyQueueMDBBean invoked. the message is: " + msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
client:
package com.kevin.ejb;
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.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MyQueueMDBBeanClient {
public static void main(String[] args) throws NamingException, JMSException {
InitialContext context = new InitialContext();
// 获得QueueConnectionFactory对象
QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
// 获得QueueConnection对象
QueueConnection connection = factory.createQueueConnection();
// 获得QueueSession对象
QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
// 获得destination对象
Queue queue = (Queue)context.lookup("queue/myqueue");
// 创建文本消息
TextMessage msg = session.createTextMessage("Hello, Kevin.");
// 创建一个发送者
QueueSender sender = session.createSender(queue);
// 发送消息
sender.send(msg);
session.close();
connection.close();
System.out.println("message has been sended successfully!");
}
}
Pub/Sub模型(Topic):所有destination名字相同的MDB bean都被调用。
Server:
package com.kevin.ejb;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/mytopic")
}
)
public class MyTopicMDBBean implements MessageListener {
public void onMessage(Message message) {
TextMessage msg = (TextMessage)message;
try {
System.out.println("MyTopicMDBBean invoked. the message is: " + msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Client:
package com.kevin.ejb;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MyTopicMDBBeanClient {
public static void main(String[] args) throws NamingException, JMSException {
InitialContext context = new InitialContext();
// 创建TopicConnectionFactory对象
TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup("ConnectionFactory");
// 创建TopicConnection对象
TopicConnection connection = factory.createTopicConnection();
// 创建TopicSession对象
TopicSession session = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
// 获取destination对象
Topic topic = (Topic)context.lookup("topic/mytopic");
// 创建文本消息对象
TextMessage msg = session.createTextMessage("Hello, Cissy.");
// 创建TopicPublisher对象
TopicPublisher publisher = session.createPublisher(topic);
// 发布消息
publisher.publish(msg);
session.close();
connection.close();
System.out.println("message has been published successfully!");
}
}