Spring integration Spring集成轮询器在触发cron表达式时启动Spring批处理作业一次

Spring integration Spring集成轮询器在触发cron表达式时启动Spring批处理作业一次,spring-integration,spring-batch,Spring Integration,Spring Batch,我们希望Spring批处理作业在cron表达式每2分钟触发一次时触发一次。但我们注意到,当它被触发时,它会持续运行——第一个作业完成后,就会启动第二个作业,如日志中所示: 2016-04-08 21:18:02,426 INFO [org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run() 133] - Job: [FlowJob: [name=irsDataPrepJob]] launched with the

我们希望Spring批处理作业在cron表达式每2分钟触发一次时触发一次。但我们注意到,当它被触发时,它会持续运行——第一个作业完成后,就会启动第二个作业,如日志中所示:

2016-04-08 21:18:02,426 INFO [org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run() 133] - Job: [FlowJob: [name=irsDataPrepJob]] launched with the following parameters: [{subscriberReaderType=originalSubscriberReaderType, time=1460175480034}]
2016-04-08 21:18:03,095 INFO [xxx.batch.listener.IrsJobExecutionListener.beforeJob() 41] - Started jobName=[irsDataPrepJob], jobId=[99018], jobParameters={subscriberReaderType=originalSubscriberReaderType, time=1460175480034}]
2016-04-08 21:18:05,738 INFO [org.springframework.batch.core.job.SimpleStepHandler.handleStep() 146] - Executing step: [generateIrsData]
.
.
.
2016-04-08 21:18:15,264 INFO [xxx.batch.listener.IrsJobExecutionListener.afterJob() 54] - Finished jobName=[irsDataPrepJob], jobId=[99018], with status=[COMPLETED], jobParameters={subscriberReaderType=originalSubscriberReaderType, time=1460175480034}]
2016-04-08 21:18:15,929 INFO [org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run() 136] - Job: [FlowJob: [name=irsDataPrepJob]] completed with the following parameters: [{subscriberReaderType=originalSubscriberReaderType, time=1460175480034}] and the following status: [COMPLETED]
2016-04-08 21:18:15,933 INFO [org.springframework.integration.handler.LoggingHandler.handleMessageInternal() 155] - JobExecution: id=99282, version=2, startTime=Fri Apr 08 21:18:02 PDT 2016, endTime=Fri Apr 08 21:18:15 PDT 2016, lastUpdated=Fri Apr 08 21:18:15 PDT 2016, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=99018, version=0, Job=[irsDataPrepJob]], jobParameters=[{subscriberReaderType=originalSubscriberReaderType, time=1460175480034}]
2016-04-08 21:18:17,826 INFO [org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run() 133] - Job: [FlowJob: [name=irsDataPrepJob]] launched with the following parameters: [{subscriberReaderType=originalSubscriberReaderType, time=1460175495933}]
我们有这样一个cron表达式:

batch.job.schedule.cron.irsDataPrepJobRunner=0 0/2 * * * *
@Configuration
@EnableIntegration
@IntegrationComponentScan
public class IrsJobIntegration {

@Autowired
private ApplicationContext appContext;

@Bean
MessageChannel control() {
    return new DirectChannel();
}

@Bean
@ServiceActivator(inputChannel="irsControl")
public ExpressionControlBusFactoryBean irsControlBus() {
    return new ExpressionControlBusFactoryBean();
}

@Bean
@InboundChannelAdapter(value = "irsDataPrepJobInputChannel", poller = @Poller(cron="${batch.job.schedule.cron.irsDataPrepJobRunner}"))
public MessageSource<JobLaunchRequest> pollIrsDataPrepJob() {

    return new MessageSource<JobLaunchRequest>() {

        @Override
        public Message<JobLaunchRequest> receive() {
            return new GenericMessage<JobLaunchRequest>(requestIrsDataPrepJob());
        }

    };
}

@Transformer(inputChannel="irsDataPrepJobInputChannel",outputChannel="irsDataPrepJobOutputChannel")
public JobLaunchRequest requestIrsDataPrepJob() {
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addString("subscriberReaderType", "originalSubscriberReaderType").addLong("time",System.currentTimeMillis());
    return new JobLaunchRequest((Job) appContext.getBean("irsDataPrepJob"), jobParametersBuilder.toJobParameters());
}
我们的JavaConfig bean如下所示:

batch.job.schedule.cron.irsDataPrepJobRunner=0 0/2 * * * *
@Configuration
@EnableIntegration
@IntegrationComponentScan
public class IrsJobIntegration {

@Autowired
private ApplicationContext appContext;

@Bean
MessageChannel control() {
    return new DirectChannel();
}

@Bean
@ServiceActivator(inputChannel="irsControl")
public ExpressionControlBusFactoryBean irsControlBus() {
    return new ExpressionControlBusFactoryBean();
}

@Bean
@InboundChannelAdapter(value = "irsDataPrepJobInputChannel", poller = @Poller(cron="${batch.job.schedule.cron.irsDataPrepJobRunner}"))
public MessageSource<JobLaunchRequest> pollIrsDataPrepJob() {

    return new MessageSource<JobLaunchRequest>() {

        @Override
        public Message<JobLaunchRequest> receive() {
            return new GenericMessage<JobLaunchRequest>(requestIrsDataPrepJob());
        }

    };
}

@Transformer(inputChannel="irsDataPrepJobInputChannel",outputChannel="irsDataPrepJobOutputChannel")
public JobLaunchRequest requestIrsDataPrepJob() {
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addString("subscriberReaderType", "originalSubscriberReaderType").addLong("time",System.currentTimeMillis());
    return new JobLaunchRequest((Job) appContext.getBean("irsDataPrepJob"), jobParametersBuilder.toJobParameters());
}
@配置
@使能集成
@集成组件扫描
公共类IRSjob集成{
@自动连线
私有应用上下文appContext;
@豆子
消息通道控制(){
返回新的DirectChannel();
}
@豆子
@ServiceActivator(inputChannel=“irsControl”)
公共表达式ControlBusFactoryBean irsControlBus(){
返回新的ExpressionControlBusFactoryBean();
}
@豆子
@InboundChannelAdapter(value=“irsDataPrepJobInputChannel”,poller=@poller(cron=“${batch.job.schedule.cron.irsDataPrepJobRunner}”))
public MessageSource pollIrsDataPrepJob(){
返回新的MessageSource(){
@凌驾
公共消息接收(){
返回新的GenericMessage(requestIrsDataPrepJob());
}
};
}
@转换器(inputChannel=“irsDataPrepJobInputChannel”,outputChannel=“irsDataPrepJobOutputChannel”)
public job launchrequest requestIrsDataPrepJob(){
JobParametersBuilder JobParametersBuilder=新的JobParametersBuilder();
jobParametersBuilder.addString(“subscriberReaderType”、“originalSubscriberReaderType”).addLong(“time”,System.currentTimeMillis());
返回新的JobLaunchRequest((Job)appContext.getBean(“irsDataPrepJob”)、jobParametersBuilder.toJobParameters());
}
应用程序上下文的通道定义为:

<int:channel id="irsDataPrepJobInputChannel"/>
<int:channel id="irsDataPrepJobOutputChannel"/>
<int:channel id="irsDataPrepJobJobLaunchReplyChannel"/>
<batch-int:job-launching-gateway request-channel="irsDataPrepJobOutputChannel"
                                 reply-channel="irsDataPrepJobJobLaunchReplyChannel"/>
<int:logging-channel-adapter channel="irsDataPrepJobJobLaunchReplyChannel"/>

轮询器有一个名为maxMessagesPerPoll的元素。当maxMessagesPerPoll=“1”时,它完成了以下操作:

@Bean
@InboundChannelAdapter(value = "irsDataPrepJobInputChannel", poller = @Poller(cron="${batch.job.schedule.cron.irsDataPrepJobRunner}", maxMessagesPerPoll="1"))
public MessageSource<JobLaunchRequest> pollIrsDataPrepJob() {

    return new MessageSource<JobLaunchRequest>() {

        @Override
        public Message<JobLaunchRequest> receive() {
            return new GenericMessage<JobLaunchRequest>(requestIrsDataPrepJob());
        }

    };
}
@Bean
@InboundChannelAdapter(value=“irsDataPrepJobInputChannel”,poller=@poller(cron=“${batch.job.schedule.cron.irsDataPrepJobRunner}”,maxMessagesPerPoll=“1”))
public MessageSource pollIrsDataPrepJob(){
返回新的MessageSource(){
@凌驾
公共消息接收(){
返回新的GenericMessage(requestIrsDataPrepJob());
}
};
}

很高兴看到您自己已经找到了答案!当然,您可以接受自己的答案