要求:需要并行启动Spring批处理作业并安排相同的工作。
Job将使用http请求url启动,启动后将根据cron表达式进行调度。我在joblauncher中添加了ThreadPoolTaskExecutor,但是第二个作业与第一个作业重叠,只有一个作业被调度。总之,我需要异步启动作业,但它同步运行。我有什么遗失的吗?
启动器文件:
@Path("/user-management")
public class UserManagementModule {
String springConfig = "spring/batch/jobs/job-report.xml";
@GET
@Path("/users/{id}")
@Produces("application/xml")
public Response getUserById(@PathParam("id") Integer id)
throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException, IOException {
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
SimpleJobLauncher jobLauncher = (SimpleJobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("reportJob");
JobExecution executionall = jobLauncher.run(
job,
new JobParametersBuilder()
.addLong("id", Long.valueOf(id))
.addDate("date", new Date()).toJobParameters());
return Response.status(200).entity(user).build();
}
Job Xml:
<!-- stored job-meta in memory -->
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor" ref="taskExecutor"/>
</bean>
<batch:job id="reportJob">
<batch:step id="temptask">
<batch:tasklet ref="abc">
<batch:listeners>
<batch:listener ref="customStepListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="5" />
</bean>
<bean id="customStepListener" class="com.howtodoinjava.service.RunScheduler" />
<bean id="abc" class="com.howtodoinjava.service.CustomTask" />
<bean id="runScheduler" class="com.howtodoinjava.service.RunScheduler" />
<task:scheduled-tasks>
<task:scheduled ref="runScheduler" method="run" cron="*/1 * * * * *" />
</task:scheduled-tasks>
运行调度程序:
@Component
public class RunScheduler implements StepExecutionListener {
private static String queryid = null;
public void run() {
System.out.println(queryid);
}
@Override
public ExitStatus afterStep(StepExecution arg0) {
return null;
}
@Override
public void beforeStep(StepExecution stepexecute) {
queryid = stepexecute.getJobExecution().getJobParameters()
.getString("id");
}
}
提前致谢。