-
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
-
配置文件
spring.activemq.broker-url=nio://127.0.0.1:61617 spring.activemq.user=admin spring.activemq.password=admin spring.activemq.packages.trust-all=true spring.jms.pub-sub-domain=true
-
配置类
@Configuration public class ActivemqConfig { //监听Topic @Bean(name = "jmsListenerContainerTopic") public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory connectionFactory) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setPubSubDomain(true); factory.setConnectionFactory(connectionFactory); //自动提交 factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE); return factory; } //监听Queue @Bean(name = "jmsListenerContainerQueue") public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory connectionFactory) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); //自动提交 factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE); return factory; } }
-
发送消息
@Service public class ActivemqProducerService { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private JmsTemplate jmsTemplate; public void jmsTemplateTest() { ActiveMQQueue user = new ActiveMQQueue("user"); jmsTemplate.setDefaultDestination(user); jmsTemplate.setSessionTransacted(false); jmsTemplate.send(new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage("user"); textMessage.setText("aaaaa"); return textMessage; } }); } public void jmsMessagingTemplateTest() throws JMSException { ArrayList<String> strings = new ArrayList<>(); strings.add("aaa"); strings.add("bbb"); //ActiveMQTopic user = new ActiveMQTopic("user"); ActiveMQQueue user = new ActiveMQQueue("user"); jmsMessagingTemplate.convertAndSend(user, strings); ConnectionFactory connectionFactory = jmsMessagingTemplate.getConnectionFactory(); Connection connection = connectionFactory.createConnection(); } }
-
接收消息
@Service public class ActivemqConsumerService { @JmsListener(destination = "user", containerFactory = "jmsListenerContainerQueue") public void consumerQueueMessage(Message message) { System.out.println(message); } @JmsListener(destination = "user", containerFactory = "jmsListenerContainerTopic") public void consumerTopicMessage(Message message) { System.out.println(message); } }
SpringBoot 整合 ActiveMQ
最新推荐文章于 2024-06-07 08:00:00 发布
关键词由CSDN通过智能技术生成