使用spring stream发送消息代码实例
更新时间:2020年05月09日 10:40:54 作者:自由港
这篇文章主要介绍了使用spring stream发送消息代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
为什么使用spring stream ?
spring stream 是用来做消息队列发送消息使用的。他隔离了各种消息队列的区别,使用统一的编程模型来发送消息。
目前支持:
- 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>
增加发送接收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
编写代码收发消息:
MessageModel messageModel=new MessageModel();
messageModel.setMsgType("mail");
messageModel.setContent("helloworld");
inputOutput.mailOutput().send( MessageBuilder.withPayload(
"mail"
).build());
inputOutput.output().send(
MessageBuilder.withPayload(
messageModel
).build()
);
这里发送的是两类消息。
接收消息:
@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");
}
}
分别接收两类消息
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
使用java -jar修改SpringBoot中application.properties的配置项
这篇文章主要介绍了使用java -jar修改SpringBoot中application.properties的配置项问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-02-02
详谈HashMap和ConcurrentHashMap的区别(HashMap的底层源码)
下面小编就为大家带来一篇详谈HashMap和ConcurrentHashMap的区别(HashMap的底层源码)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-08-08


最新评论