使用EJB_JMS
时间:2007-01-01 来源:liulibo
1.Queue
QueueConnectionFactory factory = (QueueConnectionFactory)ic.lookup("factory");
QueueConnection conn = factory.createQueueConnection();
QueueSession session = conn.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue)ic.lookup("queue");
创建发送队列
QueueSender sender = session.createSender(queue);
创建消息
TextMessage message = session.createTextMessage("zhanghao");
发送消息
sender.send(message);
创建接收队列
QueueReceiver receiver = session.createReceiver(queue);
开始接收消息
conn.start();
接收消息
TextMessage mes = (TextMessage)receiver.receive();
打印消息
System.out.println(mes.getText());
2.Topic
MessageListener接口public void onMessage(Message message)方法监听消息
TopicConnectionFactory factory = (TopicConnectionFactory)ic.lookup("factory");
TopicConnection conn = factory.createTopicConnection();
TopicSession session = conn.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic)ic.lookup("topic");
创建发布主题
TopicPublisher pub = session.createPublisher(topic);
创建消息
TextMessage message = session.createTextMessage("zhanghao");
发布消息
pub.publish(message);
创建订户主题
TopicSubscriber sub = session.createSubscriber(topic);
开始接收消息
conn.start();
接收消息
TextMessage mes = (TextMessage)receiver.receive();
3.Durable_Topic
设置ID
conn.setClientID(name);
创建持久订户主题
TopicSubscriber sub = session.createDurableSubscriber(topic,"abc");
开始接收消息
conn.start();
接收消息
TextMessage mes = (TextMessage)receiver.receive();