MessagePublisher类
package activemq.yang;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessagePublisher implements Runnable {
private String url;
private String user;
private String password;
private String topicName;
public MessagePublisher(String topicName, String url, String user,
String password) {
this.url = url;
this.user = user;
this.password = password;
this.topicName = topicName;
}
@Override
public void run() {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
Connection connection = connectionFactory.createConnection();
connection.start();
System.out.println(Thread.currentThread().getName() + "开启");
//创建Topic
//Topic topic = new ActiveMQTopic(this.topicName);
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(this.topicName);
MessageProducer sendPublisher = session.createProducer(topic);
sendPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for(int i = 0; i < 5; i++){
String text = new Date() + " 现在发送是第" + (i+1) + "条消息";
sendPublisher.send(session.createTextMessage(text));
System.out.println("send message :" + text);
// Thread.sleep(1000);
}
sendPublisher.close();
session.close();
connection.close();
// System.out.println("发布消息线程结束******************************");
} catch (JMSException e) {
e.printStackTrace();
}
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTopic() {
return topicName;
}
}
MessageSubscriber类
package activemq.yang;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageSubscriber implements Runnable {
private String url;
private String user;
private String password;
private String topicName;
public MessageSubscriber(String topicName, String url, String user,
String password) {
this.url = url;
this.user = user;
this.password = password;
this.topicName = topicName;
}
@Override
public void run() {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建Topic
Topic topic = session.createTopic(this.topicName);
MessageConsumer subscriber = session.createConsumer(topic);
subscriber.setMessageListener(new TextListener());
connection.start();
System.out.println(Thread.currentThread().getName() + "开启");
// subscriber.close();
// session.close();
// connection.close();
}
catch (JMSException e) {
e.printStackTrace();
}
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTopic() {
return topicName;
}
}
TextListener类,用来监听
package activemq.yang;
import javax.jms.*;
/**
* Text消息监听
*
* @author XXXX<br> *
*/
public class TextListener implements MessageListener {
/**
* Casts the message to a TextMessage and displays its text.
*
* @param message
* the incoming message
*/
public void onMessage(Message message) {
TextMessage msg = null;
try {
if (message instanceof TextMessage) {
msg = (TextMessage) message;
System.out.println(Thread.currentThread().getName() +"Recevie message: " + msg.getText());
} else {
System.out.println("Message of wrong type: "
+ message.getClass().getName());
}
} catch (JMSException e) {
System.out.println("JMSException in onMessage(): " + e.toString());
} catch (Throwable t) {
System.out.println("Exception in onMessage():" + t.getMessage());
}
}
}
测试类MyActiveMQDemo
package activemq.yang;
import javax.jms.JMSException;
public class MyActiveMQDemo {
public static void main(String[] args) throws InterruptedException, JMSException {
String url = "tcp://localhost:61616";
String user = null;
String password = null;
String topic = "TestTopic";
new Thread(new MessagePublisher(topic,url,user,password), "Publish").start();
new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber1").start();
new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber2").start();
// new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber3").start();
// new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber4").start();
// new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber5").start();
}
}
执行程序,结果如下:
Publish开启
Name-Subscriber1开启
Name-Subscriber2开启
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息
ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息
ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息
ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息
监听显示: