Spring Batch_JobParameters

Spring Batch_JobParametersjava


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.)。spring

JobParameter 就支持這四種類型sql

`STRING_VAL` varchar(250) DEFAULT NULL,ui

`DATE_VAL` datetime DEFAULT NULL,this

`LONG_VAL` bigint(20) DEFAULT NULL,spa

`DOUBLE_VAL` double DEFAULT NULL,.net

這些參數都會存儲在batch_job_execution_params表中,看一下該表的表結果:code

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`)xml

JOB_EXECUTION_ID既是主鍵,也是外鍵,關聯於一個job execution,job execution 對 job param 是一對多的關係。blog

以上一個例子爲基礎,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============

相關文章
相關標籤/搜索