当前位置: 首页 > news >正文

Docker安装ActiveMQ镜像以及通过Java生产消费activemq示例

拉取镜像

docker pull docker.io/webcenter/activemq

启动容器

docker run -d --name myactivemq -p 61616:61616 -p 8162:8161 docker.io/webcenter/activemq:latest

这样就代表启动成功了

浏览器访问

http://localhost:8162/

admin admin

开启验证

修改配置文件/opt/activemq/conf/activemq.xml

/opt/activemq/conf# vi activemq.xml

<plugins>

     <simpleAuthenticationPlugin>

        <users>

            <authenticationUser username="guest" password="guest" groups="guests"/>

            <authenticationUser username="user" password="user" groups="users"/>

            <authenticationUser username="admin" password="admin" groups="admins"/>

            <authenticationUser username="artemis" password="artemis" groups="admins"/>

        </users>

     </simpleAuthenticationPlugin>

</plugins>

修改完重启镜像即可

通过java代码生产,消费activemq

生产者

package com.activemq.demo;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class ActiveMQProducer {

    private static final String DEFAULT_BROKER_HOST = "tcp://localhost:61616";

    public static void main(String[] args) throws JMSException {

// 创建连接工厂

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(DEFAULT_BROKER_HOST);

// 获取 connection

        final Connection connection = connectionFactory.createConnection("admin", "admin");

// 启动

        connection.start();

// 创建会话 session,参数第一个是事务,第二个是签收

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

// 创建目的地,queue 或者 topic

        Queue queue = session.createQueue("testq1804");

创建消息的生产者

        MessageProducer producer = session.createProducer(queue);

// 创建消息

        for (int i = 0; i < 100000; i++) {

//        while(true){

            String message = "{\"id\":"+System.currentTimeMillis()+", \"name\":\""+i+"\"}"; //System.currentTimeMillis() + "这是一条消息" + new Date();

            TextMessage textMessage = session.createTextMessage(message);

            System.out.println(message);

// 发送消息给 mq

            producer.send(textMessage);

            try {

                Thread.sleep(500l);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }

}

消费者

package com.activemq.demo;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.Connection;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageConsumer;

import javax.jms.MessageListener;

import javax.jms.Queue;

import javax.jms.Session;

import javax.jms.TextMessage;

public class ActiveMQConsumer {

    public static void main(String[] args) throws Exception {

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

// 获取 connection

        final Connection connection = connectionFactory.createConnection("user", "user");

// 启动

        connection.start();

// 创建会话 session,参数第一个是事务,第二个是签收

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

// 创建目的地,queue 或者 topic

        Queue queue = session.createQueue("testq1804"); // test_topic_m1

            MessageConsumer consumer = session.createConsumer(queue);

        consumer.setMessageListener(new MessageListener() {

            @Override

            public void onMessage(Message message) {

                try {

                    System.out.println("接收到消息:" + ((TextMessage)message).getText());

                } catch (JMSException e) {

                    e.printStackTrace();

                }

            }

        });

    }

}

http://www.lryc.cn/news/465164.html

相关文章:

  • 迅为RK3562开发板/核心板240PIN引脚全部引出,产品升级自如
  • C++实现顺序栈和链栈操作(实验3--作业)
  • 龙兴物联一体机:设备监测的智能先锋
  • KinectDK相机SDK封装Dll出现k4abt_tracker_create()创建追踪器失败的问题
  • Linux 命令—— ping、telnet、curl、wget(网络连接相关命令)
  • 高速缓冲存储器Cache是如何工作的、主要功能、高速缓冲存储器Cache和主存有哪些区别
  • 极简版Java敏感词检测SDK
  • H3C路由器交换机操作系统介绍
  • 【项目案例】-音乐播放器-Android前端实现-Java后端实现
  • EasyX图形库的安装
  • 数据结构 - 队列
  • 基于springboot美食推荐商城的设计与实现
  • React开发一个WebSocket
  • Oracle DECODE 丢失时间精度的原因与解决方案
  • 如何用示波器检测次级点火系统(一)
  • 基于SpringBoot+Vue+uniapp的涪陵区特色农产品交易系统的详细设计和实现(源码+lw+部署文档+讲解等)
  • bmp怎么转换为jpg?快速批量将bmp转换为jpg
  • centos8配置java环境变量jdk8u422-b05
  • 基于SSM的校园拓展活动管理系统
  • Python随机森林算法详解与案例实现
  • 提示词高级阶段学习day2.1-在提示词编写中对{}的使用教程
  • 2024年,每一个大模型都躲不过容嬷嬷和紫薇
  • SpringBoot之RedisTemplate基本配置
  • SparseRCNN 模型,用于目标检测任务
  • 【AIGC】第一性原理下的ChatGPT提示词Prompt设计:系统信息与用户信息的深度融合
  • DeepSpeed性能调优与常见问题解决方案
  • 【GESP】C++一级练习BCQM3052,鸡兔同笼
  • Android面试之5个性能优化相关的深度面试题
  • R语言机器学习算法实战系列(六)K-邻近算法 (K-Nearest Neighbors)
  • FPGA图像处理之构建3×3矩阵