Apache Active MQ 之Topic

Apache Active MQ 之Topic:

Pre-Condition:安装好Apache ActiveMQ并启动服务

消费者:

package com.wx.jms.topic;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

/**
* @Class name: Topic_Test.java
*
* Short description on the purpose of the program.
*
* @author: wangxiang
* @modified: Mar 22, 2011
*
*/

public class Topic_Test {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection conn = factory.createConnection();
conn.start();

Topic topic = new ActiveMQTopic("mqtopic");
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer1 = session.createConsumer(topic);
consumer1.setMessageListener(new MessageListener(){
public void onMessage(Message msg) {
try {
System.out.println("consumer1:" + ((TextMessage)msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

MessageConsumer consumer2 = session.createConsumer(topic);
consumer2.setMessageListener(new MessageListener(){
public void onMessage(Message msg) {
try {
System.out.println("consumer2:" + ((TextMessage)msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

}

}


生产者:

package com.wx.jms.topic;

import javax.jms.Connection;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

/**
* @Class name: TopicProducer_Main.java
*
* Short description on the purpose of the program.
*
* @author: wangxiang
* @modified: Mar 22, 2011
*
*/

public class TopicProducer_Main {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
"tcp://localhost:61616");
Connection conn = factory.createConnection();
conn.start();

Topic topic = new ActiveMQTopic("mqtopic");
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(topic);
for (int i = 1; i < 12; i++) {
producer.send(session.createTextMessage("msg " + i));
}
conn.stop();
}

}


结果:
[b]consumer1:msg 1
consumer2:msg 1
consumer1:msg 2
consumer2:msg 2
consumer1:msg 3
consumer2:msg 3
consumer1:msg 4
consumer2:msg 4
consumer1:msg 5
consumer2:msg 5
consumer1:msg 6
consumer2:msg 6
consumer1:msg 7
consumer2:msg 7
consumer1:msg 8
consumer2:msg 8
consumer1:msg 9
consumer2:msg 9[/b]

你可能感兴趣的:(Java)