Java -- springboot 配置 activeMq

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();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值