Spring Batch_JobParameters
spring batch的JobParameters是设置job运行的参数,同时也具有标志job的作用,就是判断两个job是不是同一个job( "how is one JobInstance distinguished from another?" The answer is: JobParameters. JobParameters is a set of parameters used to start a batch job.)。
JobParameter 就支持这四种类型
`STRING_VAL` varchar(250) DEFAULT NULL,
`DATE_VAL` datetime DEFAULT NULL,
`LONG_VAL` bigint(20) DEFAULT NULL,
`DOUBLE_VAL` double DEFAULT NULL,
这些参数都会存储在batch_job_execution_params表中,看一下该表的表结果:
CREATE TABLE `batch_job_execution_params` ( `JOB_EXECUTION_ID` bigint(20) NOT NULL, `TYPE_CD` varchar(6) NOT NULL, `KEY_NAME` varchar(100) NOT NULL, `STRING_VAL` varchar(250) DEFAULT NULL, `DATE_VAL` datetime DEFAULT NULL, `LONG_VAL` bigint(20) DEFAULT NULL, `DOUBLE_VAL` double DEFAULT NULL, `IDENTIFYING` char(1) NOT NULL, KEY `JOB_EXEC_PARAMS_FK` (`JOB_EXECUTION_ID`), CONSTRAINT `JOB_EXEC_PARAMS_FK` FOREIGN KEY (`JOB_EXECUTION_ID`) REFERENCES `batch_job_execution` (`JOB_EXECUTION_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
CONSTRAINT `JOB_EXEC_PARAMS_FK` FOREIGN KEY (`JOB_EXECUTION_ID`) REFERENCES `batch_job_execution` (`JOB_EXECUTION_ID`)
JOB_EXECUTION_ID既是主键,也是外键,关联于一个job execution,job execution 对 job param 是一对多的关系。
以上一个例子为基础,http://my.oschina.net/xinxingegeya/blog/343190
看一下job param的用法和用处
AppMain2.java
package com.lyx.batch; import java.util.Date; import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.JobParametersInvalidException; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; import org.springframework.batch.core.repository.JobRestartException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppMain2 { public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { // TODO Auto-generated method stub @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath:spring-batch3.xml" }); JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); // 设置JobParameter jobParametersBuilder.addDate("date", new Date()) .addString("hello", "world").addDouble("cost", 12.12); Job job = (Job) context.getBean("addPeopleDescJob"); JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher"); JobExecution result = launcher.run(job, jobParametersBuilder.toJobParameters()); ExitStatus es = result.getExitStatus(); if (es.getExitCode().equals(ExitStatus.COMPLETED.getExitCode())) { System.out.println("任务正常完成"); } else { System.out.println("任务失败,exitCode=" + es.getExitCode()); } } }
以下代码是设置job param的,可以看到我设置了三个参数:
// 设置JobParameter jobParametersBuilder.addDate("date", new Date()) .addString("hello", "world").addDouble("cost", 12.12);
我先把spring batch已有的表数据清空,如下:
-- init spring batch database SET FOREIGN_KEY_CHECKS=0; truncate batch_job_execution; truncate batch_job_execution_context; truncate batch_job_execution_params; truncate batch_job_execution_seq; truncate batch_job_instance; truncate batch_job_seq; truncate batch_step_execution; truncate batch_step_execution_context; truncate batch_step_execution_seq;
在以上参数的基础上,运行一下job,最后查询一下batch_job_execution_params表,可以看到如下:
JOB_EXECUTION_ID TYPE_CD KEY_NAME STRING_VAL DATE_VAL LONG_VAL DOUBLE_VAL IDENTIFYING ---------------- ------- -------- ---------- ------------------- -------- ---------- ----------- 0 DATE date 2014-11-12 11:11:53 0 0.0 Y 0 STRING hello world 1970-01-01 08:00:00 0 0.0 Y 0 DOUBLE cost 1970-01-01 08:00:00 0 12.12 Y
可以看到date,hello,cost参数都持久化到了该表中,那么如何使用JobParameters
请看下一篇文章:http://my.oschina.net/xinxingegeya/blog/343509
这样使用:
#{jobParameters['input.file.name']}
Often in a batch setting it is preferable to parameterize the file name in the JobParameters of the job, instead of through system properties, and access them that way. To accomplish this, Spring Batch allows for the late binding of various Job and Step attributes:
<bean id="flatFileItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource" value="#{jobParameters['input.file.name']}" /> </bean>
Both the JobExecution and StepExecution level ExecutionContext can be accessed in the same way:
<bean id="flatFileItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource" value="#{jobExecutionContext['input.file.name']}" /> </bean>
<bean id="flatFileItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource" value="#{stepExecutionContext['input.file.name']}" /> </bean>
============END============