Search in sources :

Example 1 with JobParametersInvalidException

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);
}
Also used : HashMap(java.util.HashMap) Date(java.util.Date) JobParametersInvalidException(org.springframework.batch.core.JobParametersInvalidException) JobParameters(org.springframework.batch.core.JobParameters) JobParameter(org.springframework.batch.core.JobParameter)

Example 2 with JobParametersInvalidException

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();
    }
}
Also used : JobLauncher(org.springframework.batch.core.launch.JobLauncher) FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DemoProcessor(org.family.processor.DemoProcessor) JobExecutionAlreadyRunningException(org.springframework.batch.core.repository.JobExecutionAlreadyRunningException) FileSystemResource(org.springframework.core.io.FileSystemResource) Step(org.springframework.batch.core.Step) JobRepository(org.springframework.batch.core.repository.JobRepository) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JobParametersInvalidException(org.springframework.batch.core.JobParametersInvalidException) Job(org.springframework.batch.core.Job) DemoLineAggregator(org.family.aggregator.DemoLineAggregator) StepBuilderFactory(org.springframework.batch.core.configuration.annotation.StepBuilderFactory) DeviceCommand(org.family.dto.DeviceCommand) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) JobInstanceAlreadyCompleteException(org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException) JobBuilderFactory(org.springframework.batch.core.configuration.annotation.JobBuilderFactory) DemoLineMapper(org.family.mapper.DemoLineMapper) FlatFileItemWriter(org.springframework.batch.item.file.FlatFileItemWriter) JobParameters(org.springframework.batch.core.JobParameters) JobRestartException(org.springframework.batch.core.repository.JobRestartException)

Aggregations

JobParameters (org.springframework.batch.core.JobParameters)2 JobParametersInvalidException (org.springframework.batch.core.JobParametersInvalidException)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 DemoLineAggregator (org.family.aggregator.DemoLineAggregator)1 DeviceCommand (org.family.dto.DeviceCommand)1 DemoLineMapper (org.family.mapper.DemoLineMapper)1 DemoProcessor (org.family.processor.DemoProcessor)1 Job (org.springframework.batch.core.Job)1 JobParameter (org.springframework.batch.core.JobParameter)1 Step (org.springframework.batch.core.Step)1 JobBuilderFactory (org.springframework.batch.core.configuration.annotation.JobBuilderFactory)1 StepBuilderFactory (org.springframework.batch.core.configuration.annotation.StepBuilderFactory)1 JobLauncher (org.springframework.batch.core.launch.JobLauncher)1 JobExecutionAlreadyRunningException (org.springframework.batch.core.repository.JobExecutionAlreadyRunningException)1 JobInstanceAlreadyCompleteException (org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException)1 JobRepository (org.springframework.batch.core.repository.JobRepository)1 JobRestartException (org.springframework.batch.core.repository.JobRestartException)1 FlatFileItemReader (org.springframework.batch.item.file.FlatFileItemReader)1 FlatFileItemWriter (org.springframework.batch.item.file.FlatFileItemWriter)1