如何在 Spring 集成中从外部解析和获取 headers 属性 值

How to parse and get headers property values externally in Spring Integration

我需要解析并获取我通过 MessagingGateway 发送的 headers 属性 值,我怀疑它是否与 IntegrationEvaluationContext[=20= 有关]

这是我尝试过但惨遭失败的方法:

@Bean
public IntegrationFlow deleteFiles(){
    return IntegrationFlows.from("integration.channel.bulk-delete")
            .handle(Sftp.outboundGateway(sftpSessionFactory(),
            AbstractRemoteFileOutboundGateway.Command.LS,"headers[path]")
                    .options(AbstractRemoteFileOutboundGateway.Option.NAME_ONLY)
                    .filterFunction(file->{
                     //Here I'm trying to get the age value from headers
                        int age = ExpressionUtils.intExpression("headers[age]").getValue(integrationEvaluationContext.getObject(),Integer.class);
                        Instant decidedTime = Instant.now().minus(age, ChronoUnit.SECONDS);
                        return Instant.ofEpochSecond(file.getAttrs().getMTime()).isBefore(decidedTime);
                    }))
                   .get();

错误信息:

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'headers' cannot be found on null 

我什至尝试过自动装配 StandardEvaluationContext 并传递:

new SpelExpressionParser().parseExpression("headers[age]").getValue(standardEvaluationContext)

如何获取 EvaluationContext 并传递它以便 headers[] 得到解析?

过滤器无权访问网关的输入消息;他们只能访问 LsEntry.

您需要以其他方式提供 age,也许将其存储在 ThreadLocal.

public class AgeHolder {

     private static final ages = new ThreadLocal<Integer>;

     public static setAge(int age) {
         ages.set(age);
     }

     public static Integer getAge() {
         Integer age = ages.get();
         ages.remove();
         return age;
     }

}

然后在表达式中T(AgeHolder).getAge()

或者您可以在 Spring 集成中进行过滤


    .from(Sfp...) // not NAME_ONLY
    .split
    .filter(...) // use the `headers` and `payload`
    .transform(...) -> LsEntry -> name
    .aggregate()
    ...