使用春流发送消息的方法

  介绍

这篇文章主要讲解了使用春流发送消息的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

<强>为什么使用春天stream 

春流是用来做消息队列发送消息使用的。他隔离了各种消息队列的区别,使用统一的编程模型来发送消息。

目前支持:

    <李> rabbitmq李 <李>卡夫卡李 <>李rocketmq李

启动rocketmq

rocketmq支持windows

 & lt; dependency>
  & lt; groupId> com.alibaba.cloud
  & lt; artifactId> spring-cloud-stream-binder-rocketmq
  & lt;/dependency> 

增加发送接收JAVA代码

公共接口输入输出{
  字符串MAIL_OUTPUT=癿ailOutput";
  字符串MAIL_INPUT=癿ailInput";
  字符串输出=皁utput";
  字符串输入=癷nput";
  @Output(输出)
  MessageChannel输出();
  @Input(输入)
  SubscribableChannel输入();
  @Output (MAIL_OUTPUT)
  MessageChannel mailOutput ();
  @Input (MAIL_INPUT)
  SubscribableChannel mailInput ();
  }

在应用上增加注解

@EnableBinding ({InputOutput.class})

增加yml配置

春:   云:   流:   rocketmq:   粘结剂:   名称服务器:127.0.0.1:9876   绑定:   输出:   目的地:bpmmessage   组:bpmmessage-group      输入:   目的地:bpmmessage   组:bpmmessage-group-consumer      mailOutput:   目的地:邮件   组:邮件集团      mailInput:   目的地:邮件   组:mail-group-consumer

编写代码收发消息:

 MessageModel MessageModel=new MessageModel ();
  
  messageModel.setMsgType (“mail");
  messageModel.setContent (“helloworld");
  
  inputOutput.mailOutput ()。发送(MessageBuilder.withPayload (
  “mail"
  ).build ());
  
  inputOutput.output () .send (
  MessageBuilder.withPayload (
  messageModel
  ).build ()
  );

这里发送的是两类消息。

接收消息:

@ service   公开课MessageListener {      @StreamListener (InputOutput.INPUT)   公共空间接收(MessageModel消息){   System.err.println(消息);   System.err.println (“ok");   }         @StreamListener (InputOutput.MAIL_INPUT)   公共空间接收(字符串消息){   System.err.println(消息);   System.err.println (“ok");   }   }

分别接收两类消息

看完上述内容,是不是对使用春流发送消息的方法有进一步的了解,如果还想学习更多内容,欢迎关注行业资讯频道。

使用春流发送消息的方法