activemq P2P和sub/pub
1.P2P
生产者
public classJmsActiveMqProduct {
private static final String USER = ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BORKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;
private static final int SENDNUM = 10;
public static voidmain(String[] args) {
ConnectionFactory connectionFactory; //链接工厂,用于创建connection
Connection connection = null; //链接用于发送接收消息的通道
Session session;//会话接收或发送消息线程
Destination destination; //消息生产者发送消息(发送的位置),消费者消费消息(从何处获取)
MessageProducer messageProducer;
try {
connectionFactory = newActiveMQConnectionFactory(JmsActiveMq.USER,JmsActiveMq.PASSWORD, JmsActiveMq.BORKER_URL);
connection = connectionFactory.createConnection();
connection.start();
session =connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //支持事务 自动持久化
destination = session.createQueue("ActiveMQ_Producter");
messageProducer =session.createProducer(destination);
sendMessage(session,messageProducer);
session.commit();
} catch(JMSException e) {
e.printStackTrace();
}finally{
if(connection!=null){
try {
connection.close();
} catch(JMSException e) {
e.printStackTrace();
}
}
}
}
public static voidsendMessage(Session session,MessageProducer messageProducer)
throws JMSException{
for(inti=0;i<JmsActiveMq.SENDNUM;i++){
TextMessagetxtMessage = session.createTextMessage("ActiveMQ_Producter发送消息"+i);
System.out.println("发送消息"+i);
messageProducer.send(txtMessage);//发送消息
}
}
}
消费者
public class JmsActiveMqCustmer{
private static final String USER =ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD =ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BORKER_URL =ActiveMQConnection.DEFAULT_BROKER_URL;
private static final long RECEIVENUM = 100000;
public static voidmain(String[] args) {
ConnectionFactory connectionFactory;
Connection connection;
Session session;
Destination destination;
MessageConsumer messageConsumer;
try {
connectionFactory= new ActiveMQConnectionFactory(JmsCustmer.USER,JmsCustmer.PASSWORD, JmsCustmer.BORKER_URL);
connection= connectionFactory.createConnection();
connection.start();
session= connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
destination= session.createQueue("ActiveMQ_Producter");
messageConsumer= session.createConsumer(destination);
while(true){
TextMessage mesg=(TextMessage) messageConsumer.receive(JmsCustmer.RECEIVENUM);
if(mesg!=null){
System.out.println("接收消息"+mesg.getText());
}else{
break;
}
}
} catch(JMSException e) {
e.printStackTrace();
}
}
}
使用监听器(异步)
public class Listenter implementsMessageListener {
@Override
public voidonMessage(Message message) {
// TODOAuto-generated method stub
TextMessagetextMessage=(TextMessage)message;
try {
System.out.println("收到的消息=====:"+textMessage.getText());
} catch(JMSException e) {
e.printStackTrace();
}
}
}
异步消费者中添加
messageConsumer.setMessageListener(newListenter());
2.pub/sub
生产者
public class HelloTopicProducer {
public void send(String msg) {
// 生产者的主要流程
Connection connection = null;
try {
// 1.初始化connection工厂,使用默认的URL
//failover://tcp://localhost:61616
ConnectionFactoryconnectionFactory = new ActiveMQConnectionFactory();
// 2.创建Connection
connection =connectionFactory.createConnection();
// 3.打开连接
connection.start();
// 4.创建Session,(是否支持事务)
Session session =connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5.创建消息目标
Destination destination_send =session.createTopic(MQConstants.DESTINATION_SEND);
// 6.创建生产者
MessageProducerproducer = session.createProducer(destination_send);
// 7.配置消息是否持久化
/*
* DeliverMode有2种方式:
* public interface DeliveryMode { static finalint NON_PERSISTENT =
* 1;//不持久化:服务器重启之后,消息销毁
* static final int PERSISTENT = 2;//持久化:服务器重启之后,该消息仍存在 }
*/
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 8.初始化要发送的消息
TextMessage message= session.createTextMessage(msg);
// 9.发送消息
producer.send(message);
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
newHelloTopicProducer().send("我来试一试发布/订阅...");
}
}
消费者
public classHelloTopicConsumer implements MessageListener {
@Override
//消费者继承MessageListener内部定义监听器
public void onMessage(Message message){
if (message instanceofTextMessage) {
TextMessage txtMsg =(TextMessage) message;
try {
System.out.println("哈,我接收到了消息:" + txtMsg.getText());
} catch(JMSException e) {
e.printStackTrace();
}
}
}
public void receive() {
// 消费者的主要流程
Connection connection = null;
try {
// 1.初始化connection工厂
ConnectionFactoryconnectionFactory = new ActiveMQConnectionFactory();
// 2.创建Connection
connection =connectionFactory.createConnection();
// 3.打开连接
connection.start();
// 4.创建session
Session session =connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5.创建消息目标
Destinationdestination = session.createTopic(MQConstants.DESTINATION_SEND);
// 6.创建消费者
MessageConsumerconsumer = session.createConsumer(destination);
// 7.配置监听
consumer.setMessageListener(newHelloTopicConsumer());
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
//测试
newHelloTopicConsumer().receive();
}
}
测试。。。