在 spring 集成中成功发送到 jms 队列后重命名源目录中的文件。使用 ExpressionEvaluatingRequestHandlerAdvice

rename a file in source directory after successfully sending to jms queue , in spring integration. using ExpressionEvaluatingRequestHandlerAdvice

得到以下异常 AdviceMessage [payload=org.springframework.expression.spel.SpelEvaluationException: EL1008E: 在类型 'java.lang.String' 的对象上找不到 属性 或字段 'absolutePath' - 可能不是 public 或无效?

查看源代码..

@Bean
@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
public MessageSource<File> fileReadingMessageSource(@Value("${file.poller.path}") final String path,
        @Value("${file.poller.fileName-pattern}") final String fileExt) {
    CompositeFileListFilter<File> filters = new CompositeFileListFilter<>();
    filters.addFilter(new SimplePatternFileListFilter(fileExt));
    // filters.addFilter(new AcceptOnceFileListFilter<File>());

    FileReadingMessageSource source = new FileReadingMessageSource();
    source.setAutoCreateDirectory(false);
    source.setDirectory(new File(path));
    source.setFilter(filters);
    source.setUseWatchService(true);
    source.setWatchEvents(WatchEventType.CREATE);
    System.out.println(path);
    return source;
}


@Bean
public IntegrationFlow processFile() {
    return IntegrationFlows
                .from("fileInputChannel")           
                .transform(fileToStringTransformer())
                .handle("fileProcessor", "process")//added some data manupilation code and returns Message<String>//
                .log(LoggingHandler.Level.INFO, "process file", m -> m.getHeaders().get("Message_Type"))
                .channel(this.jmsOutboundChannel())

                .get();

}

@Bean
    public IntegrationFlow sendToJmsQueue(JmsTemplate wlsJmsTemplate) {
        return IntegrationFlows.from(this.jmsOutboundChannel())

                .log(LoggingHandler.Level.INFO, "sending to queue", m -> 
                                     m.getHeaders().get("Message_Type"))

                .handle(Jms.outboundAdapter(wlsJmsTemplate).destination(inboundDataQueue), 
                                                                      e -> e.advice(expressionAdvice()))

    }

@Bean
    public Advice expressionAdvice() {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setSuccessChannelName("success.input");
        advice.setOnSuccessExpressionString("payload.delete()");
        //advice.setOnSuccessExpressionString("payload.renameTo(new java.io.File(payload.absolutePath + '.Done'))");
        advice.setFailureChannelName("failure.input");
        advice.setOnFailureExpressionString("payload.renameTo(new java.io.File(payload.absolutePath + '.FailedToSend'))");
        advice.setTrapException(true);
        return advice;
    }

当使用 FileReadingMessageSource 以及将 File 放入负载中时,文件也会添加为 header FileHeaders.ORIGINAL_FILE 以便它可用稍后如果发生转换。

所以你的表达式需要使用

headers['file_originalFile'].renameTo(new java.io.File(headers['file_originalFile']).absolutePath + '.failed')

headers['file_originalFile'].delete()