SpringBoot 整合 ActiveMQ

34 篇文章 0 订阅
9 篇文章 0 订阅
  1. 依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    
  2. 配置文件

    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
    
  3. 配置类

    @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;
        }
    
    }
    
  4. 发送消息

    @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();
    
        }
    
      }
    
  5. 接收消息

    @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);
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值