是否有可能使方法(处理程序)从Spring Cloud Stream中的多个入站通道接收消息?

问题描述 投票:0回答:2

假设我对Sink接口具有此定义

public interface Sink {
  @Input("input")
  SubscribableChannel input();

  @Input("anotherInput")
  SubscribableChannel anotherInput();
}

并且以下控制器已绑定到RabbitMQ(均已根据Spring Cloud Stream Binder Rabbit正确设置)

@Controller
@EnableBinding(Sink.class)
public class InputMessageController {

    @StreamListener("input")
    public void handle(String message) {
        System.out.println("Received from input: " + message);
    }

    @StreamListener("anotherInput")
    public void handleOther(String message) {
        System.out.println("Received from another input: " + message);
    }

尽管可以,但是由于重复等原因,它是不好的代码。>>

我想让这个控制器中的一个处理程序监听,换句话说,它订阅Sink接口中的两个通道,并根据某些条件处理这两个通道。到目前为止,我无法使用标准的@StreamListener注释来弄清楚,因为它显然不接受String(通道名称)数组。

我的目标是这样的:

@Controller
@EnableBinding(Sink.class)
public class InputMessageController {

    @StreamListener("input", "anotherInput")
    public void handle(String message) {
        System.out.println("Received " + message + "from " +
                   ((/*some condition*/) ? "1st" : "2nd") + " input");
    }

我很高兴有任何思路可以解决它,也不能对Spring Cloud Stream API进行解释!

假设我对接收器接口公共接口具有此定义Sink {@Input(“ input”)SubscribableChannel input(); @Input(“ anotherInput”)SubscribableChannel anotherInput(); }和...

java spring rabbitmq microservices spring-cloud-stream
2个回答
1
投票

用Rabbitlistener注释您的类,并将队列声明为参数,然后使用Rabbithandler注释方法。此方法接收所有已声明队列的输入。

@Controller
@EnableBinding(Sink.class)
@RabbitListener(queues = {"input", "anotherInput"})
public class InputMessageController {

    @RabbitHandler
    public void handle(String message) {
        System.out.println("Received " + message + "from " +
                   ((/*some condition*/) ? "1st" : "2nd") + " input");
    }

1
投票

当前,唯一的方法是这样

© www.soinside.com 2019 - 2024. All rights reserved.