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();

}

}

});

}

}

相关推荐
念何架构之路3 小时前
Docker 容器状态机 学习
java·学习·docker
jieyucx3 小时前
Docker 入门第六阶段:综合实战项目
运维·docker·容器
不能只会打代码5 小时前
Day 002 — Python 工程化 & FastAPI & 数据库速通
数据库·redis·python·docker·fastapi·测试
yanjieliyun8 小时前
家庭服务器从入门到精通 · 第 5 篇 · NPM反向代理从安装到起飞:一条命令搞定HTTPS
docker
天狼啸月19909 小时前
RagSEDE复现: Docker and Ragflow 关系
docker·容器·ragflow·ragsede
java_logo9 小时前
Docker 部署 SamWaf 完整教程:开源 Web 应用防火墙实测
docker·开源·防火墙·waf·samwaf·轩辕镜像·docker部署waf
C137的本贾尼9 小时前
第十篇:Docker & K8s——标准化应用的“集装箱“和“码头调度中心“
docker·容器·kubernetes
增量星球10 小时前
《持续交付2.0系列一》从技术交付到业务引领
java·docker·容器·devops
探索云原生1 天前
终于搞懂 Kueue:5 个核心对象一次讲透
linux·docker·ai·云原生·kubernetes
AI服务老曹1 天前
Docker部署AI视频分析平台参数配置说明与调优指南
人工智能·docker·音视频