ActiveMQ

JMS  一天规范

ActiveMQ原理:

消息的生产者,---------------------发给MQ的,MQ有一个队列---存消息  -----------消息的消费者,

 

ActiveMQ的管理页面8161  admin/admin

 

点对点方式:消费者主动接收,从消息队列拉取

发布订阅方式:消费者被动接收

消息中间件:

在客户端和服务端之间rpc同步调用。

 

消息中间件的好处:

解决耦合问题。服务提供者不用知道下游调用者的细节。

异步模型。请求者不用像之前那样一直要等服务器响应

保护主业务,抵御洪峰。请求不直接打到服务端

 

MOM:面向像消息的中间件。

 

activeMQ安装

1.下载  http://activemq.apache.org/

2.linux 版本,放到服务器解压即可,进入bin目录启动,window类似

tar -xvf apache-activemq-5.15.12-bin.tar.gz ##解压
cd /opt/apache-activemq-5.15.12/bin  ###进入bin
./activemq start   ##启动
ps -ef | grep activemq  ##查看是否启动成功

 

activemq提供了可视化页面:端口8161    admin/admin  

 

mq上手案例点对点

pom.xml

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.1</version>
        </dependency>

生产者

package com.wang.common.mq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 消息生成者
 * 往mq发消息过程 --点对点
 *
 */
public class JmsProduce {


    private static String mqUri="tcp://127.0.0.1:61616";

    public static void main(String[] args) throws JMSException {
        //1 连接mq服务器的工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(mqUri);

        Connection connection =null;
        Session session =null;
        Queue queue=null;
        MessageProducer mp=null;
        TextMessage tm = null;
        try {
            //2从工厂拿到连接
            connection = activeMQConnectionFactory.createConnection();
            //3 开启连接
            connection.start();
            //4 会话
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //5 消息队列
            queue = session.createQueue("helloMQ");
            //6 消息生成者创建 绑定到队列上
             mp = session.createProducer(queue);
            //7 消息实例
             tm = session.createTextMessage("hello,i'm message content");
            //8 发送消息
            mp.send(tm);

        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
//            关闭资源
            mp.close();
            session.close();
            connection.close();
        }
    }

}

消费者

package com.wang.common.mq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JmsConsumer {

    private static String mqUri="tcp://127.0.0.1:61616";
    public static void main(String[] args) throws JMSException {
        //1 连接mq服务器的工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(mqUri);

        Connection connection =null;
        Session session =null;
        Queue queue=null;
        MessageConsumer messageConsumer=null;
        TextMessage tm = null;
        try {
            //2从工厂拿到连接
            connection = activeMQConnectionFactory.createConnection();
            //3 开启连接
            connection.start();
            //4 会话
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //5 消息队列
            queue = session.createQueue("helloMQ");
            //6 消息消费者 绑定到队列上
            messageConsumer = session.createConsumer(queue);
          
            //7 接收消息
            while (true){
                TextMessage textMessage = (TextMessage) messageConsumer.receive();
                if (textMessage!=null) {
                    System.out.println("收到消息==>"+textMessage);
                }else {
                    break;
                }
            }

        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
//            关闭资源
            messageConsumer.close();
            session.close();
            connection.close();
        }
    }



}

 

消费者另外一种方式获取消息

setMessageListener() 方法监听

当有多个消费者时监听时,发布者发布了多条,那么消费者平均分配发布的消息。

package com.wang.common.mq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import javax.xml.soap.Text;
import java.io.IOException;

public class JmsConsumerListen {

    private static String mqUri="tcp://123.56.2.0:61616";
    public static void main(String[] args) throws JMSException, IOException {
        //1 连接mq服务器的工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(mqUri);

        Connection connection =null;
        Session session =null;
        Queue queue=null;
        MessageConsumer messageConsumer=null;
        TextMessage tm = null;
            //2从工厂拿到连接
            connection = activeMQConnectionFactory.createConnection();
            //3 开启连接
            connection.start();
            //4 会话
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //5 消息队列
            queue = session.createQueue("helloMQ");
            //6 消息消费者 绑定到队列上
            messageConsumer = session.createConsumer(queue);

            //7 接收消息  监听的方式
            messageConsumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    if(null!=message && message instanceof TextMessage ){
                        TextMessage textMessage = (TextMessage) message;
                        try {
                            System.out.println(textMessage.getText());
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }

                    }
                }
            });
            //需要等待,不然来不及消费就被关闭连接了
            System.in.read();
            messageConsumer.close();
            session.close();
            connection.close();

        }
    }



案例  发布订阅  一对多

微信公众号订阅。 

消费者订阅之后,发布者发布了消息之后,消费者就可以收到消息了。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值