当cron表达式每2分钟触发一次时,我们希望我们的Spring Batch作业触发一次。但是我们注意到它在触发时会持续运行 - 只要第一个作业完成,就会启动第二个作业,如日志中所示:
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 * * * *
我们的JavaConfig bean看起来像这样:
@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());
}
应用程序上下文的通道定义为:
<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"/>
答案 0 :(得分:1)
@Poller有一个名为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());
}
};
}