Springboot Activemq整合过程代码图解

 更新时间:2020年02月21日 13:41:46   作者:流氓大队长  
这篇文章主要介绍了Springboot Activemq整合过程代码图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了Springboot Activemq整合过程代码图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Springboot+Activemq整合

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:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8080
queue=myqueue

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

package com.example.demo;

import javax.jms.Queue;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
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.core.JmsTemplate;

@Configuration
public class QueueConfig {

  @Value("${queue}")
  private String queue;

  @Bean
  public Queue logQueue() {
    return new ActiveMQQueue(queue);
  }}

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

package com.example.demo.producter;

import javax.jms.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import com.example.demo.SpringbootActivemqApplication;

@Component
public class Producter {
  @Autowired
  private JmsMessagingTemplate jmsMessagingTemplate;
  @Autowired
  private Queue queue;
  private static Logger logger = LoggerFactory.getLogger(
Producter 
.class); public void send() { String str = "生产者生产数据:" + System.currentTimeMillis(); jmsMessagingTemplate.convertAndSend(queue, str); logger.info("生产者数据:{}", str); } }

5.启动类:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.example.demo.producter.Producter;
import com.example.demo.producter.consumer.Consumer;

@SpringBootApplication
@EnableScheduling
public class SpringbootActivemqApplication implements ApplicationListener<ContextRefreshedEvent> {
  @Autowired
  public Producter producter;
  @Autowired
  public Consumer consumer;

  public static void main(String[] args) {
    SpringApplication.run(SpringbootActivemqApplication.class, args);
    //onApplicationEvent方法 在启动springboot的时候 会运行该方法,可根据项目实际情况 选择合适调用消息发送方法

  }

  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {
    producter.send();
  }

}

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

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

package com.example.demo.producter.consumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

  @JmsListener(destination = "${queue}")
  public void receive(String msg) {
    System.out.println("监听器收到msg:" + msg);
  }

}

8.最后结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 使用Spring Cache设置缓存条件操作

    使用Spring Cache设置缓存条件操作

    这篇文章主要介绍了使用Spring Cache设置缓存条件操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Idea如何导入java mysql驱动包

    Idea如何导入java mysql驱动包

    本文介绍了如何在IntelliJ IDEA中配置MySQL数据库连接,首先下载MySQL Connector/J驱动并解压,然后在Idea项目中创建lib文件夹并将.jar文件复制到该文件夹,接着,将.jar文件添加为项目库,通过这些步骤,可以成功配置MySQL数据库连接
    2024-12-12
  • Java之MyBatis入门详解

    Java之MyBatis入门详解

    这篇文章主要介绍了Java之MyBatis入门详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 解决spring-boot2.0.6中webflux无法获得请求IP的问题

    解决spring-boot2.0.6中webflux无法获得请求IP的问题

    这几天在用 spring-boot 2 的 webflux 重构一个工程,写到了一个需要获得客户端请求 IP 的地方,在写的过程中遇到很多问题,下面小编通过一段代码给大家介绍解决spring-boot2.0.6中webflux无法获得请求IP的问题,感兴趣的朋友跟随小编一起看看吧
    2018-10-10
  • Java快速排序与归并排序及基数排序图解示例

    Java快速排序与归并排序及基数排序图解示例

    快速排序是基于二分的思想,对冒泡排序的一种改进。主要思想是确立一个基数,将小于基数的数放到基数左边,大于基数的数字放到基数的右边,然后在对这两部分进一步排序,从而实现对数组的排序
    2022-09-09
  • ScheduledThreadPoolExecutor巨坑解决

    ScheduledThreadPoolExecutor巨坑解决

    这篇文章主要为大家介绍了使用ScheduledThreadPoolExecutor遇到的巨坑解决示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Spring超出最大会话数(Max sessions limit reached: 10000)

    Spring超出最大会话数(Max sessions limit reached: 10000)

    在Spring系统中遇到的Maxsessionslimitreached:10000错误,该错误由于会话数超过默认限制10000而触发,下面就来介绍一下解决方法,感兴趣的可以了解一下
    2024-12-12
  • 微信、支付宝二码合一扫码支付实现思路(java)

    微信、支付宝二码合一扫码支付实现思路(java)

    这篇文章主要为大家详细介绍了微信、支付宝二码合一扫码支付实现思路,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • Java装饰者模式实例详解

    Java装饰者模式实例详解

    这篇文章主要介绍了Java装饰者模式,结合实例形式详细分析了装饰着模式的原理与java具体实现技巧,需要的朋友可以参考下
    2017-09-09
  • Java实现代码块耗时测算工具类

    Java实现代码块耗时测算工具类

    这篇文章主要为大家介绍了如何利用Java语言编写一个工具类,用来测算代码块的耗时,同时还能显示进度,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-05-05

最新评论