码迷,mamicode.com
首页 > 编程语言 > 详细

activeMQ队列模式和主题模式的Java实现

时间:2017-07-23 19:40:28      阅读:471      评论:0      收藏:0      [点我收藏+]

标签:队列   des   producer   rod   sum   发送   cti   127.0.0.1   rri   

一、队列模式

生产者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;



public class AppProducer {
    public static final String url = "tcp://127.0.0.1:61616";
    public static final String queueName = "queue-test";

    public static void main(String[] args) throws JMSException{
        //1. 创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url);
        
        //2. 创建Connection
        Connection connection = connectionFactory.createConnection();
        
        //3. 启动链接
        connection.start();
        
        //4. 创建会话
        Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE);
        
        //5. 创建一个目标
        Destination destination = session.createQueue( queueName);
        
        //6. 创建一个生产者
        MessageProducer producer = session.createProducer( destination);
        
        for( int i=0; i<100; i++){
            //7. 创建消息
            TextMessage textMessage = session.createTextMessage( "test" + i);
            //8. 发布消息
            producer.send( textMessage);
            
            System.out.println( "发送消息" + textMessage.getText());
        }
        
        //9. 关闭链接
        connection.close();
    }

}

 

消费者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class AppConsumer {
    public static final String url = "tcp://127.0.0.1:61616";
    public static final String queueName = "queue-test";

    public static void main(String[] args) throws JMSException{
        //1. 创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url);
        
        //2. 创建Connection
        Connection connection = connectionFactory.createConnection();
        
        //3. 启动链接
        connection.start();
        
        //4. 创建会话
        Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE);
        
        //5. 创建一个目标
        Destination destination = session.createQueue( queueName);
        
        //6. 创建一个消费者
        MessageConsumer consumer = session.createConsumer( destination);
        
        //7. 创建一个监听器
        consumer.setMessageListener( new MessageListener() {
            @Override
            public void onMessage(Message message) {
                TextMessage textMessage = ( TextMessage) message;
                try{
                    System.out.println( "0接收消息" + textMessage.getText());
                }catch( JMSException e){
                    e.printStackTrace();
                }
                
            }
        });
        
        //8. 关闭链接
        //connection.close();
    }
}

 

二、主题模式

生产者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;



public class AppProducer {
    public static final String url = "tcp://127.0.0.1:61616";
    public static final String topicName = "topic-test";

    public static void main(String[] args) throws JMSException{
        //1. 创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url);
        
        //2. 创建Connection
        Connection connection = connectionFactory.createConnection();
        
        //3. 启动链接
        connection.start();
        
        //4. 创建会话
        Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE);
        
        //5. 创建一个目标
        Destination destination = session.createTopic( topicName);
        
        //6. 创建一个生产者
        MessageProducer producer = session.createProducer( destination);
        
        for( int i=0; i<100; i++){
            //7. 创建消息
            TextMessage textMessage = session.createTextMessage( "test" + i);
            //8. 发布消息
            producer.send( textMessage);
            
            System.out.println( "发送消息" + textMessage.getText());
        }
        
        //9. 关闭链接
        connection.close();
    }

}

消费者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class AppConsumer {
    public static final String url = "tcp://127.0.0.1:61616";
    public static final String topicName = "topic-test";

    public static void main(String[] args) throws JMSException{
        //1. 创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url);
        
        //2. 创建Connection
        Connection connection = connectionFactory.createConnection();
        
        //3. 启动链接
        connection.start();
        
        //4. 创建会话
        Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE);
        
        //5. 创建一个目标
        Destination destination = session.createTopic( topicName);
        
        //6. 创建一个消费者
        MessageConsumer consumer = session.createConsumer( destination);
        
        //7. 创建一个监听器
        consumer.setMessageListener( new MessageListener() {
            @Override
            public void onMessage(Message message) {
                TextMessage textMessage = ( TextMessage) message;
                try{
                    System.out.println( "0接收消息" + textMessage.getText());
                }catch( JMSException e){
                    e.printStackTrace();
                }
                
            }
        });
        
        //8. 关闭链接
        //connection.close();
    }
}

 

activeMQ队列模式和主题模式的Java实现

标签:队列   des   producer   rod   sum   发送   cti   127.0.0.1   rri   

原文地址:http://www.cnblogs.com/aston/p/7225389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!