activemq
activemq的安装很容易,直接去官网下载zip包解压即可http://activemq.apache.org/。windows下进入bin目录的win64目录,以管理员运行InstallService.bat就可以通过服务的方式运行mq。
pom需要引入的依赖如下
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<!-- <version>5.7.0</version> -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置文件如下,比较简单。
#broker的url
spring.activemq.broker-url=tcp://localhost:61616
#是否是内存模式
spring.activemq.in-memory=true
#是否使用连接池
spring.activemq.pool.enabled=false
#设置为true为发布订阅模式,默认为点对点模式
spring.jms.pub-sub-domain=false
配置类如下,主要创建队列和主题的bean
package org.mq.activemq;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
import javax.jms.Topic;
@Configuration
public class MqConfig {
//创建队列bean
@Bean
public Queue queue(){
return new ActiveMQQueue("testqueue");
}
//创建主题bean
@Bean
public Topic topic(){
return new ActiveMQTopic("testtopic1");
}
}
生产者如下
package org.mq.activemq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Queue;
import javax.jms.Topic;
@Service
public class Producter {
//注入队列和主题对象
@Autowired
private Queue queue;
@Autowired
private Topic topic;
//通过JmsMessagingTemplate发送消息
@Autowired
private JmsMessagingTemplate jmsTemplate;
// 发送消息到队列
public void sendQueueMessage(final String message){
jmsTemplate.convertAndSend(queue, message);
}
// 发送消息到主题
public void sendTopicMessage(final String message){
jmsTemplate.convertAndSend(