首页 > 其他分享 >SpringBoot官方笔记6消息

SpringBoot官方笔记6消息

时间:2023-07-17 22:56:25浏览次数:44  
标签:Spring SpringBoot spring springframework 官方 笔记 org import public

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

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

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

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

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

标签:Spring,SpringBoot,spring,springframework,官方,笔记,org,import,public
From: https://www.cnblogs.com/df888/p/17561526.html

相关文章

  • SpringBoot官方笔记5Data
    SpringBootintegrateswithanumberofdatatechnologies,bothSQLandNoSQL.SQLDatabasesSpringData providesanadditionalleveloffunctionality:creating Repository implementationsdirectlyfrominterfacesandusingconventionstogeneratequeries......
  • SpringBoot官方笔记8其他
    ContainerImagesFROMeclipse-temurin:17-jreasbuilderWORKDIRapplicationARGJAR_FILE=target/*.jarCOPY${JAR_FILE}application.jarRUNjava-Djarmode=layertools-jarapplication.jarextractFROMeclipse-temurin:17-jreWORKDIRapplicationCOPY--from=......
  • 决策单调性优化DP 学习笔记 & P4767 [IOI2000] 邮局 题解
    0.题面题目描述高速公路旁边有一些村庄。高速公路表示为整数轴,每个村庄的位置用单个整数坐标标识。没有两个在同样地方的村庄。两个位置之间的距离是其整数坐标差的绝对值。邮局将建在一些,但不一定是所有的村庄中。为了建立邮局,应选择他们建造的位置,使每个村庄与其最近的邮局......
  • 组合数学学习笔记
    组合数学学习笔记组合数学常用公式基本公式排列:\[A_{n}^r=\frac{n!}{(n-r)!}\]组合:\[C_{n}^r=\frac{n!}{r!(n-r)!}\\\dbinom{n}{r}=\frac{n!}{r!(n-r)!}\]组合公式杨辉恒等式......
  • 第一篇博客 练习typora笔记
    学习MarkDown字体helloworld!helloworld!helloworld!helloworld! 引用 乐交诤友不交损友 分割线 图片  超链接点击跳转到百度 列表ABC 无序列表ABC 列表姓名性別年齡張三男18 代碼publicvoid......
  • springboot下使用rabbitMQ之开发配置方式(一)
    springboot下使用rabbitMQ之开发配置方式(一)距离上次发布博客已经小一年了,这次...嗯,没错,我又回来啦.........
  • C++笔记(2)——函数
    六.函数6.1函数基础一个典型的函数(function)定义包括:返回类型(returntype)、函数名字,由0或多个形参(parameter)组成的列表以及函数体。我们通过调用运算符来执行函数,形式为"()"。函数调用完成两项工作:一是用实参初始化函数对应的形参,二是将控制权转移给被调用函数。此时,主调......
  • mysql 笔记
    行转列: namecoursegradezhangsanjava20zhangsanc#60zhangsanpython40lisijava109lisic#30lisipython20wangwujava33 selectname,sum(casewhencourse='java'thengradeend)as'java',sum(casewhen......
  • 小红书获得小红书笔记详情 API 返回值说明
    ​ item_get_video-获得小红书笔记详情 注册开通smallredbook.item_get_video公共参数名称类型必须描述keyString是调用key(必须以GET方式拼接在URL中)secretString是调用密钥api_nameString是API接口名称(包括在请求地址中)[item_search,item_get,item_s......
  • Learning hard C#学习笔记——读书笔记 03
    C#是面向对象的语言,每次到这里就会有一个问题,什么是对象,其实一句话就可以解释,那就是——万物皆是对象,这句话就像“如来”一样抽象,其实,我们无须在这上面耗费太大的精力,我们随着学习的深入,对象的概念自然会深入到脑海中所有面向对象的编程语言都有以下三个基础特征封装——把客......