JMS-ActiveMQ学习笔记

1.下载安装

ActiveMQ官网下载地址: http://activemq.apache.org/download.html
官网提供了Windows,Unix版本的安装包,下载对应安装包即可。
2.启动ActiveMQ服务

以Windows为例,进入解压后的ActiveMQ文件夹的bin目录下, 这里写图片描述
选择相应的版本,进入启动activemq.bat
这里写图片描述

ActiveMQ内置了jetty服务器,提供一个用于监控ActiveMQ的应用
admin:http://127.0.0.1:8161/admin/
用户名:admin
密码:admin
进入网址,登录会看到如下界面:
这里写图片描述

3.编写测试程序

ActiveMQ有两种消息模式
1.点对点的消息模式,即一个生产者发出的一条消息只能被一个消费者所接受。
   点对点模式生产者只负责把消息发送至ActiveMQ服务器上,此时不管哪一个消费者接受,此时,先连上服务器的便会限接收到消息
2.订阅模式
   即消息生产者发送消息至服务器后,所有处在监听状态的消息消费者都可以接收到消息

测试Demo
1.创建消息生产者,创建点对点的消息队列

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

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

/**
 * 1.创建消息生产者
 * @author Du
 * @Date 2018年3月7日
 */
public class Peoducer {

    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) {
        Destination destination;
        Session session;
        Connection connection = null;
        //实例化链接工厂
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
        try {
            //创建链接
            connection = connectionFactory.createConnection();
            //启动链接
            connection.start();
            //创建session
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("queue1");// 创建点对点的消息队列
            MessageProducer messageProducer = session.createProducer(destination);
            messageProducer.send(session.createTextMessage("queue"));
            session.commit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
                try {
                    if(connection!=null)
                        connection.close();
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
}

2.创建消息消费者,使用receive方法接收消息

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

public class Consumer {

    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;
    static Destination destination;
    static Session session;
    static Connection connection;

    public static void main(String[] args) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("queue1");
            MessageConsumer consumer = session.createConsumer(destination);
            while(true){
                TextMessage textMessage=(TextMessage)consumer.receive(100000);//receive方法每次接收一条消息,参数timeout超时时间
                if(textMessage!=null){
                    System.out.println("收到的消息:"+textMessage.getText());
                }else{
                    break;
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
                try {
                    if(connection!=null)
                        connection.close();
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
}

3.使用Listener监听器接收消息

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

public class Listener implements MessageListener{

    @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
        try {
            System.out.println("接收到订阅者二的消息:"+((TextMessage)message).getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

注册监听器

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
 * 2.创建消息消费者
 * @author Du
 * @Date 2018年3月7日
 */
public class Consumer {

    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;
    static Destination destination;
    static Session session;
    static Connection connection;

    public static void main(String[] args) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("queue1");
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new Listener());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行Producer,在监控台上可以看到:
这里写图片描述
未处理的消息1条,队列中的消息1条,消费者数量为0
接下来运行Consumer类,再刷新监控台:
这里写图片描述
消费者数量变为1,在看控制台输出
这里写图片描述

2.创先发布订阅模式
消息生产者

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

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

/**
 * ActiveMQ是JMS的一套实现
 * 创建点对点的消息会话
 * 使用步骤:
 *  1.创建消息生产者
 *  2.创建消息消费者
 * @author Du
 * @Date 2018年3月7日
 */
public class Producer {

    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) {
        Destination destination;
        Session session;
        Connection connection = null;
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createTopic("topic");// 创建发布订阅的消息模式
            MessageProducer messageProducer = session.createProducer(destination);
            for (int i = 0; i <10; i++) {
                messageProducer.send(session.createTextMessage("topic"+i));
            }
            session.commit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
                try {
                    if(connection!=null)
                        connection.close();
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
}

消息消费者1

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

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


public class Consumer {

    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;
    static Destination destination;
    static Session session;
    static Connection connection;

    public static void main(String[] args) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createTopic("topic");
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new Listener());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

监听器1

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

public class Listener implements MessageListener{

    @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
        try {
            System.out.println("接收到订阅者一的消息:"+((TextMessage)message).getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

消息消费者二

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

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


public class Consumer2 {

    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;
    static Destination destination;
    static Session session;
    static Connection connection;

    public static void main(String[] args) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createTopic("topic");
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new Listener2());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

监听器2

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

public class Listener2 implements MessageListener{

    @Override
    public void onMessage(Message message) {
    // TODO Auto-generated method stub
        try {
            System.out.println("接收到订阅者二的消息:"+((TextMessage)message).getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

启动生产者,观察监控台
这里写图片描述
可以看到队列中有十条消息,0个消费者,然后启动两个消息消费者
这里写图片描述
可以看到消费者数量增加为2,此时控制台
这里写图片描述
没有任何输出,再运行生产者
这里写图片描述
这里写图片描述

控制台输出了消息

点对点的消息模式是在没有消费者接受的情况下,消息会存储在服务器,当有消费者链接到服务器以后会获取服务器上存储的消息,而订阅发布模式是只有当处在监听状态的消费者才能接受带生产者发送的消息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值