1、配置 maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2、配置地址
spring.activemq.broker-url=tcp://127.0.0.1:61616
3、配置 ActiveMqConfig
package com.sample.common.config;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
@Configuration
public class ActiveMqConfig {
public static final String SIMPLE_DESTINATION = "simple_destination";
public static final String TOPIC_DESTINATION = "topic_destination";
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
/**
* 重发策略
* @return
*/
@Bean
public RedeliveryPolicy redeliveryPolicy(){
RedeliveryPolicy policy = new RedeliveryPolicy();
policy.setUseExponentialBackOff(true);
policy.setMaximumRedeliveries(1);
policy.setMaximumRedeliveryDelay(5000);
return policy;
}
/**
* connectionFactory
* @return
*/
@Bean
public ActiveMQConnectionFactory connectionFactory (){
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
factory.setBrokerURL(brokerUrl);
factory.setRedeliveryPolicy(redeliveryPolicy());
factory.setTrustAllPackages(true);
return factory;
}
/**
* jmsTemplate
* 开启确认模式
* @return
*/
@Bean
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
jmsTemplate.setSessionAcknowledgeMode(4);
return jmsTemplate;
}
/**
* Queue模式
* 开启确认模式
* @return
*/
@Bean(name = "jmsQueueListener")
public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setSessionAcknowledgeMode(4);
return factory;
}
/**
* Topic模式
* 开启确认模式
* @return
*/
@Bean(name = "jmsTopicListener")
public JmsListenerContainerFactory<?> jmsListenerContainerTopic() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(true);
factory.setConnectionFactory(connectionFactory());
factory.setSessionAcknowledgeMode(4);
return factory;
}
}
4、创建发送端
package com.sample.modules.web.controller;
import com.sample.common.config.ActiveMqConfig;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/active")
public class ActiveMqController {
@Autowired
private JmsTemplate jmsTemplate;
@RequestMapping("/simple")
public String simple() {
jmsTemplate.convertAndSend(ActiveMqConfig.SIMPLE_DESTINATION, "simple message");
return "success";
}
@RequestMapping("/topic")
public String topic() throws Exception{
ActiveMQTopic topic = new ActiveMQTopic(ActiveMqConfig.TOPIC_DESTINATION);
jmsTemplate.convertAndSend(topic, "topic message");
return "success";
}
}
5、创建消费端
package com.sample.modules.amqp;
import com.sample.common.config.ActiveMqConfig;
import org.apache.activemq.Message;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.Session;
@Component
public class ActiveConsumer {
@JmsListener(destination = ActiveMqConfig.SIMPLE_DESTINATION, containerFactory = "jmsQueueListener")
public void simpleA(Message message) throws Exception{
System.out.println("simpleA");
message.acknowledge();
}
@JmsListener(destination = ActiveMqConfig.SIMPLE_DESTINATION, containerFactory = "jmsQueueListener")
public void simpleB(Message message) throws Exception{
System.out.println("simpleB");
message.acknowledge();
}
@JmsListener(destination = ActiveMqConfig.TOPIC_DESTINATION, containerFactory = "jmsTopicListener")
public void topicA(Session session, Message message) throws Exception{
System.out.println("topicA");
message.acknowledge();
}
@JmsListener(destination = ActiveMqConfig.TOPIC_DESTINATION, containerFactory = "jmsTopicListener")
public void topicB(Session session, Message message) throws Exception{
System.out.println("topicB");
message.acknowledge();
}
}