springboot集成activemq

springboot给我们的开发带了很大简便,帮住我们管理复杂的项目依赖,以springboot与activemq集成为例,在集成activemq的时候我们只需要加上一个依赖就可以了
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
加上依赖以后我们只需要配置对应brokenUrl,user,paswword即可

下面给大家展示下我对activemq的一个简单集成处理

@Configuration
@EnableJms
public class DefaultConf {
    @Autowired
    PropretyConf propretyConf;

    @Bean
    public RedeliveryPolicy redeliveryPolicy(){
        RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
        redeliveryPolicy.setMaximumRedeliveries(3);
        redeliveryPolicy.setUseExponentialBackOff(true);
        return redeliveryPolicy;
    }

    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory(RedeliveryPolicy redeliveryPolicy) throws IllegalArgumentException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(StringUtils.isEmpty(propretyConf.getBrokenUrl())?ActiveMQConnectionFactory.DEFAULT_BROKER_URL:propretyConf.getBrokenUrl());
        activeMQConnectionFactory.setUserName(StringUtils.isEmpty(propretyConf.getUser())?ActiveMQConnectionFactory.DEFAULT_USER:propretyConf.getUser());
        activeMQConnectionFactory.setPassword(StringUtils.isEmpty(propretyConf.getPassword())?ActiveMQConnectionFactory.DEFAULT_PASSWORD:propretyConf.getPassword());
        activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
        return activeMQConnectionFactory;
    }

    @Bean
    public JmsTemplate jmsQueueTemplate(ActiveMQConnectionFactory activeMQConnectionFactory){
        JmsTemplate jmsQueueTemplate = new JmsTemplate();
        if(StringUtils.isEmpty(propretyConf.getDestination())) throw new IllegalArgumentException("缺少destination参数。。。");
        Queue jmsQueue = new ActiveMQQueue(propretyConf.getDestination());
//        连接工厂
        jmsQueueTemplate.setConnectionFactory(activeMQConnectionFactory);
//        消息持久化
        jmsQueueTemplate.setDeliveryPersistent(propretyConf.isDeliveryPersistent());
//        目的地
        jmsQueueTemplate.setDefaultDestination(jmsQueue);

        return jmsQueueTemplate;
    }

    @Bean
    public JmsTemplate jmsTopicTemplate(ActiveMQConnectionFactory activeMQConnectionFactory){
        JmsTemplate jmsTopicTemplate = new JmsTemplate();
        if(StringUtils.isEmpty(propretyConf.getDestination())) throw new IllegalArgumentException("缺少destination参数。。。");
        Topic jmsTopic = new ActiveMQTopic(propretyConf.getDestination());
        //        连接工厂
        jmsTopicTemplate.setConnectionFactory(activeMQConnectionFactory);
//        消息持久化
        jmsTopicTemplate.setDeliveryPersistent(propretyConf.isDeliveryPersistent());
//        目的地
        jmsTopicTemplate.setDefaultDestination(jmsTopic);
//        设置订阅模式
        jmsTopicTemplate.setPubSubDomain(true);

        return jmsTopicTemplate;
    }

    @Bean
    public DefaultJmsListenerContainerFactory chenTopicJmsListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory){
        DefaultJmsListenerContainerFactory chenJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        chenJmsListenerContainerFactory.setPubSubDomain(true);
        chenJmsListenerContainerFactory.setConnectionFactory(activeMQConnectionFactory);
        return chenJmsListenerContainerFactory;
    }

    @Bean
    public DefaultJmsListenerContainerFactory chenQueueJmsListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory){
        DefaultJmsListenerContainerFactory chenJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        chenJmsListenerContainerFactory.setPubSubDomain(false);
        chenJmsListenerContainerFactory.setConnectionFactory(activeMQConnectionFactory);
        return chenJmsListenerContainerFactory;
    }
}
@Component
@PropertySource("classpath:application.yml")
@ConfigurationProperties(prefix="jms")
public class PropretyConf {

    public String destination;

    public String brokenUrl;

    public String user;

    public String password;

    public boolean deliveryPersistent;

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getBrokenUrl() {
        return brokenUrl;
    }

    public void setBrokenUrl(String brokenUrl) {
        this.brokenUrl = brokenUrl;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isDeliveryPersistent() {
        return deliveryPersistent;
    }

    public void setDeliveryPersistent(boolean deliveryPersistent) {
        this.deliveryPersistent = deliveryPersistent;
    }


}
@Component
public class JmsQueueSender {

    @Autowired
    JmsTemplate jmsQueueTemplate;

    public void send(ChenMessage message){
        jmsQueueTemplate.convertAndSend(JsonUtils.Object2Json(message));
    }

    public void sendText(String message){
        jmsQueueTemplate.convertAndSend(message);
    }

    public JmsQueueSender() {
    }
}

消息生产者

@Service
public class SendMsg {

    @Autowired
    JmsSenderFactory jmsSenderFactory;

    public void send(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        jmsSenderFactory.getJmsTopicSender().sendText("我是测数据1");
    }

    public void sendQueue(){
        jmsSenderFactory.getJmsQueueSender().sendText("我是测数据1");
    }
}

消息消费者

@Service
public class RecevierMsg {

    @JmsListener(destination = "chen.queue",containerFactory = "chenQueueJmsListenerContainerFactory")
    public void recevierMsg(String string){
        System.out.println("queue接受到数据》》》》》"+string);
    }

    @JmsListener(destination = "chen.queue",containerFactory = "chenTopicJmsListenerContainerFactory")
    public void recevierMsg1(String string){
        System.out.println("topic接受到数据》》》》》"+string);
    }
}
具体代码集成https://gitee.com/pgleo/spring-boot-jms.git
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值