这次要说的是springboot整合activemq,对于消息队列小伙伴们有怎样的理解呢?下面来看看springboot如何集成activemq吧。
首先创建一个springboot项目,结构如下:
Pom文件
<?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> <groupId>com.zhisheng</groupId> <artifactId>activemq</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>activemq</name> <description>Demo project for Spring Boot ActiveMQ</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties配置文件
spring.activemq.broker - url = tcp: //localhost:61616 spring.activemq.user = admin spring.activemq.password = admin
发送消息类
package com.zhisheng.activemq.client; 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); } }
消息接收类
package com.zhisheng.activemq.server; 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); } }
PS:这个队列是不需要我们提前定义好的,它和 RabbitMQ 不一样,它会在我们需要的时候动态的创建。
下面可以开始运行
package com.zhisheng.activemq; import com.zhisheng.activemq.client.ActiveMQClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.util.StopWatch; import javax.annotation.PostConstruct; @SpringBootApplication public class ActivemqApplication { @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(ActivemqApplication.class, args); } }
这样就全部集成成功了。
以上就是关于springboot整合activemq的所有内容,经历了这么多,相信对于java项目中常见问题你已经有了自己独特的解决方法,还想了解更多的话,就来关注我们吧。
推荐阅读: