activemq实现简单的消息传递,
消息传递有两种消息处理模式:点对点(PTP) 和订阅/发布(sub/pub)
PTP的目地地是QUEUE, sub/pub的目的地是TOPIC
Topic和queue的最大区别在于topic是以广播的形式,通知所有在线监听的客户端有新的消息,没有监听的客户端将收不到消息;而queue则是以点对点的形式通知多个处于监听状态的客户端中的一个。
下面实现消息生产者代码(sub/pub)
package com.panther.dong.activemq.pubsub.consumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.*;
/**
* Created by panther on 15-8-18.
*/
public class ConsumerMessage {
private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerMessage.class);
private static final String url = "tcp://192.168.236.150:61616?connectionTimeout=0&wireFormat.maxInactivityDuration=0";
private Session session = null;
private Connection connection = null;
private MessageConsumer messageConsumer = null;
private static ConsumerMessage instance = new ConsumerMessage();
public static ConsumerMessage getInstance() {
return instance;
}
public ConsumerMessage() {
this.init();
}
public void init() {
try {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(url);
if (activeMQConnectionFactory != null) {
connection = (Connection) activeMQConnectionFactory.createConnection();
connection.start();
}
if (connection != null) {
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("panther.test");
messageConsumer = session.createConsumer(destination);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getMessage() {
String result = null;
try {
Message message = messageConsumer.receive(1000);
if (message instanceof TextMessage) {
result = ((TextMessage) message).getText();
}
} catch (JMSException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
for (int i = 0; i < Integer.MAX_VALUE; ++i) {
String result = ConsumerMessage.getInstance().getMessage();
System.out.println("----+++--------" + result);
}
}
}
消息消费者代码(pub/sub)
package com.panther.dong.activemq.pubsub.consumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.*;
/**
* Created by panther on 15-8-18.
*/
public class ConsumerMessage {
private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerMessage.class);
private static final String url = "tcp://192.168.236.150:61616?connectionTimeout=0&wireFormat.maxInactivityDuration=0";
private Session session = null;
private Connection connection = null;
private MessageConsumer messageConsumer = null;
private static ConsumerMessage instance = new ConsumerMessage();
public static ConsumerMessage getInstance() {
return instance;
}
public ConsumerMessage() {
this.init();
}
public void init() {
try {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(url);
if (activeMQConnectionFactory != null) {
connection = (Connection) activeMQConnectionFactory.createConnection();
connection.start();
}
if (connection != null) {
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("panther.test");
messageConsumer = session.createConsumer(destination);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getMessage() {
String result = null;
try {
Message message = messageConsumer.receive(1000);
if (message instanceof TextMessage) {
result = ((TextMessage) message).getText();
}
} catch (JMSException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
for (int i = 0; i < Integer.MAX_VALUE; ++i) {
String result = ConsumerMessage.getInstance().getMessage();
System.out.println("----+++--------" + result);
}
}
}
运行结果:
生产者生产消息,消费者消费消息