Spring Boot与JMS消息中间件的集成

Spring Boot与JMS消息中间件的集成

大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们来探讨一下如何在Spring Boot中集成JMS(Java Message Service)消息中间件。

一、JMS简介

JMS是Java平台中的一项消息传递API,用于在两个应用程序或分布式系统之间发送消息。它提供了一种异步通信机制,可以提高系统的可扩展性和解耦性。常见的JMS消息中间件包括ActiveMQ、RabbitMQ和IBM MQ。

二、Spring Boot集成ActiveMQ

1. 引入ActiveMQ依赖

pom.xml文件中添加ActiveMQ的依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.16.3</version>
</dependency>
2. 配置ActiveMQ连接

application.properties文件中配置ActiveMQ连接信息:

properties 复制代码
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
3. 创建消息生产者
java 复制代码
package cn.juwatech.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class MessageProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public void sendMessage(String destination, String message) {
        jmsTemplate.convertAndSend(destination, message);
    }
}
4. 创建消息消费者
java 复制代码
package cn.juwatech.jms;

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

@Component
public class MessageConsumer {

    @JmsListener(destination = "test.queue")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}
5. 启用JMS监听

在Spring Boot应用主类中添加@EnableJms注解:

java 复制代码
package cn.juwatech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;

@SpringBootApplication
@EnableJms
public class JmsApplication {
    public static void main(String[] args) {
        SpringApplication.run(JmsApplication.class, args);
    }
}

三、Spring Boot集成RabbitMQ

1. 引入RabbitMQ依赖

pom.xml文件中添加RabbitMQ的依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2. 配置RabbitMQ连接

application.properties文件中配置RabbitMQ连接信息:

properties 复制代码
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3. 创建消息生产者
java 复制代码
package cn.juwatech.rabbitmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String exchange, String routingKey, String message) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
    }
}
4. 创建消息消费者
java 复制代码
package cn.juwatech.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQConsumer {

    @RabbitListener(queues = "test.queue")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}
5. 配置RabbitMQ队列、交换机和绑定
java 复制代码
package cn.juwatech.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue queue() {
        return new Queue("test.queue", false);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange("test.exchange");
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("test.routingkey");
    }
}

四、总结

通过本文的介绍,我们了解了如何在Spring Boot中集成ActiveMQ和RabbitMQ作为JMS消息中间件。通过这些配置和代码示例,可以轻松地在Spring Boot应用中实现消息的发送和接收,提升系统的可扩展性和解耦性。

相关推荐
xieliyu.37 分钟前
Java算法精讲:双指针(三)
java·开发语言·算法
星辰徐哥1 小时前
Spring Boot 微服务架构设计与实现
spring boot·后端·微服务
星辰徐哥1 小时前
Spring Boot 数据导入导出与报表生成
spring boot·后端·ui
明夜之约1 小时前
Spring Boot 自动装配源码
java·spring boot·后端
Leaton Lee1 小时前
Spring Boot分层架构详解:从Controller到Service再到Mapper的完整流程
java·spring boot·后端·架构
Micro麦可乐1 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统
Jinkxs1 小时前
Resilience4j- 与 Spring Boot 快速集成:自动配置与基础注解使用
java·spring boot·后端
毕设源码_郑学姐1 小时前
计算机毕业设计springboot网络相册设计与实现 基于Spring Boot框架的在线相册管理系统开发与应用 Spring Boot驱动的网络影集设计与实践
spring boot·后端·课程设计
辣机小司1 小时前
【踩坑记录:Spring Boot 配置文件读取值不一致?警惕 YAML 的“八进制陷阱”与 SnakeYAML 版本之谜】
java·spring boot·后端·yaml·踩坑记录
一条小锦吕*1 小时前
基于Spring Boot + 数据可视化 + 协同过滤算法的推荐系统设计与实现(源码+论文+部署全讲解)
spring boot·算法·信息可视化