ActiveMQ学习(一)

本文介绍了如何在SpringBoot应用中集成ActiveMQ,通过配置broker-url、user和password来创建ActiveMQConnectionFactory和JmsTemplate。示例中展示了生产者和消费者的实现,使用JmsListener监听消息,并在Controller中发送消息到队列。注意spring.jms.pub-sub-domain属性对订阅模式的影响。
摘要由CSDN通过智能技术生成

使用了springboot框架,直接上手的activeMQ

1. propertise基本配置:

	spring.activemq.broker-url=tcp://XXXXX:XXX
	spring.activemq.user=admin
	spring.activemq.password=XXXXXX
	
	#springboot自动根据这些配置进行注册**ActiveMQConnectionFactory**
	和 **JmsTemplate**
	#注意
	#spring.jms.pub-sub-domain=true  注意开启后为订阅模式,
	消费者就会跑到Topic上,即使你收听的是队列。

2. 在service上构建消费者和生产者。

Producer
@Service
public class Producer {

    @Autowired
    JmsMessagingTemplate jmsTemplate;



    public void sendMessage(Destination destination, Message message){
        jmsTemplate.convertAndSend(destination,message);

    }

}
Consumer
@Component
@Slf4j
public class Consumer {

    @HikActiveMQDiscovery
    @JmsListener(destination = "hello")
    public void receive(Message message) {
        System.out.println("here is the message :" + message);
        log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!recevived!!!!!!!!!!!!!!!!!!");
    }
}

3. Controller上发布消息(很鸡肋)

public class mqController {

//    @Autowired
//    JmsComponent jmsComponent;
    @Autowired
    Producer producer;
    @Autowired
    Consumer consumer;

    @RequestMapping("/mq")
    public void help() throws InterruptedException {

        ActiveMQQueue destination = new ActiveMQQueue("hello");
        for (int i = 0; i < 100; i++) {
            Message me = new Message(i + "this is the activemq producer", new Date());
            producer.sendMessage(destination, me);
            Thread.sleep(1000);
            log.info(i + "have sent!");


        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值