【问题标题】:Spring batch integration : java.lang.ClassCastException: org.springframework.integration.file.FileReadingMessageSource cannot be cast to java.io.FileSpring批量集成:java.lang.ClassCastException:org.springframework.integration.file.FileReadingMessageSource 无法转换为java.io.File
【发布时间】:2019-04-10 20:13:11
【问题描述】:
@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "15000"))
public MessageSource<File> fileReadingMessageSource() {

    FileReadingMessageSource source = new FileReadingMessageSource();
    source.setDirectory(new File(ftpUploadDir));
    source.setFilter(new SimplePatternFileListFilter("*.csv"));
    source.setScanEachPoll(true);

    source.setUseWatchService(true);


    return source;
}

@Transformer(inputChannel = "fileInputChannel", outputChannel = "jobChannel")
public JobLaunchRequest transform(Message<File>  file) {

    log.info( file.getPayload().toString());


    JobParameters jobParameters = new JobParametersBuilder()
            .addString("fileName", file.getPayload().getAbsolutePath())
            .addDate("dateTime", new Date())
            .toJobParameters();


    JobLaunchRequest request = new JobLaunchRequest(job, jobParameters);

    return request;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!为了帮助其他人理解和回答您的问题,请您:1. 清楚地说明您的目标和您面临的问题 2. 显示错误发生的确切时间并提供堆栈跟踪 3. 适当地格式化代码
  • 请提供更多背景信息以便为您提供帮助。
  • 请看我的回答。

标签: spring-boot spring-integration spring-batch


【解决方案1】:

您还需要在fileReadingMessageSource() 方法中添加@Bean 注释以及@InboundChannelAdapter

关键是,如果没有 @Bean,框架将根据 fileReadingMessageSource() 方法调用的结果创建带有 MethodInvokingMessageSourceSourcePollingChannelAdapter

有关详细信息,请参阅 Spring 集成参考手册:https://docs.spring.io/spring-integration/docs/5.0.9.RELEASE/reference/html/configuration.html#annotations

【讨论】:

  • 谢谢阿特姆。我现在有点卡住了。我在调试模式下在应用程序中运行。我可以看到控制进入 fileReadingMessageSource Bean ,控制没有进入 FileToJobTransformer 的转换方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 2014-05-12
  • 2013-06-03
  • 2018-08-11
  • 2021-02-20
相关资源
最近更新 更多