• ActiveMQ学习第一篇:Hello-ActiveMQ


    ActiveMQ简介

    • ActiveMQ是什么

      ActiveMQ是Apache推出的一款开源的,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现的消息中间件(Message Oriented Middleware,MOM)

    • ActiveMQ能干什么

      最主要的功能就是:实现JMS Provider,用来帮助实现高可用、高性能、可伸缩、异步、解耦、易用和安全的企业级面向消息服务中间件的系统.

    • 消息中间件的特点:

      消息异步接受,类似手机短信的行为,消息发送者不需要等待消息接受者的响应,减少软件多系统集成的耦合度;
      消息可靠接收,确保消息在中间件可靠保存,只有接收方收到后才删除消息,多个消息也可以组成原子事务

    • 消息中间件的主要应用场景:

      在多个系统间进行整合和通讯的时候,通常会要求:
      可靠传输,数据不能丢失,有的时候,也会要求不能重复传输;
      异步传输,否则各个系统同步发送接受数据,互相等待,造成系统瓶颈

    ActiveMQ入门使用

    安装ActiveMQ

    下载地址
    通过ftp将下载好的ActiveMq传入服务器里面

    //解压
    tar -zxvf apache-activemq-5.15.9-bin.tar.gz 
    //重命名
    mv apache-activemq-5.15.9 activemq
    

    启动方式:

    // 进入bin目录然后启动
    cd activemq/bin/
    . /activemq start
    ​	1: activemq start :使用默认的conf/activemq. xml来启动
    ​	2: activemq start xbean:file:.. /conf/activemq-X. xml :使用指定的配置文件来启动
    	3: ./activemq/bin/activemq start  >/yangk/activemq/log :启动并指定日志文件
    

    关闭方式:

    ./activemq stop
    或者找到进程直接杀死,ps -ef| grep activemq  得到进程号,直接kill -9 PID
    

    启动成功之后访问管理端页面:http://192.168.100.155:8161/
    默认的账号密码:admin/admin
    在这里插入图片描述

    java使用ActiveMQ

    • Maven依赖
     			<dependency>
                        <groupId>org.apache.activemq</groupId>
                        <artifactId>activemq-all</artifactId>
                        <version>5.15.9</version>
                    </dependency>
    
    • 发送消息
     public static void main(String[] args) throws Exception {
            //获取连接工厂
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://192.168.100.155:61616");
            //获取连接对象
            Connection connection = activeMQConnectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建会话                                 开启事务,        会话自动确认
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            //创建一个队列
            Destination destination = session.createQueue("test");
            //创建生产者
            MessageProducer producer = session.createProducer(destination);
            for (int i=0;i<3;i++){
                TextMessage textMessage = session.createTextMessage("test" + i);
                Thread.sleep(1000);
                //发送消息
                producer.send(textMessage);
            }
            //关闭
            session.commit();
            session.close();
            connection.close();
        }
    

    在这里插入图片描述

    • 接收消息
     public static void main(String[] args) throws Exception {
            //获取连接工厂
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://192.168.100.155:61616");
            //获取连接对象
            Connection connection = activeMQConnectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建会话                                 开启事务,        会话自动确认
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            //创建一个队列
            Destination destination = session.createQueue("test");
            //创建消费者
            MessageConsumer consumer = session.createConsumer(destination);
            int i=0;
            while (i<3){
                i++;
                //接受到消息
                TextMessage message = (TextMessage) consumer.receive();
                //确认消息。ACK
                session.commit();
                System.out.println("收到消息:"+message.getText());
            }
            //关闭
            session.close();
            connection.close();
        }
    

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    Ultra-QuickSort(归并排序求逆序对数)
    Power Network (最大流增广路算法模板题)
    Sorting It All Out
    Asteroids(二分图最大匹配模板题)
    昂贵的聘礼
    Borg Maze(bfs+prim)
    Currency Exchange(判断是否有正环)
    Children of the Candy Corn (bfs+dfs)
    Tautology
    Flip Game
  • 原文地址:https://www.cnblogs.com/yangk1996/p/12663617.html
Copyright © 2020-2023  润新知