Spring 来自控制器的批量集成 JobLaunchRequest

Spring Batch Integration JobLaunchRequest from Controller

我有一个控制器,我想从中 运行 一个 Spring 批处理作业。 FilePoller 充当轮询器,运行 按计划进行,但我们也希望手动 运行 它。 FilePoller 按 cron 计划工作,而 JobLaunchRequest 以这种方式工作。但是当我们使用从控制器调用的 JobLaunchRequest 时,没有任何反应——Spring 批处理作业没有启动。这是控制器:

@Controller
public class PollerController {

@Autowired
FilePoller FilePoller;

@Autowired
private ApplicationContext appContext;

@RequestMapping(value = "ui/manualPoll.action", method = RequestMethod.GET)
public void manualPollRequest() {

    Message<File> message = filePoller.fileMessageSource().receive();

    filePoller.setFileParameterName(message.getPayload().getName());        
    filePoller.setJob((Job)appContext.getBean("myJob"));

    filePoller.toRequest(message);
}

消息负载具有文件名,我从应用程序上下文中将 Spring 批处理作业发送到 运行。我已经调试并逐步执行代码,并确保消息负载中的文件名不为空,并且 Spring 批处理作业也不为空。在 FilePoller class 里面我有这个:

@Configuration
@PropertySource("classpath:my.properties") 
@EnableIntegration
@IntegrationComponentScan
public class FilePoller {

private Job job;

private String fileParameterName;

@Autowired
MyProperty myProperty;

public void setFileParameterName(String fileParameterName) {
    this.fileParameterName = fileParameterName;
}

public void setJob(Job job) {
    this.job = job;
}

@Bean
@InboundChannelAdapter(value = "inboundFileChannel", poller = @Poller(cron="${my/POLLER}"))
public MessageSource<File> fileMessageSource() {
    FileReadingMessageSource source = initialSetUp();
    source.setDirectory(new File(myProperty.getProperty(MyConstants.WORKING_DIR)))
    return source;
}

private FileReadingMessageSource initialSetUp() {
    FileReadingMessageSource source = new FileReadingMessageSource();
    CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<File>(); 
    SimplePatternFileListFilter simplePatternFileListFilter = new SimplePatternFileListFilter("*.done");
    AcceptOnceFileListFilter<File> acceptOnceFileListFilter = new AcceptOnceFileListFilter<File>();

    compositeFileListFilter.addFilter(simplePatternFileListFilter);
    compositeFileListFilter.addFilter(acceptOnceFileListFilter);

    source.setFilter(compositeFileListFilter);
    return source;
}

@Transformer
public JobLaunchRequest toRequest(Message<File> message) {
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addString(fileParameterName, message.getPayload().getAbsolutePath());
    return new JobLaunchRequest(job, jobParametersBuilder.toJobParameters());
}

JobLaunchRequest 似乎没有执行任何操作。我从来没有进入我的第一步,在 XML 配置中显示:

<int:annotation-config />           
<int:channel id="inboundFileChannel" />
<int:channel id="outboundJobRequestChannel" />
<int:channel id="jobLaunchReplyChannel" />  

<int:transformer input-channel="inboundFileChannel"
    output-channel="outboundJobRequestChannel">
    <bean
        class="org.my.poller.FilePoller">
        <property name="job" ref="myJob" />
        <property name="fileParameterName" value="input.file.name" />
    </bean>
</int:transformer>

<batch-int:job-launching-gateway request-channel="outboundJobRequestChannel" reply-channel="jobLaunchReplyChannel" />
<int:logging-channel-adapter channel="jobLaunchReplyChannel" />



<job id="myJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="Step1" next="Step2">
            <tasklet ref="checkifFileinLogTbl"/>

更新 谢谢,加里。现在可以了。如果其他人有兴趣:

@Autowired
MessageChannel outboundJobRequestChannel;

@Autowired
MessagingTemplate template;

@RequestMapping(value = "ui/manualPoll.action", method = RequestMethod.GET)
public void manualPollRequest() {

Message<File> message = filePoller.fileMessageSource().receive();

    //if message !=null, there is a file present on inboundFileChannel
    if(message !=null){
        filePoller.setFileParameterName("input.file.name");
        filePoller.setJob((Job) appContext.getBean("myJob"));

        template.convertAndSend(outboundJobRequestChannel, filePoller.toRequest(message));
    }

并且在 XML 我添加了:

<bean class="org.springframework.integration.core.MessagingTemplate" />
filePoller.toRequest(message);

只是构建请求对象。

您需要将其发送至 outboundJobRequestChannel

使用 MessagingTemplate (convertSendAndReceive()) 或 MessagingGateway 来做到这一点。