- docker安装activeMq
- springboot集成activemq
查找镜像:
docker search activemq
拉取最新镜像:
docker pull docker.io/webcenter/activemq
查看
docker images
启动
docker run -d --name activemq -p 61616:61616 -p 8161:8161 webcenter/activemq
查看正在运行的容器
docker ps
访问地址
http://ip:8161
我将 消息生产者/消息消费者 放在了一个项目
项目目录
pom
org.springframework.boot spring-boot-starter-activemqorg.messaginghub pooled-jms
配置文件
spring: datasource: url: jdbc:mysql://localhost:3306/dept username: root password: root activemq: broker-url: tcp://192.168.72.130:61616 user: admin password: admin #true 表示使用内置的MQ,false则连接服务器 in-memory: false pool: #true表示使用连接池;false时,每发送一条数据创建一个连接 enabled: true #空闲的连接过期时间,默认为30秒 idle-timeout: 30000 #连接池最大连接数 max-connections: 10 # activemq 队列、订阅 的名称 queue-name: active.queue topic-name: active.topic
配置类
package **** import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.jms.Queue; import javax.jms.Topic; @Configuration public class ActiveMqConfig { @Value("${spring.activemq.queue-name}") private String QUEUE_NAME; @Value("${spring.activemq.topic-name}") private String TOPIC_NAME; @Bean public Queue queue(){ return new ActiveMQQueue(QUEUE_NAME); } @Bean public Topic topic(){ return new ActiveMQTopic(TOPIC_NAME); } }
生产者
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.jms.Queue; import javax.jms.Topic; @RestController @RequestMapping("producer") public class ProducerController { private static final Logger logger = LoggerFactory.getLogger(ProducerController.class); @Resource private JmsMessagingTemplate jmsMessagingTemplate; @Resource private Queue queue; @Resource private Topic topic; @PostMapping("send") public MessageModelsend(@RequestParam String str){ try { jmsMessagingTemplate.convertAndSend(queue, str); return new MessageModel<>(MessageModel.success, "成功"); }catch (Exception e){ logger.error("args: str ==> {}", str); logger.error("activeMq消息发送失败。", e); return new MessageModel<>(MessageModel.error, "activeMq消息发送失败"); } } }
消费者
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component public class HrUserListen { private static final Logger logger = LoggerFactory.getLogger(HrUserListen.class); @JmsListener(destination="active.queue") public void receiveHrUser(String str){ logger.info("====接收信息====:str ==> {}", str); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)