使用spring stream發(fā)送消息代碼實(shí)例
為什么使用spring stream ?
spring stream 是用來(lái)做消息隊(duì)列發(fā)送消息使用的。他隔離了各種消息隊(duì)列的區(qū)別,使用統(tǒng)一的編程模型來(lái)發(fā)送消息。
目前支持:
rabbitmq kafka rocketmq啟動(dòng)rocketmq
rocketmq 支持windows
start mqnamesrv.cmdstart 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ā)送接收J(rèn)AVA代碼
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();}
在應(yīng)用上增加注解
@EnableBinding({InputOutput.class})
增加yml配置
spring: cloud: stream: rocketmq: binder: name-server: 127.0.0.1:9876 bindings: output: destination: bpmmessage group: bpmmessage-groupinput: destination: bpmmessage group: bpmmessage-group-consumermailOutput: destination: mail group: mail-groupmailInput:destination: mailgroup: 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ā)送的是兩類消息。
接收消息:
@Servicepublic 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'); }}
分別接收兩類消息
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼2. XML入門的常見(jiàn)問(wèn)題(一)3. ASP實(shí)現(xiàn)加法驗(yàn)證碼4. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法5. ASP.NET MVC使用異步Action的方法6. 匹配模式 - XSL教程 - 47. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容8. JS中map和parseInt的用法詳解9. XML入門精解之結(jié)構(gòu)與語(yǔ)法10. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera
