Springboot中如何整合Activemq(activemq,springboot,移动开发)

时间:2024-04-28 14:41:08 作者 : 石家庄SEO 分类 : 移动开发
  • TAG :

1 导入整合所需要的依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency>

2 创建application.properties文件

spring.activemq.broker-url=tcp://127.0.0.1:61616spring.activemq.user=adminspring.activemq.password=adminserver.port=8080queue=myqueue

3.自定义配置文件QueueConfig 读取配置文件的队列名,根据队列名字创建一个Queue

packagecom.example.demo;importjavax.jms.Queue;importorg.apache.activemq.ActiveMQConnectionFactory;importorg.apache.activemq.command.ActiveMQQueue;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.jms.config.DefaultJmsListenerContainerFactory;importorg.springframework.jms.core.JmsTemplate;@ConfigurationpublicclassQueueConfig{@Value("${queue}")privateStringqueue;@BeanpublicQueuelogQueue(){returnnewActiveMQQueue(queue);}}

4.创建生产者,可以直接使用提供的模板JmsMessagingTemplate 进行消息的发送:

packagecom.example.demo.producter;importjavax.jms.Queue;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jms.core.JmsMessagingTemplate;importorg.springframework.stereotype.Component;importcom.example.demo.SpringbootActivemqApplication;@ComponentpublicclassProducter{@AutowiredprivateJmsMessagingTemplatejmsMessagingTemplate;@AutowiredprivateQueuequeue;privatestaticLoggerlogger=LoggerFactory.getLogger(Producter.class);publicvoidsend(){Stringstr="生产者生产数据:"+System.currentTimeMillis();jmsMessagingTemplate.convertAndSend(queue,str);logger.info("生产者数据:{}",str);}}

5.启动类:

packagecom.example.demo;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.context.ApplicationListener;importorg.springframework.context.event.ContextRefreshedEvent;importorg.springframework.scheduling.annotation.EnableScheduling;importcom.example.demo.producter.Producter;importcom.example.demo.producter.consumer.Consumer;@SpringBootApplication@EnableSchedulingpublicclassSpringbootActivemqApplicationimplementsApplicationListener<ContextRefreshedEvent>{@AutowiredpublicProducterproducter;@AutowiredpublicConsumerconsumer;publicstaticvoidmain(String[]args){SpringApplication.run(SpringbootActivemqApplication.class,args);//onApplicationEvent方法在启动springboot的时候会运行该方法,可根据项目实际情况选择合适调用消息发送方法}@OverridepublicvoidonApplicationEvent(ContextRefreshedEventevent){producter.send();}}

6.启动项目,控制台输出内容:

Springboot中如何整合Activemq

Springboot中如何整合Activemq

7.创建消费者,创建消费者比较容易,只需要监听队列就可以:

packagecom.example.demo.producter.consumer;importorg.springframework.jms.annotation.JmsListener;importorg.springframework.stereotype.Component;@ComponentpublicclassConsumer{@JmsListener(destination="${queue}")publicvoidreceive(Stringmsg){System.out.println("监听器收到msg:"+msg);}}

8.最后结果:

Springboot中如何整合Activemq

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Springboot中如何整合Activemq的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:idea中如何将springboot项目部署到docker下一篇:

8 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18