use of org.springframework.batch.core.JobParametersInvalidException in project RecordManager2 by moravianlibrary.
the class JobExecutorImpl method transformJobParameters.
/**
* transform parameters values from String to required type;
* @param inParams input {@link JobParameter}
* @param inValidator input {@link JobParametersValidator}
* @return transformed {@link JobParameter} with defined types
* @throws JobParametersInvalidException when one of parameters can't be converted to required type
*/
private JobParameters transformJobParameters(final String jobName, final JobParameters inParams, final JobParametersValidator inValidator) throws JobParametersInvalidException {
if (!(inValidator instanceof IntrospectiveJobParametersValidator)) {
throw new IllegalArgumentException("Job %s does not support introspection.");
}
IntrospectiveJobParametersValidator insValidator = (IntrospectiveJobParametersValidator) inValidator;
final Map<String, JobParameter> inParamMap = inParams.getParameters();
final Map<String, JobParameter> transformedMap = new HashMap<String, JobParameter>();
Map<String, JobParameterDeclaration> paramDeclMap = new HashMap<String, JobParameterDeclaration>();
for (JobParameterDeclaration declaration : insValidator.getParameters()) {
paramDeclMap.put(declaration.getName(), declaration);
}
// Parameter uuid is used for starting the job with the same parameters again
paramDeclMap.put(Constants.JOB_PARAM_UUID, new JobParameterDeclaration(Constants.JOB_PARAM_UUID, ParameterType.STRING, true));
// No need for declare 'repeat' parameter in job validator
if (paramDeclMap.get(Constants.JOB_PARAM_REPEAT) == null) {
paramDeclMap.put(Constants.JOB_PARAM_REPEAT, new JobParameterDeclaration(Constants.JOB_PARAM_REPEAT, ParameterType.LONG, false));
}
for (String key : inParamMap.keySet()) {
JobParameterDeclaration declaration = paramDeclMap.get(key);
if (declaration == null) {
throw new JobParametersInvalidException("Unknown parameter: " + key);
}
Object originalValue = inParamMap.get(key).getValue();
switch(declaration.getType()) {
case DATE:
if (originalValue instanceof Date) {
transformedMap.put(key, new JobParameter((Date) originalValue));
} else {
Date date = OAIGranularity.stringToDate((String) originalValue);
if (date != null) {
transformedMap.put(key, new JobParameter(date));
} else {
throw new JobParametersInvalidException("Parameter " + key + " isn't in valid format.");
}
}
break;
case DOUBLE:
if (originalValue instanceof Double) {
transformedMap.put(key, new JobParameter((Double) originalValue));
} else {
transformedMap.put(key, new JobParameter(Double.valueOf((String) originalValue)));
}
break;
case LONG:
if (originalValue instanceof Long) {
transformedMap.put(key, new JobParameter((Long) originalValue));
} else {
transformedMap.put(key, new JobParameter(Long.valueOf((String) originalValue)));
}
break;
case STRING:
if (originalValue instanceof String) {
transformedMap.put(key, new JobParameter((String) originalValue));
} else {
transformedMap.put(key, new JobParameter(originalValue.toString()));
}
default:
break;
}
}
// compute incremental 'from'
if (transformedMap.containsKey(Constants.JOB_PARAM_INCREMENTAL)) {
JobParameter param = transformedMap.get(Constants.JOB_PARAM_INCREMENTAL);
if (param.getValue().equals(1L)) {
Long importConfId = null;
if (transformedMap.containsKey(Constants.JOB_PARAM_CONF_ID)) {
JobParameter tempParam = transformedMap.get(Constants.JOB_PARAM_CONF_ID);
if (tempParam.getType().equals(JobParameter.ParameterType.LONG)) {
importConfId = (Long) tempParam.getValue();
}
}
Date incrementalFrom = getIncrementalStartingDate(jobName, importConfId);
if (incrementalFrom != null) {
if (transformedMap.containsKey(Constants.JOB_PARAM_FROM_DATE)) {
logger.warn("Overriding given 'from' parameter.");
}
transformedMap.put(Constants.JOB_PARAM_FROM_DATE, new JobParameter(incrementalFrom));
logger.info("Detected incremental 'from' parameter: " + incrementalFrom);
}
}
}
JobParameter repeatParam = transformedMap.get(Constants.JOB_PARAM_REPEAT);
if (repeatParam != null && (new Long(1)).equals(repeatParam.getValue())) {
transformedMap.put(Constants.JOB_PARAM_TIMESTAMP, new JobParameter(new Date()));
}
transformedMap.remove(Constants.JOB_PARAM_REPEAT);
return new JobParameters(transformedMap);
}
use of org.springframework.batch.core.JobParametersInvalidException in project Spring-Family by Sierou-Java.
the class Main method main.
public static void main(String[] args) {
try {
String[] configLocations = { "classpath*:applicationContext.xml" };
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocations);
JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
JobRepository jobRepository = applicationContext.getBean(JobRepository.class);
PlatformTransactionManager transactionManager = applicationContext.getBean(PlatformTransactionManager.class);
// 创建reader
FlatFileItemReader<DeviceCommand> flatFileItemReader = new FlatFileItemReader<>();
flatFileItemReader.setResource(new FileSystemResource("/project/Spring-Family-Parent/Spring-Family-Batch/src/main/resources/batch-data.csv"));
// // 转换数据
flatFileItemReader.setLineMapper(new DemoLineMapper());
// 创建processor 对数据进行操作
DemoProcessor helloItemProcessor = new DemoProcessor();
// 创建writer
FlatFileItemWriter<DeviceCommand> flatFileItemWriter = new FlatFileItemWriter<>();
flatFileItemWriter.setResource(new FileSystemResource("/project/Spring-Family-Parent/Spring-Family-Batch/src/main/resources/batch-data.csv"));
flatFileItemWriter.setLineAggregator(new DemoLineAggregator());
// 创建Step
StepBuilderFactory stepBuilderFactory = new StepBuilderFactory(jobRepository, transactionManager);
Step step = stepBuilderFactory.get("step").<DeviceCommand, DeviceCommand>chunk(1).reader(// 读操作
flatFileItemReader).processor(// 处理操作
helloItemProcessor).writer(// 写操作
flatFileItemWriter).build();
// 创建Job
JobBuilderFactory jobBuilderFactory = new JobBuilderFactory(jobRepository);
Job job = jobBuilderFactory.get("job").start(step).build();
// 启动任务
jobLauncher.run(job, new JobParameters());
} catch (JobExecutionAlreadyRunningException e) {
e.printStackTrace();
} catch (JobRestartException e) {
e.printStackTrace();
} catch (JobInstanceAlreadyCompleteException e) {
e.printStackTrace();
} catch (JobParametersInvalidException e) {
e.printStackTrace();
}
}
Aggregations