ActiveMQ集成SpringBoot
(1)创建项目导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
(2)编写application.xml
#ActiveMQ配置
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admimn
pool:
max-connections: 100
jms:
pub-sub-domain: true #false:队列 true:主题
#队列名称
myqueue: boot-activemq-queue
#主题名称
mytopic: boot-activemq-topic
(3)编写配置类
/**
* 队列配置类
*/
@Component
public class queueConfig {
@Value("${myqueue}")
private String myQueue;
@Bean
public Queue queue(){
return new ActiveMQQueue(myQueue);
}
}
```java
/**
* 主题配置类
*/
@Component
public class TopicConfig {
@Value("${mytopic}")
private String mytopic;
@Bean
public Topic topic(){
return new ActiveMQTopic(mytopic);
}
}
(4)编写生产类
/**
* Queue生产类
*/
//@Component
public class QueueProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@SuppressWarnings("deprecation")
public void produceMsg(){
jmsMessagingTemplate.convertAndSend(queue,"message:"+ new Date().toLocaleString());
}
}
/**
* topic生产类
*/
@Component
public class TopicProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
@SuppressWarnings("deprecation")
//定时发送消息 3秒
@Scheduled(fixedDelay = 3000)
public void produceMsg(){
jmsMessagingTemplate.convertAndSend(topic,"message:"+new Date().toLocaleString());
}
}
(5)编写消费类
/**
* queue消费类
*/
@Component
public class QueueConsumer {
@JmsListener(destination = "${myqueue}")
public void receive(TextMessage textMessage) throws JMSException {
System.out.println("Queue消费者接受消息:"+textMessage.getText());
}
}
/**
* topic消费类
*/
@Component
public class TopicConsumer {
@JmsListener(destination = "${mytopic}")
public void receive(TextMessage textMessage) throws JMSException {
System.out.println("Topic收到的信息:"+textMessage.getText());
}
}
(5)测试
@SpringBootApplication
//启用jms
@EnableJms
//定时发送消息
@EnableScheduling
public class ActivemqbootApplication {
public static void main(String[] args) {
SpringApplication.run(ActivemqbootApplication.class, args);
}
}
@SpringBootTest
class ActivemqbootApplicationTests {
@Autowired
TopicProducer topicProducer;
@Test
void contextLoads() {
topicProducer.produceMsg();
}
}
注意:
队列模式:
先启动ActivemqbootApplicationTests方法,在启动ActivemqbootApplication
主题模式:
先启动ActivemqbootApplication ,后启动ActivemqbootApplicationTests
一个项目同时监听队列和主题配置
(1)application.yml配置
#ActiveMQ配置
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
jms:
pub-sub-domain: false
(2)创建config类
@Configuration
public class TopicConfig {
@Bean("configContainerFactory")
public DefaultJmsListenerContainerFactory configContainerFactory(ConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory containerFactory = new DefaultJmsListenerContainerFactory();
containerFactory.setConnectionFactory(connectionFactory);
containerFactory.setPubSubDomain(true);
return containerFactory;
}
}
(4)生产者类
@RestController
@RequestMapping("/active")
public class ActiveMQProducer {
@Autowired
private JmsTemplate jmsTemplate;
/**
* 队列生产者
* @return
*/
@RequestMapping("/sendqueue")
public Map<String,Object> queuehello(){
Map<String,Object> map = new HashMap<>();
jmsTemplate.setDefaultDestination(new ActiveMQQueue("queue-hello"));
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("Hello-queue------" + new Date().toLocaleString());
map.put("resources","发送成功");
return textMessage;
}
});
return map;
}
/**
* 队列生产者
* @return
*/
@RequestMapping("/sendtopic")
public Map<String,Object> topichello(){
Map<String,Object> map = new HashMap<>();
jmsTemplate.setDefaultDestination(new ActiveMQTopic("topic-hello"));
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("Hello-queue----" + new Date().toLocaleString());
map.put("resources","发送成功");
return textMessage;
}
});
return map;
}
}
(4)消费类
@Component
public class ActiveMqConsumer {
@JmsListener(destination = "queue-hello")
public void queueConsumer(TextMessage textMessage) throws JMSException {
System.out.println("Queue收到的信息:"+textMessage.getText());
}
//containerFactory 名称与config类的@Bean("configContainerFactory")一样
@JmsListener(destination = "topic-hello",containerFactory = "configContainerFactory")
public void topicConsumer(TextMessage textMessage) throws JMSException {
System.out.println("Topic收到的信息:"+textMessage.getText());
}
}
(5)测试类
1.消费者启动类
@SpringBootApplication
@EnableJms
public class ActivemqConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ActivemqConsumerApplication.class, args);
System.out.println("消费者启动");
}
}
2.生产者启动类
@SpringBootApplication
@EnableJms
public class ActivemqProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ActivemqProducerApplication.class, args);
System.out.println("生产者项目");
}
}