使用spring stream發(fā)送消息代碼實例
為什么使用spring stream ?
spring stream 是用來做消息隊列發(fā)送消息使用的。他隔離了各種消息隊列的區(qū)別,使用統(tǒng)一的編程模型來發(fā)送消息。
目前支持:
- rabbitmq
- kafka
- rocketmq
啟動rocketmq
rocketmq 支持windows
start mqnamesrv.cmd start mqbroker.cmd -n 127.0.0.1:9876 autoCreateTopicEnable=true
修改pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
</dependency>
增加發(fā)送接收JAVA代碼
public interface InputOutput {
String MAIL_OUTPUT = "mailOutput";
String MAIL_INPUT = "mailInput";
String OUTPUT = "output";
String INPUT = "input";
@Output(OUTPUT)
MessageChannel output();
@Input(INPUT)
SubscribableChannel input();
@Output(MAIL_OUTPUT)
MessageChannel mailOutput();
@Input(MAIL_INPUT)
SubscribableChannel mailInput();
}
在應用上增加注解
@EnableBinding({InputOutput.class})
增加yml配置
spring:
cloud:
stream:
rocketmq:
binder:
name-server: 127.0.0.1:9876
bindings:
output:
destination: bpmmessage
group: bpmmessage-group
input:
destination: bpmmessage
group: bpmmessage-group-consumer
mailOutput:
destination: mail
group: mail-group
mailInput:
destination: mail
group: mail-group-consumer
編寫代碼收發(fā)消息:
MessageModel messageModel=new MessageModel();
messageModel.setMsgType("mail");
messageModel.setContent("helloworld");
inputOutput.mailOutput().send( MessageBuilder.withPayload(
"mail"
).build());
inputOutput.output().send(
MessageBuilder.withPayload(
messageModel
).build()
);
這里發(fā)送的是兩類消息。
接收消息:
@Service
public class MessageListener {
@StreamListener(InputOutput.INPUT)
public void receive(MessageModel message) {
System.err.println(message);
System.err.println("ok");
}
@StreamListener(InputOutput.MAIL_INPUT)
public void receive(String message) {
System.err.println(message);
System.err.println("ok");
}
}
分別接收兩類消息
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Springboot Session共享實現(xiàn)原理及代碼實例
這篇文章主要介紹了Springboot Session共享實現(xiàn)原理及代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
使用java?-jar修改SpringBoot中application.properties的配置項
這篇文章主要介紹了使用java?-jar修改SpringBoot中application.properties的配置項問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Java優(yōu)化if-else代碼的實戰(zhàn)記錄
開發(fā)中經(jīng)常會根據(jù)不同的參數(shù)判斷走不同的邏輯業(yè)務,我們常用的方法就是if/else嵌套使用,導致每增加一個需求就加一個if,慢慢的就會發(fā)現(xiàn)自己寫的代碼中出現(xiàn)了大量的if/else,這篇文章主要給大家介紹了關于Java優(yōu)化if-else代碼的相關資料,需要的朋友可以參考下2021-09-09
詳談HashMap和ConcurrentHashMap的區(qū)別(HashMap的底層源碼)
下面小編就為大家?guī)硪黄斦凥ashMap和ConcurrentHashMap的區(qū)別(HashMap的底層源碼)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

