springCloudStream整合rabbitMq的简易DEMO

spring:
  cloud:
    stream:
      bindings:
        my_output:  #自定义的输出通道
          destination: zzz  #交换机名
          content-type: application/json
        my_input:   #自定义的输入通道
          destination: zzz
          content-type: application/json
          group: one   #通过这个指定队列名,zzz.one
      rabbit:
        bindings:
          my_output:
            producer: #设置生产者的参数
              exchangeType: topic
              routing-key-expression: '''test.one'''
          my_input:
            consumer:  #设置消费者的参数
              bindingRoutingKey: test.one
              acknowledgeMode: manual

public interface StreamMessageChanel {
    String MY_OUTPUT = "my_output";
    String MY_INPUT = "my_input";

    @Output(StreamMessageChanel.MY_OUTPUT)
    MessageChannel sendMessage();

    @Input(StreamMessageChanel.MY_INPUT)
    SubscribableChannel dealMessage();

}
@Component
@EnableBinding(StreamMessageChanel.class)
public class InputOutBean {
    @Resource
    private StreamMessageChanel streamMessageChanelImpl;
    @StreamListener(StreamMessageChanel.MY_INPUT)
    @SendTo(StreamMessageChanel.MY_OUTPUT)
    public String input(Message<String> message, @Header(AmqpHeaders.CHANNEL) Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag) {
        try {
            System.out.println("接收到消息!!!!!!!");
            channel.basicAck(deliveryTag, false);
            System.out.println(message.getPayload().toString());
        } catch (Exception e) {
            System.out.println("error");
        }
        return "循环!!!";
    }
    public void output(String data) {
        System.out.println("发送消息-------------------");
        streamMessageChanelImpl.sendMessage().send(MessageBuilder.withPayload(data).build());
    }

}
@RestController
@RequestMapping("/queue")
public class TestMqController {

    @Resource
    private InputOutBean inputOutBean;

    @GetMapping("/one")
    public KeyValue one() {
        inputOutBean.output("测试队列");
        return KeyValue.ok("over");
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值