JMS - ActiveMQ简单实现

基本概念

JMS是java的消息服务,JMS的客户端之间可以通过JMS服务进行异步的消息传输。

简单的说就是 消息的生产者把消息发布出来,消息的消费者把获取订阅的消息。

生产者实现:

package com.ijiami.main.shiro.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JmsMessageProducer {
    
    private static final String HOST = "127.0.0.1";
    private static final String PORT = "61616";
    
    public static void main(String[] args) {
        ConnectionFactory factory = null;
        Connection connection = null ;
        Destination destination = null ;
        Session session = null ;
        MessageProducer producer = null;
        
        try {
            //1 、创建ConnecttionFactory.  
            factory = new ActiveMQConnectionFactory("tcp://"+HOST+":"+PORT);
            
            //2、 创建Connecttion.
            connection = factory.createConnection();
            connection.start();
            
            //3、 创建Session.
            session = connection.createSession(false, session.AUTO_ACKNOWLEDGE);
            
            //4 、创建Destination.
            destination = session.createQueue("testList");
            
            //5、 创建MessageProducer.
            producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            
            //6、 发送消息
            sentMessage(session, producer);
            session.commit();
            
        } catch (JMSException e) {
            e.printStackTrace();
        }finally{
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    public static void sentMessage(Session session , MessageProducer producer){
        
        try {
            for (int j = 0; j < 10; j++) {
                Message message = session.createTextMessage();
                message.setStringProperty("message","message :"+j);
                producer.send(message);
                Thread.sleep(2000);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

 

消费者实现:

package com.ijiami.main.shiro.test;

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.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JmsMessageConsumer implements MessageListener{

    private static final String HOST = "127.0.0.1";
    private static final String PORT = "61616";
    
    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof TextMessage) {
                TextMessage message2 = (TextMessage) message;
                String mes = message2.getStringProperty("message");
                System.out.println("receive message:"+mes);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    
    public void receive(){
        
        ConnectionFactory factory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageConsumer consumer = null;
        
        try {
            //1 、创建ConnecttionFactory.
            factory = new ActiveMQConnectionFactory("tcp://"+HOST+":"+PORT);
            
            //2、 创建Connecttion.
            connection = factory.createConnection();
            connection.start();
            
            //3、 创建Session.
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            //4 、创建Destination.
            destination = session.createQueue("testList");
            
            //5、 创建MessageConsumer.
            consumer = session.createConsumer(destination);
            
            //监听消息
            consumer.setMessageListener(this);
        } catch (JMSException e) {
            e.printStackTrace();
        }
        
    }
    public static void main(String[] args) {
        new JmsMessageConsumer().receive();
        
    }

}

 

posted @ 2016-02-17 16:09  phyxis_xu  阅读(219)  评论(0编辑  收藏  举报