docker pull webcenter/activemq
docker run -d --name myactivemq -p 61616:61616 -p 8161:8161 webcenter/activemq
docker exec -it myactivemq /bin/bash
2.4.2 编辑activemq.xml文件,在shutdownHooks下方添加:
<plugins >
<simpleAuthenticationPlugin >
<users >
<authenticationUser username =" ${activemq.username}" password =" ${activemq.password}" groups =" users,admins" />
</users >
</simpleAuthenticationPlugin >
</plugins >
2.4.3 修改credentials.properties文件:
activemq.username =admin
activemq.password =123456
guest.password =123456
3.2 创建maven目录结构,以及pom.xml文件
<parent >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-parent</artifactId >
<version >2.0.5.RELEASE</version >
<relativePath />
</parent >
3.4 pom.xml文件中加入springboot-starter依赖
<dependencies >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-web</artifactId >
</dependency >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-activemq</artifactId >
</dependency >
</dependencies >
3.5 pom.xml文件中加入maven-springboot打包插件
<build >
<plugins >
<plugin >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-maven-plugin</artifactId >
</plugin >
</plugins >
</build >
import org .springframework .boot .SpringApplication ;
import org .springframework .boot .autoconfigure .SpringBootApplication ;
@ SpringBootApplication
public class Application {
public static void main (String [] args ) {
SpringApplication .run (Application .class , args );
}
}
import org .springframework .jms .core .JmsMessagingTemplate ;
import org .springframework .stereotype .Component ;
import javax .annotation .Resource ;
import javax .jms .Destination ;
@ Component
public class ActiveMQProducer {
@ Resource
private JmsMessagingTemplate messagingTemplate ;
public void send (Destination destination , String message ) {
messagingTemplate .convertAndSend (destination , message );
}
}
import com .oven .producer .ActiveMQProducer ;
import org .apache .activemq .command .ActiveMQQueue ;
import org .springframework .web .bind .annotation .RequestMapping ;
import org .springframework .web .bind .annotation .RestController ;
import javax .annotation .Resource ;
import javax .jms .Destination ;
@ RestController
public class DemoController {
@ Resource
private ActiveMQProducer producer ;
@ RequestMapping ("/send" )
public String send (String msg ) {
Destination destination = new ActiveMQQueue ("springboot-activemq" );
producer .send (destination , msg );
return "发送成功!" ;
}
}
import org .springframework .jms .annotation .JmsListener ;
import org .springframework .stereotype .Service ;
@ Service
public class ActiveMQConsumer {
@ JmsListener (destination = "springboot-activemq" )
public void consumer (String message ) {
System .out .printf ("消费者收到消息:%s" , message );
}
}
spring :
activemq :
broker-url : tcp://172.16.188.184:61616
user : admin
password : 123456