Problems using -next option with the CommandLineRunner and JDBC back-end
Trying to get up to speed by running a simple Hello World app with the JDBC back-end. I'm having trouble passing the "-next" parameter into the CommandLineRunner with a JDBC back end.
My initial problem before trying "-next" was this:
Quote:
A job execution for this job is already running: JobInstance: id=2, version=0, JobParameters=[{}], Job=[simpleJo
My job persistence set-up looks like this:
Code:
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.SimpleJobRepository">
<constructor-arg ref="jobInstanceDao"/>
<constructor-arg ref="jobExecutionDao"/>
<constructor-arg ref="stepExecutionDao"/>
<constructor-arg ref="executionContextDao"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="jobIncrementer" class="org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer">
<constructor-arg ref="dataSource"/>
<constructor-arg value="BATCH_JOB_SEQ"/>
<constructor-arg value="id"/>
</bean>
<bean id="jobExecutionIncrementer" class="org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer">
<constructor-arg ref="dataSource"/>
<constructor-arg value="BATCH_JOB_EXECUTION_SEQ"/>
<constructor-arg value="id"/>
</bean>
<bean id="stepExecutionIncrementer" class="org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer">
<constructor-arg ref="dataSource"/>
<constructor-arg value="BATCH_STEP_EXECUTION_SEQ"/>
<constructor-arg value="id"/>
</bean>
<bean id="jobInstanceDao" class="org.springframework.batch.core.repository.dao.JdbcJobInstanceDao"
p:jdbcTemplate-ref="jdbcTemplate"
p:jobIncrementer-ref="jobIncrementer"/>
<bean id="jobExecutionDao" class="org.springframework.batch.core.repository.dao.JdbcJobExecutionDao"
p:jdbcTemplate-ref="jdbcTemplate"
p:jobExecutionIncrementer-ref="jobExecutionIncrementer"/>
<bean id="stepExecutionDao" class="org.springframework.batch.core.repository.dao.JdbcStepExecutionDao"
p:jdbcTemplate-ref="jdbcTemplate"
p:stepExecutionIncrementer-ref="stepExecutionIncrementer"/>
<bean id="executionContextDao" class="org.springframework.batch.core.repository.dao.JdbcExecutionContextDao"
p:jdbcTemplate-ref="jdbcTemplate"/>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
p:taskExecutor-ref="taskExecutor"
p:jobRepository-ref="jobRepository"/>
I added a dynamic job parameters class to supply the parameters:
Code:
public JobParameters getNext(JobParameters parameters) {
date = new Date();
parameters = new JobParametersBuilder()
.addLong("currentTime", System.currentTimeMillis())
.toJobParameters();
return parameters;
}
I inject that into the jobParametersIncrementer property of SimpleJob
Code:
<bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob"
p:name="simpleJob"
p:jobParametersIncrementer-ref="dynamicJobParameters"
p:jobRepository-ref="jobRepository"
>
<property name="steps">
<list>
...
</list>
</property>
</bean>
Everything works fine if I run things manually:
Code:
private void run() {
try {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"classpath:application-test-context.xml"});
SimpleJob job = (SimpleJob)applicationContext.getBean("simpleJob");
SimpleJobLauncher jobLauncher = (SimpleJobLauncher)applicationContext.getBean("jobLauncher");
DynamicJobParameters dynamicJobParameters = (DynamicJobParameters)applicationContext.getBean("dynamicJobParameters");
jobLauncher.run(job, dynamicJobParameters.getNext(null));
} catch (Exception e) {
logger.error(e,e);
}
}
If I use the "-next" parameter on the CommandLineJobRunner that causes this error:
Quote:
Job Terminated in error: A JobExplorer must be provided for a restart or start next operation. Please add one to the configuration.
java.lang.IllegalStateException: A JobExplorer must be provided for a restart or start next operation. Please add one to the configuration.
I set-up the JobExplorer like this:
Code:
<bean id="jobOperator"
class="org.springframework.batch.core.launch.support.SimpleJobOperator"
p:jobLauncher-ref="jobLauncher"
p:jobExplorer-ref="jobExplorer"
p:jobRepository-ref="jobRepository"
p:jobRegistry-ref="jobRegistry"/>
<bean id="jobRegistry"
class="org.springframework.batch.core.configuration.support.MapJobRegistry"/>
<bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor"
p:jobRegistry-ref="jobRegistry"/>
<bean id="jobExplorer"
class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
p:dataSource-ref="dataSource"/>
That, in turn, leads to this error:
Quote:
Job Terminated in error: No job configuration with the name [simpleJob] was registered
org.springframework.batch.core.launch.NoSuchJobExc eption: No job configuration with the name [simpleJob] was registered
at org.springframework.batch.core.configuration.suppo rt.MapJobRegistry.getJob(MapJobRegistry.java:78)
I think that is because "jobRegistry" is using MapJobRegistry. I didn't see an implementation that would allow me to tie the jobOperator back to my SimpleJobRepository.
What is the correct set-up so that I can pass "-next" into the CommandLineRunner using my JDBC-backed SimpleJobRepository?