对 JobLaunchRequest 的误解

Misunderstanding with JobLaunchRequest

这是我之前的问题:

项目在注释配置上运行良好,但我希望在 xml 配置上也能使用相同的配置 :)

xml配置:

    <int:service-activator input-channel="fileInputChannel"
                           method="fileWritingMessageHandler"
                           output-channel="jobLaunchRequestChannel">
        <bean class="service.impl.IntegrationServiceImpl"/>
    </int:service-activator>

    <int:service-activator input-channel="jobLaunchRequestChannel"
                           method="jobLaunchRequest"
                           output-channel="jobLaunchingGatewayChannel">
        <bean class="service.impl.IntegrationServiceImpl"/>
    </int:service-activator>

    <batch-int:job-launching-gateway request-channel="jobLaunchingGatewayChannel"
                                     reply-channel="finish"/>

    <int:service-activator input-channel="finish"
                           ref="integrationServiceImpl"
                           method="finishJob">
    </int:service-activator>

IntegrationConfiguration.java:

    @Bean
    public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
        FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(defaultFtpSessionFactory);
        fileSynchronizer.setRemoteDirectory(remoteDirectory);
        fileSynchronizer.setDeleteRemoteFiles(false);
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(channel = "fileInputChannel", poller = @Poller(cron = "*/5 * * * * ?"))
    public FtpInboundFileSynchronizingMessageSource ftpInboundFileSynchronizingMessageSource(FtpInboundFileSynchronizer fileSynchronizer) throws Exception {
        FtpInboundFileSynchronizingMessageSource messageSource = new FtpInboundFileSynchronizingMessageSource(fileSynchronizer);
        messageSource.setAutoCreateLocalDirectory(true);
        messageSource.setLocalDirectory(new File(localDirectory));
        messageSource.setLocalFilter(new AcceptOnceFileListFilter<>());
        return messageSource;
    }

IntegrationServiceImpl:

    @Override
    public FileWritingMessageHandler fileWritingMessageHandler() {
        FileWritingMessageHandler messageHandler = new FileWritingMessageHandler(new File(storageDirectory));
        messageHandler.setDeleteSourceFiles(true);
        messageHandler.setFileNameGenerator(message -> {
            Long timestamp = new Date().getTime();
            log.info(timestamp);
            return "test_" + timestamp;
        });
        return messageHandler;
    }

    @Override
    public JobLaunchRequest jobLaunchRequest(File file) throws IOException {
//    public JobLaunchRequest jobLaunchRequest(FileWritingMessageHandler fileWritingMessageHandler) throws IOException {
        String[] content = FileUtils.readFileToString(file, "UTF-8").split("\s+");
        JobParameters jobParameters = new JobParametersBuilder()
                .addString("filename", file.getAbsolutePath())
                .addString("id", content[0])
                .addString("salary", content[1])
                .toJobParameters();
        log.info(jobParameters);
        return new JobLaunchRequest(increaseSalaryJob, jobParameters);
    }

    @Override
    public void finishJob() {
        log.info("Job finished");
    }

如您所见,此 xml 配置与之前的 post 注释配置类似,但我有一个错误:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method jobLaunchRequest(org.springframework.integration.file.FileWritingMessageHandler) cannot be found on type service.impl.IntegrationServiceImpl 

为什么我不能使用 jobLaunchRequest(File)?如果我需要使用 jobLaunchRequest(FileWritingMessageHandler),我该如何操作文件?

jobLaunchRequest() 方法肯定必须与 File 参数一起使用,因为这确实是 FileWritingMessageHandler.

回复消息中产生的负载。

你的<int:service-activator input-channel="fileInputChannel" method="fileWritingMessageHandler">定义是错误的。

既然您想使用 FileWritingMessageHandler 作为服务,您需要考虑改用 <int-file:outbound-gateway>

service-activator用于调用POJO方法。由于 FileWritingMessageHandler 是一个 MessageHandler 实现,它必须直接从 ref 属性在 <service-activator> 中使用,而不使用任何 method 属性。