Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Events 无法使用Spring Cloud Stream@StreamListener注释中的条件属性筛选收到的消息_Events_Apache Kafka_Spring Integration_Spring Cloud Stream - Fatal编程技术网

Events 无法使用Spring Cloud Stream@StreamListener注释中的条件属性筛选收到的消息

Events 无法使用Spring Cloud Stream@StreamListener注释中的条件属性筛选收到的消息,events,apache-kafka,spring-integration,spring-cloud-stream,Events,Apache Kafka,Spring Integration,Spring Cloud Stream,我正在尝试创建一个基于事件的系统,用于使用ApacheKafka作为消息传递系统和SpringCloudStreamKafka在服务之间进行通信 我已经编写了如下的Receiver类方法 @StreamListener(target = Sink.INPUT, condition = "headers['eventType']=='EmployeeCreatedEvent'") public void handleEmployeeCreatedEvent(@Payload Str

我正在尝试创建一个基于事件的系统,用于使用ApacheKafka作为消息传递系统和SpringCloudStreamKafka在服务之间进行通信

我已经编写了如下的Receiver类方法

    @StreamListener(target = Sink.INPUT, condition = "headers['eventType']=='EmployeeCreatedEvent'")
    public void handleEmployeeCreatedEvent(@Payload String payload) {
        logger.info("Received EmployeeCreatedEvent: " + payload);
    }
@Component
@EnableBinding(Source.class)
public class CustomMessageSource {
    @Autowired
    private Source source;                


    public void  sendMessage(String payload,String eventType) {
        Message<String> myMessage = MessageBuilder.withPayload(payload)
                .setHeader("eventType", eventType)
                .build();
        source.output().send(myMessage);

     }

}
customMessageSource.sendMessage("Hello","EmployeeCreatedEvent");
@Autowired
CustomMessageSource customMessageSource;
此方法专门用于捕获与EmployeeCreatedEvent相关的消息或事件

    @StreamListener(target = Sink.INPUT, condition = "headers['eventType']=='EmployeeTransferredEvent'")
    public void handleEmployeeTransferredEvent(@Payload String payload) {
        logger.info("Received EmployeeTransferredEvent: " + payload);
    }
此方法专门用于捕获与EmployeeTransferredEvent相关的消息或事件

    @StreamListener(target = Sink.INPUT)
    public void handleDefaultEvent(@Payload String payload) {
        logger.info("Received payload: " + payload);
    }
这是默认方法

当我运行应用程序时,我无法看到被调用的带有条件属性的方法。我只看到调用handleDefaultEvent方法

我正在使用下面的CustomMessageSource类从发送/源应用程序向此接收器应用程序发送消息,如下所示

    @StreamListener(target = Sink.INPUT, condition = "headers['eventType']=='EmployeeCreatedEvent'")
    public void handleEmployeeCreatedEvent(@Payload String payload) {
        logger.info("Received EmployeeCreatedEvent: " + payload);
    }
@Component
@EnableBinding(Source.class)
public class CustomMessageSource {
    @Autowired
    private Source source;                


    public void  sendMessage(String payload,String eventType) {
        Message<String> myMessage = MessageBuilder.withPayload(payload)
                .setHeader("eventType", eventType)
                .build();
        source.output().send(myMessage);

     }

}
customMessageSource.sendMessage("Hello","EmployeeCreatedEvent");
@Autowired
CustomMessageSource customMessageSource;
customMessageSource实例自动连接如下:

    @StreamListener(target = Sink.INPUT, condition = "headers['eventType']=='EmployeeCreatedEvent'")
    public void handleEmployeeCreatedEvent(@Payload String payload) {
        logger.info("Received EmployeeCreatedEvent: " + payload);
    }
@Component
@EnableBinding(Source.class)
public class CustomMessageSource {
    @Autowired
    private Source source;                


    public void  sendMessage(String payload,String eventType) {
        Message<String> myMessage = MessageBuilder.withPayload(payload)
                .setHeader("eventType", eventType)
                .build();
        source.output().send(myMessage);

     }

}
customMessageSource.sendMessage("Hello","EmployeeCreatedEvent");
@Autowired
CustomMessageSource customMessageSource;
基本上,我想过滤接收器/接收器应用程序接收到的消息,并相应地处理它们

为此,我使用带有条件属性的@StreamListener注释来模拟处理不同事件的行为

我正在使用SpringCloudStreamChelsea.SR2版本


有人能帮我解决这个问题吗。

似乎没有传播标题。确保在spring.cloud.stream.kafka.binder.headers中包含自定义标题

谢谢你的回复。但是,我没有得到你的回应。我使用有效负载和作为输入接收的头显式构造消息。我不确定为什么我应该在配置文件中包含自定义头。Spring cloud stream默认情况下不会传播自定义头。此设置将告诉生产者应用程序我们应该这样做;我们必须将它们嵌入到数据中;因此,选择加入的方法,以避免添加不必要的数据。谢谢,这是有意义的。