ActiveMQ消息队列学习

ActiveMQ消息队列是apache下面的开源的消息中间件,使用方便扩展性好。下面我就我学习mq跟大家分享一下:

  1. ActiveMQ环境的配置
    下载ActiveMQ:http://activemq.apache.org/
    解压缩apache-activemq-5.9.1-bin.zip,然后双击apache-activemq-5.9.1\bin\win64\activemq.bat运行ActiveMQ程序。
  2. 启动ActiveMQ以后,登陆:http://localhost:8161/admin/ 用户名和密码都是admin
  3. 打开eclipse创建一个Java项目
    项目图片

     1. 创建一个消息生产者
    
package com.java.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;

public class MessageProvider {

    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;//创建连接工厂
        Connection connection = null;//创建连接
        Session session;//创建session 消息的线程
        Destination destination;//创建消息发送目的地
        MessageProducer messageProducer;//消息生产者
        try {
            connectionFactory = 
                            new ActiveMQConnectionFactory(MessageProvider.USERNAME, MessageProvider.PASSWORD, MessageProvider.BROKERURL);
            connection = connectionFactory.createConnection();
            connection.start();
            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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void sendMessage(Session session , MessageProducer messageProducer)throws JMSException{
        for(int i = 0 ;i<10;i++){
            TextMessage message = session.createTextMessage("activeMQ发送的消息:"+i);
            messageProducer.send(message);
            System.out.println("发送消息:activeMQ发送的消息:"+i);
        }
    }
}

运行的效果如下:

log4j:WARN No appenders could be found for logger (org.apache.activemq.thread.TaskRunnerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
发送消息:activeMQ发送的消息:0
发送消息:activeMQ发送的消息:1
发送消息:activeMQ发送的消息:2
发送消息:activeMQ发送的消息:3
发送消息:activeMQ发送的消息:4
发送消息:activeMQ发送的消息:5
发送消息:activeMQ发送的消息:6
发送消息:activeMQ发送的消息:7
发送消息:activeMQ发送的消息:8
发送消息:activeMQ发送的消息:9

     2. 创建一个消息消费者
package com.java.activemq;

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

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

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;//创建连接工厂
            Connection connection = null;//创建连接
            Session session;//创建session 消息的线程
            Destination destination;//创建消息发送目的地
            MessageConsumer messageConsumer;//消息生产者
            try {
                connectionFactory = 
                                new ActiveMQConnectionFactory(
                                        JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKERURL);
                connection = connectionFactory.createConnection();
                connection.start();
                session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
                destination = session.createQueue("FirstQueue1");
                messageConsumer = session.createConsumer(destination);
                while(true){
                    TextMessage 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) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    }
}

执行的效果是:

log4j:WARN No appenders could be found for logger (org.apache.activemq.thread.TaskRunnerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
接收到的消息为:activeMQ发送的消息:0
接收到的消息为:activeMQ发送的消息:1
接收到的消息为:activeMQ发送的消息:2
接收到的消息为:activeMQ发送的消息:3
接收到的消息为:activeMQ发送的消息:4
接收到的消息为:activeMQ发送的消息:5
接收到的消息为:activeMQ发送的消息:6
接收到的消息为:activeMQ发送的消息:7
接收到的消息为:activeMQ发送的消息:8
接收到的消息为:activeMQ发送的消息:9

客户端效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值