SpringBoot官方笔记6消息

The Spring Framework provides extensive support for integrating with messaging systems, from simplified use of the JMS API using JmsTemplate to a complete infrastructure to receive messages asynchronously. Spring AMQP provides a similar feature set for the Advanced Message Queuing Protocol. Spring Boot also provides auto-configuration options for RabbitTemplate and RabbitMQ. Spring WebSocket natively includes support for STOMP messaging, and Spring Boot has support for that through starters and a small amount of auto-configuration. Spring Boot also has support for Apache Kafka.

AMQP

The Advanced Message Queuing Protocol (AMQP) is a platform-neutral, wire-level protocol for message-oriented middleware. Spring Boot offers several conveniences for working with AMQP through RabbitMQ, including the spring-boot-starter-amqp "Starter".

RabbitMQ

复制代码
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=secret

spring.rabbitmq.addresses=amqp://admin:secret@localhost

Sending a Message

java 复制代码
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    private final AmqpAdmin amqpAdmin;

    private final AmqpTemplate amqpTemplate;

    public MyBean(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
        this.amqpAdmin = amqpAdmin;
        this.amqpTemplate = amqpTemplate;
    }

    public void someMethod() {
        this.amqpAdmin.getQueueInfo("someQueue");
    }

    public void someOtherMethod() {
        this.amqpTemplate.convertAndSend("hello");
    }

}

Sending a Message To A Stream

复制代码
spring.rabbitmq.stream.name=my-stream

Receiving a Message

java 复制代码
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    @RabbitListener(queues = "someQueue")
    public void processMessage(String content) {
        // ...
    }

}

Apache Kafka

复制代码
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=myGroup

Sending a Message

java 复制代码
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    private final KafkaTemplate<String, String> kafkaTemplate;

    public MyBean(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void someMethod() {
        this.kafkaTemplate.send("someTopic", "Hello");
    }

}

Receiving a Message

java 复制代码
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    @KafkaListener(topics = "someTopic")
    public void processMessage(String content) {
        // ...
    }

}

WebSockets

Spring Boot provides WebSockets auto-configuration for embedded Tomcat, Jetty, and Undertow. If you deploy a war file to a standalone container, Spring Boot assumes that the container is responsible for the configuration of its WebSocket support.

参考资料:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#messaging

相关推荐
indexsunny2 分钟前
互联网大厂Java面试实录:Spring Boot到微服务的深入探讨
java·spring boot·微服务·面试·eureka·kafka·jwt
鸽鸽程序猿6 分钟前
【JavaEE】【SpringAI】Tool Calling(工具调用)
java·java-ee
ZzYH2210 分钟前
文献阅读 260407-Leveraging edge artificial intelligence for sustainable agriculture
笔记
于先生吖11 分钟前
高并发稳定运营,JAVA 动漫短剧小程序 + H5 源码
java·开发语言·小程序
云和数据.ChenGuang13 分钟前
鸿蒙应用对接DeepSeek大模型:构建智能问答系统的技术实践
java·华为·langchain·harmonyos·euler·openduler
曹牧14 分钟前
在 Eclipse 中变更 SVN 地址
java·svn·eclipse
中科三方16 分钟前
域名NS记录修改全攻略:规则、误区、实操流程和常见问题
java·后端·spring
墨白曦煜16 分钟前
告别 Thread.stop():并发编程的最高礼仪——两阶段终止模式
java
重生之我是Java开发战士22 分钟前
【笔试强训】Week1:点击消除,数组中两个字符串的最小距离,dd爱框框,腐烂的苹果,大数乘法
java·开发语言·算法
七夜zippoe26 分钟前
设计模式在Spring等框架中的应用:模板方法、工厂、适配器等
java·spring·设计模式·模板·适配器·工厂