1、下载启动ActiveMQ
http://activemq.apache.org/download-archives.html
下载解压后,进入bin目录,执行activemq.bat start启动,进入localhost:8161/admin,默认用户名和密码都为admin,如果能成功访问,说明启动成功。
2、引入依赖
SpringBoot2.0以上的版本,引入如下的依赖
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
<version>1.0.4</version>
</dependency>
SpringBoot2.0以下的版本,引入如下的依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
3、写配置文件
这里需要创建两个SpringBoot项目,分别为生产者与消费者,
在两个项目的application.yml文件中添加以下配置
spring:
activemq:
#指定ActiveMQ broker的url,默认自动生成
broker-url: tcp://localhost:61616
#是否是内存模式,默认为true
in-memory: true
user: admin
password: admin
pool:
#是否创建PooledConnectionFactory,而非ConnectionFactory,默认false
enabled: true
max-connections: 10
idle-timeout: 30s
4、加注解
在两个项目的启动类上添加注解@EnableJms
5、写配置类
在生产者的项目中新建一个ActiveMQConfig.class文件
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsMessagingTemplate;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;
@Configuration
public class ActiveMQConfig {
@Bean(name = "queue")
public Queue queue(){
return new ActiveMQQueue("ActiveMQQueue");
}
@Bean(name = "topic")
public Topic topic(){
return new ActiveMQTopic("ActiveMQTopic");
}
/**这里直接用读取配置的形式注入了connectionFactory的属性,还可以使用构造器来创建connectionFactory
@Bean
public ConnectionFactory connectionFactory(){
return new ActiveMQConnectionFactory(user, password, brokerUrl);
}
*/
@Bean
@ConfigurationProperties(prefix = "spring.activemq")
public ConnectionFactory connectionFactory(){
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
return activeMQConnectionFactory;
}
@Bean
public JmsMessagingTemplate jmsMessagingTemplate(){
return new JmsMessagingTemplate(connectionFactory());
}
}
在消费者的项目中新建ActiveMQConfig.class
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import javax.jms.ConnectionFactory;
@Configuration
public class ActiveMQConfig {
@Bean
@ConfigurationProperties(prefix = "spring.activemq")
public ConnectionFactory connectionFactory(){
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
return activeMQConnectionFactory;
}
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
//设置是否为发布订阅模式,默认为false
factory.setPubSubDomain(false);
return factory;
}
@Bean("topicListener")
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
}
6、发消息与接收消息
在生产者项目中新建ProviderController.class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
import javax.jms.Topic;
@RestController
public class ProviderController {
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@RequestMapping("queue")
public void send(String name){
jmsMessagingTemplate.convertAndSend(queue,name);
}
@RequestMapping("topic")
public void topic(String name){
jmsMessagingTemplate.convertAndSend(topic,name);
}
}
在消费者项目中新建Consumerservice.class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
@Component
public class ConsumerService {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@JmsListener(destination = "ActiveMQQueue",containerFactory = "queueListener")
public void handleMessage(String name){
System.out.println("Queue成功接收Name:"+name);
}
@JmsListener(destination = "ActiveMQTopic" , containerFactory = "topicListener")
public void topicMsg(String name){
System.out.println("Topic成功接收Name:"+name);
}
}
7、测试
在浏览器地址中输入http://localhost:8080/queue?name=CSDN
此时在http://localhost:8161/admin/queues.jsp中可以看到
然后控制台
说明queue消息发送成功,并成功接收了。
然后在浏览器地址栏中输入http://localhost:8080/topic?name=NDSC
此时在http://localhost:8161/admin/topics.jsp中可以看到
控制台
说明topic消息也发送成功,并被成功接收了。