1、docker-compose安装activemq
version: '2'
services:
activemq:
#定义主机名
hostname: myactivemq
#使用的镜像
image: webcenter/activemq
#容器的映射端口
ports:
- 61616:61616
- 8161:8161
restart: always
#定义挂载点
volumes:
- ./data/activemq:/data/activemq
- ./var/log/activemq:/var/log/activemq
2、maven依赖pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ilovewl</groupId>
<artifactId>activemq-demo</artifactId>
<version>1.0</version>
<name>activemq-demo</name>
<description>activemq Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、配置文件
spring.activemq.broker-url=tcp://120.79.70.19:61616
spring.activemq.user=admin
spring.activemq.password=admin
#一开始设为true,生成bean错误,百度好像是说springboot有自己的线程池管理,后设为false
spring.activemq.pool.enabled=false
spring.activemq.pool.max-connections=50
spring.activemq.pool.idle-timeout=30000
3、消费者和生产者
消费者类
package com.ilovewl.activemqdemo.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class JMSConsumer {
private final static Logger logger = LoggerFactory.getLogger(JMSConsumer.class);
@JmsListener(destination = "springboot.queue.test")
public void receiveQueue(String msg) {
logger.info("接收到消息:{}",msg);
}
}
生产者:
package com.ilovewl.activemqdemo.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Destination;
@Component
public class JMSProducer {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(Destination destination, String message) {
this.jmsTemplate.convertAndSend(destination,message);
}
}
4、测试类
package com.ilovewl.activemqdemo;
import com.ilovewl.activemqdemo.config.JMSProducer;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.jms.Destination;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivemqDemoApplicationTests {
@Autowired
private JMSProducer jmsProducer;
@Test
public void contextLoads() {
Destination destination = new ActiveMQQueue("springboot.queue.test");
for(int i=0;i<10;i++){
jmsProducer.sendMessage(destination,"hello "+i);
}
}
}
启动Application类,运行成功,感恩!
end