开始学习 ActiveMQ(1)_发送消息
1、创建消息生产者(JMSProducer)

package com.liwei.activemq;

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.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息生产者
 * @author Administrator
 *
 */
public class JMSProducer {
    // 默认的连接用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    // 默认的密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    // 默认的连接地址
    private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
    // 发送消息的数量
    private static final int SENDNUM = 10 ;

    public static void main(String[] args) {
        // 连接工厂
        ConnectionFactory connectionFactory = null;
        // 连接
        Connection connection = null;
        // 会话:接受或者发送消息的线程
        Session session =null;
        // 消息的目的地
        Destination destination = null;
        // 消息的生产者
        MessageProducer messageProducer = null;

        // 实例化连接工厂
        connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKERURL);

        try {
            // 通过连接工厂获取连接 
            connection = connectionFactory.createConnection();
            // 启动连接
            connection.start();
            // 创建 Session ,开启事务
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 创建消息队列
            destination = session.createQueue("FirstQueue1");
            // 创建消息生产者
            messageProducer = session.createProducer(destination);
            // 发送消息
            sendMessage(session, messageProducer);
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        } finally{
            try {
                if(connection!=null){
                    connection.close();
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }




    }



    /**
     * 创建一个私有的方法,用于发送消息
     * @param session
     * @param messageProducer
     * @throws JMSException 
     */
    private static void sendMessage(Session session,MessageProducer messageProducer) throws JMSException{
        TextMessage message = null;
        for (int i = 0; i < JMSProducer.SENDNUM; i++) {
            message = session.createTextMessage("ActiveMQ 发送的消息:" + i);
            System.out.println("发送消息:" + "ActiveMQ 发送的消息:" + i);
            messageProducer.send(message);
        }
    }















}

2、创建消息消费者(使用直接 receive 的方式)
注意:消费者的消费线程会阻塞,所以不要使用 finally 块。
使用直接Receive 方式。
(1)Session.AUTO_ACKNOWLEDGE。当客户成功的从receive 方法返回的时候,或者从MessageListener.onMessage
方法成功返回的时候,会话自动确认客户收到的消息。
(2)Session.CLIENT_ACKNOWLEDGE。客户通过消息的acknowledge 方法确认消息。需要注意的是,在这种模
式中,确认是在会话层上进行:确认一个被消费的消息将自动确认所有已被会话消费的消息。例如,如果一
个消息消费者消费了10 个消息,然后确认第5 个消息,那么所有10 个消息都被确认。
(3)Session.DUPS_ACKNOWLEDGE。该选择只是会话迟钝第确认消息的提交。如果JMS provider 失败,那么可
能会导致一些重复的消息。如果是重复的消息,那么JMS provider 必须把消息头的JMSRedelivered 字段设置
为true。

package com.liwei.activemq;

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

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息的消费者
 * @author Administrator
 *
 */
public class JMSConsumer {
    // 默认的连接用户名
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        // 默认的密码
        private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
        // 默认的连接地址
        private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
        public static void main(String[] args) {
            // 连接工厂
            ConnectionFactory connectionFactory = null;
            // 连接
            Connection connection = null;
            // 会话:接受或者发送消息的线程
            Session session =null;
            // 消息的目的地
            Destination destination = null;
            // 消息的生产者
            MessageConsumer messageConsumer = null;


            // 实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);

            try {
                // 通过连接工厂获取连接 
                connection = connectionFactory.createConnection();
                // 启动连接
                connection.start();
                // 创建 Session ,开启事务
                session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
                // 创建消息队列(和消息生产者方一定要一致)
                destination = session.createQueue("FirstQueue1");

                /**
                 * 以上都和创建消息生产者一样
                 */
                // 获得消息消费者
                messageConsumer = session.createConsumer(destination);

                TextMessage textMessage =null;
                while(true){
                    textMessage = (TextMessage) messageConsumer.receive(100000);// 以毫秒为单位
                    if(textMessage!=null){
                        System.out.println("接受到的消息是:" + textMessage.getText());
                    }else{
                        break;
                    }
                }

                // 这里发生了阻塞
            } catch (JMSException e) {
                e.printStackTrace();
            } /*finally{
                try {
                    if(connection!=null){
                        connection.close();
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }*/
        }
}

我们查看控制台:
开始学习 ActiveMQ(1)_ActiveMQ_02

3、使用监听的方式(最佳实践)
(1)创建一个实现了 MessageListener 接口的类

package com.liwei.activemq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * 使用Listener 监听方式是最佳实践
 * 消息监听
 * @author Administrator
 *
 */
public class Listener implements MessageListener{

    @Override
    public void onMessage(Message message) {
        /*TextMessage textMessage = (TextMessage)message;
        System.out.println("收到的消息是:" + textMessage);*/
        try {
            System.out.println("收到的消息:"+((TextMessage)message).getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}

我们再查看控制台:
开始学习 ActiveMQ(1)_ActiveMQ_03
(2)使用监听方式的消费者接收消息的类

package com.liwei.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息的消费者
 * @author Administrator
 *
 */
public class JMSConsumer2 {
    // 默认的连接用户名
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        // 默认的密码
        private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
        // 默认的连接地址
        private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
        public static void main(String[] args) {

            // 连接工厂
            ConnectionFactory connectionFactory = null;
            // 连接
            Connection connection = null;
            // 会话:接受或者发送消息的线程
            Session session =null;
            // 消息的目的地
            Destination destination = null;
            // 消息的生产者
            MessageConsumer messageConsumer = null;

            // 实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD, JMSConsumer2.BROKERURL);
            try {
                // 通过连接工厂获取连接 
                connection = connectionFactory.createConnection();
                // 启动连接
                connection.start();
                // 创建 Session ,开启事务
                session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
                // 创建消息队列(和消息生产者方一定要一致)
                destination = session.createQueue("FirstQueue1");
                /**
                 * 以上都和创建消息生产者一样
                 */
                // 获得消息消费者
                messageConsumer = session.createConsumer(destination);
                // 注册消息监听
                messageConsumer.setMessageListener(new Listener());
            } catch (JMSException e) {
                e.printStackTrace();
            } /*finally{
                try {
                    if(connection!=null){
                        connection.close();
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }*/
        }
}