首页 > 解决方案 > 在spring集成中成功发送到jms队列后重命名源目录中的文件。使用 ExpressionEvaluatingRequestHandlerAdvice

问题描述

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

看源代码。。

@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;
    }

标签: spring-integration

解决方案


使用 aFileReadingMessageSource时,除了将 放入File有效负载之外,该文件还作为标头添加,FileHeaders.ORIGINAL_FILE以便以后发生转换时可用。

所以你的表达式需要使用

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

headers['file_originalFile'].delete()

推荐阅读