如何在@StreamListener 中添加日期条件

How to add date condition in @StreamListener

假设,我能够通过 Kafka 或 RabbitMQ 发送消息并使用 @StreamListener 使用它们。

绑定器配置为 content-type = 'application/json',所以我想可以在有效载荷上添加条件。

我的要求是仅当字段的日期早于 now() 时才获取元素。当此条件为真时,将消耗所有其他元素。

例如,我希望是这样的:

@StreamListener(value = INPUT, condition = "data.startDate > now()")
public void onMessage(@Payload Data data) { 
    // ...
}

第一个问题是我收到错误:EL1008E: 属性 或字段 'startDate' 无法在 'byte[]' 类型的对象上找到 - 也许不是 public有效吗?

这是正确的,因为有效负载没有转换为任何类型,而是以 byte[] 的原始形式存在。请参阅 this section 具体 "Content Type Negotiation in the Context of condition" 子部分。

但是,对于您的情况,您可以简单地将表达式修改为类似 new String(payload).... 的内容,知道它在转换之前总是 byte[]

我认为答案在文档中并且符合 Oleg 的答案

That is because you are testing something that does not yet exist in a state you expect. That is because the payload of the message is not yet converted from the wire format (byte[]) to the desired type. In other words, it has not yet gone through the type conversion process described in the Chapter 10, Content Type Negotiation.

So, unless you use a SPeL expression that evaluates raw data (for example, the value of the first byte in the byte array), use message header-based expressions (such as condition = "headers['type']=='dog'").