Contents
Overview
This article will show how to integrate Springboot with ActiveMQ
Step
- Setup ActiveMQ using docker
- Using command below to setup ActiveMQ by using docker,
docker run -d -p 8161:8161 -p 61616:61616 -e ACTIVEMQ_ADMIN_LOGIN=admin -e ACTIVEMQ_ADMIN_PASSWORD=admin --name activemq webcenter/activemq
Once finish you can visit http://localhost:8161
- Create a springboot application to send messages to ActiveMQ instance and retrieve messages from it
- Project Structure
-
pom.xml
-
<groupId>com.andrew-programming</groupId> <artifactId>springbootmq</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springbootmq</name> <description>Demo project for Springboot MQ Demo</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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><?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>
ActiveMQClient.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Component; @Component public class ActiveMQClient { @Autowired private JmsTemplate jmsTemplate; public void send(String message) { jmsTemplate.convertAndSend("zhisheng", message); } }
ActiveMQServer.java
import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class ActiveMQServer { @JmsListener(destination = "zhisheng") public void receive(String message) { System.out.println("收到的 message 是:" + message); } }
SpringbootmqApplication.java
import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.util.StopWatch; @SpringBootApplication public class SpringbootmqApplication { @Autowired ActiveMQClient client; @PostConstruct public void init() { StopWatch stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i < 10000; i++) { client.send("发送消息----zhisheng-----"); } stopWatch.stop(); System.out.println("发送消息耗时: " + stopWatch.getTotalTimeMillis()); } public static void main(String[] args) { SpringApplication.run(SpringbootmqApplication.class, args); } }
Running The Program
As you can see sending messages over 10000 is really a time consuming task.
Queue In ActiveMQ
Source code
-