使用camel分发activeMQ消息给消费者

本文介绍了如何结合ActiveMQ与Apache Camel来创建可配置消费者数量的消费者组件。通过引入camel-spring-boot-starter和camel-jms库,实现了简洁的代码结构。消费者示例展示了从JMS队列接收消息并调用处理方法的过程,而生产者部分展示了如何使用Spring Boot和ActiveMQ发送消息到VirtualTopic。这种组合提供了高度灵活的消息处理能力。
摘要由CSDN通过智能技术生成

activeMQ配合使用camel可灵活配置消费者个数,代码也更简洁

需要引入的jar包

compile group: 'org.apache.camel', name: 'camel-spring-boot-starter', version: '2.18.3'
compile group: 'org.apache.camel', name: 'camel-jms', version: '2.18.3'

消费者-consumer

@Component
public class StoreRouter extends RouteBuilder{

    @Override
    public void configure() throws Exception {
    //命名规则必须遵守Consumer.consumerName.VirtualTopic.topicName,简单来说只能有三个. 如需分割可使用-来分割
        from("jms:queue:Consumer.consumer1.VirtualTopic.store-MessageProducer-storeRegister?concurrentConsumers=1").
                to("bean:storeMessageConsumer?method=storeRegister").routeId("注册消息消费者");

    }
}
@Component
public class StoreMessageConsumer {
    private Logger logger = LoggerFactory.getLogger(StoreMessageConsumer.class);

  
    /**
     * 注册消息
     *
     * @throws IOException
     */
    public void storeRegister(@Headers Map result) throws IOException {
        try {
            logger.info("消费注册消息,信息:"+ result.get("id"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

生产者-producer

引入jar包:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-activemq', version: '1.4.2.RELEASE'
@Component
public class StoreMessageProducer {
    private static Logger logger = LoggerFactory.getLogger(StoreMessageProducer.class);

    //这里使用的是VirtualTopic,优点不赘诉
    @Produce(uri = "jms:topic:VirtualTopic.store-MessageProducer-storeRegister")
    private ProducerTemplate registTopicProducerTemplate;


    
    public void storeRegister(String id) {
        Map map = new HashMap();
        map.put("id", id);
        try {
            storeRegistTopicProducerTemplate.sendBodyAndHeaders( "store:register", map);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("注册 activeMQ发送出错", e);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值