发送端:
- package com.ccl.app;
-
- import javax.jms.Destination;
- import javax.jms.MessageProducer;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import javax.jms.TopicConnection;
- import javax.jms.TopicConnectionFactory;
- import javax.jms.TopicSession;
- import javax.naming.InitialContext;
-
- public class SendQueue {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
-
- try {
-
- InitialContext ctx = new InitialContext();
-
- TopicConnectionFactory factory = (TopicConnectionFactory) ctx
- .lookup("TopicConnectionFactory");
-
- TopicConnection conn = factory.createTopicConnection();
-
- Session session = conn.createSession(false,
- TopicSession.AUTO_ACKNOWLEDGE);/* 建立不需要事务的并且能够自动确认已接收的会话 */
-
- Destination destination = (Destination) ctx
- .lookup("queue/jboossTopic");
-
- MessageProducer producer = session.createProducer(destination);
-
- TextMessage msg = session
- .createTextMessage("hello java Message service of topic.");
-
- producer.send(msg);
-
- System.out.println(msg);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- }
接收端:
- package com.ccl.app.client;
-
- 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 = "queue/jboossTopic")
-
- })
- public class MessageDrivenBean implements MessageListener {
-
- @Override
- public void onMessage(Message mess) {
-
- TextMessage msg = (TextMessage)mess;
-
- try {
-
- System.out.println(msg.getText());
-
- } catch (JMSException e) {
- e.printStackTrace();
- }
-
- }
-
- }
作者:chengchanglun 发表于2012-4-9 14:56:42
原文链接