Example usage for org.springframework.batch.core JobParametersBuilder JobParametersBuilder

List of usage examples for org.springframework.batch.core JobParametersBuilder JobParametersBuilder

Introduction

In this page you can find the example usage for org.springframework.batch.core JobParametersBuilder JobParametersBuilder.

Prototype

public JobParametersBuilder() 

Source Link

Document

Default constructor.

Usage

From source file:fr.hoteia.qalingo.core.batch.JobLauncherDetails.java

/**
 * Copy parameters that are of the correct type over to
 * {@link JobParameters}, ignoring jobName.
 * //from w  w w.  j  a va  2 s  . com
 * @return a {@link JobParameters} instance
 */
private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {

    JobParametersBuilder builder = new JobParametersBuilder();
    builder.addString("Exec ISO date", isoDateFormat.format(new Date()));

    for (Entry<String, Object> entry : jobDataMap.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (value instanceof String && !key.equals(JOB_NAME)) {
            builder.addString(key, (String) value);

        } else if (value instanceof Float || value instanceof Double) {
            builder.addDouble(key, ((Number) value).doubleValue());

        } else if (value instanceof Integer || value instanceof Long) {
            builder.addLong(key, ((Number) value).longValue());

        } else if (value instanceof Date) {
            builder.addDate(key, (Date) value);

        } else {
            log.debug("JobDataMap contains values which are not job parameters (ignoring).");
        }
    }

    return builder.toJobParameters();
}

From source file:com.my.batch.controller.JobOperationsController.java

@RequestMapping(value = "job/{jobName}", method = RequestMethod.POST)
public JsonResponse launch(@RequestBody LinkedHashMap<String, String> jobParams, @PathVariable String jobName)
        throws NoSuchJobException, JobInstanceAlreadyExistsException, JobParametersInvalidException,
        JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
        JobParametersNotFoundException {
    try {// ww  w  .  j av a2 s. c o  m
        //         LinkedHashMap<String, Object> jobParams = new LinkedHashMap<>();
        Job job = jobRegistry.getJob(jobName);
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        for (Map.Entry<String, String> entry : jobParams.entrySet()) {
            jobParametersBuilder.addString(entry.getKey(), String.valueOf(entry.getValue()));
        }
        jobLauncher.run(job, jobParametersBuilder.toJobParameters());
    } catch (NoSuchJobException e) {
        log.error("job start fail, jobName " + jobName, e);
        JsonResponse.createFailMsg(e.getMessage());
    }
    return JsonResponse.createSuccess();
}

From source file:org.cbio.portal.pipelines.FoundationPipeline.java

private static void launchJob(String[] args, String sourceDirectory, String outputDirectory,
        String cancerStudyId, boolean generateXmlDocument) throws Exception {
    // set up application context and job launcher
    SpringApplication app = new SpringApplication(FoundationPipeline.class);
    ConfigurableApplicationContext ctx = app.run(args);
    JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);

    // get the appropriate job name
    String jobName = generateXmlDocument ? BatchConfiguration.FOUNDATION_XML_DOCUMENT_JOB
            : BatchConfiguration.FOUNDATION_JOB;

    // configure job parameters and launch job
    Job foundationJob = ctx.getBean(jobName, Job.class);
    JobParameters jobParameters = new JobParametersBuilder().addString("sourceDirectory", sourceDirectory)
            .addString("outputDirectory", outputDirectory).addString("cancerStudyId", cancerStudyId)
            .toJobParameters();// w  w w . ja v a 2  s .co  m
    JobExecution jobExecution = jobLauncher.run(foundationJob, jobParameters);

    // close job after completion 
    LOG.info("Closing FoundationPipeline.");
    ctx.close();
}

From source file:org.cloudfoundry.identity.uaa.scim.job.UserSyncJobIntegrationTests.java

@Test
public void testJobRunsWithFilters() throws Exception {
    Date dateInTheFuture = new Date(System.currentTimeMillis() + 10000);
    setUpModifiedUaaData(dateInTheFuture);
    JobExecution execution = jobLauncher.run(job, new JobParametersBuilder()
            .addDate("start.date", new Date(System.currentTimeMillis() - 100000)).toJobParameters());
    assertEquals(BatchStatus.COMPLETED, execution.getStatus());
    StepExecution stepExecution = execution.getStepExecutions().iterator().next();
    assertEquals(3, stepExecution.getReadCount());
    assertEquals(3, stepExecution.getFilterCount());
    assertEquals(0, stepExecution.getWriteCount());
}

From source file:com.greedchina.gcrawler.batch.JobLauncherDetails.java

private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {

    JobParametersBuilder builder = new JobParametersBuilder();

    builder.addLong("time", System.currentTimeMillis());

    for (Entry<String, Object> entry : jobDataMap.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (value instanceof String && !key.equals(JOB_NAME)) {
            builder.addString(key, (String) value);
        } else if (value instanceof Float || value instanceof Double) {
            builder.addDouble(key, ((Number) value).doubleValue());
        } else if (value instanceof Integer || value instanceof Long) {
            builder.addLong(key, ((Number) value).longValue());
        } else if (value instanceof Date) {
            builder.addDate(key, (Date) value);
        } else {//  w  w w . j  a  v a2  s  .  c  o m
            log.debug("JobDataMap contains values which are not job parameters (ignoring).");
        }
    }

    return builder.toJobParameters();

}

From source file:uk.ac.ebi.intact.editor.controller.dbmanager.DbImportController.java

public void launchFileImport(ActionEvent evt) {
    if (this.uploadedFile != null && this.uploadedFile.getFileName() != null) {
        File[] files = saveUploadedFileTemporarily();
        if (files != null) {
            try {
                JobParametersBuilder builder = new JobParametersBuilder();
                this.jobId = "interactionMixImport_" + System.currentTimeMillis();
                String eMail = "intact-dev@ebi.ac.uk";
                User user = userSessionController.getCurrentUser();
                String userLogin = null;
                if (user != null && user.getEmail() != null) {
                    eMail = user.getEmail();
                    userLogin = user.getLogin();
                }//from  w w w .j ava2  s  .  c  o m

                getIntactJobLauncher().run((Job) ApplicationContextProvider.getBean("interactionMixImport"),
                        builder.addString("MIJobId", jobId).addString("input.file", files[0].getAbsolutePath())
                                .addString("error.file", files[1].getAbsolutePath())
                                .addString("email.recipient", eMail).addString("user.login", userLogin)
                                .toJobParameters());

                addInfoMessage("Job started", "Job ID: " + jobId);
            } catch (JobParametersInvalidException e) {
                addErrorMessage("Invalid job parameters", "Job Param: " + "input.file="
                        + files[0].getAbsolutePath() + "error.file" + files[1].getAbsolutePath());
                e.printStackTrace();
                jobId = null;
            } catch (JobExecutionAlreadyRunningException e) {
                addErrorMessage("Job already running", "Job Param: " + "input.file="
                        + files[0].getAbsolutePath() + "error.file" + files[1].getAbsolutePath());
                e.printStackTrace();
                jobId = null;
            } catch (JobRestartException e) {
                addErrorMessage("Job cannot be restarted", "Job Param: " + "input.file="
                        + files[0].getAbsolutePath() + "error.file" + files[1].getAbsolutePath());
                e.printStackTrace();
                jobId = null;
            } catch (JobInstanceAlreadyCompleteException e) {
                addErrorMessage("Job already finished", "Job Param: " + "input.file="
                        + files[0].getAbsolutePath() + "error.file" + files[1].getAbsolutePath());
                e.printStackTrace();
                jobId = null;
            }
        } else {
            addErrorMessage("Could not upload file " + uploadedFile.getFileName(), "Import failed");
            jobId = null;
        }
    } else {
        addErrorMessage("Could not upload file", "Import failed");
        jobId = null;
    }
}

From source file:org.opensourcebank.transaction.processor.OfflineTransactionProcessingGridGainIntegrationTest.java

@Test
public void shouldProcessAllIso8583Transactions() throws Exception {

    assertNotNull(jobLauncher.run(job, new JobParametersBuilder()
            .addString("run.id", "offline-transaction-processing-integration.test").toJobParameters()));

    long repoSize = iso8583TransactionRepository.size();

    // this test will always start with an empty map, hence the first ID is 0
    for (long id = 0; id < repoSize; id++) {
        Iso8583Transaction tx = iso8583TransactionRepository.findById(id);

        assertEquals("transaction was not completed: [" + tx + "]", TransactionStatus.COMPLETED,
                ((AbstractIso8583Transaction) tx).getStatus());
    }/*from   w w w  . j  a v  a  2  s. co m*/

}

From source file:com.enterra.batch.admin.sample.JobExecutionTests.java

@Test
public void testFailedJob() throws Exception {
    int before = SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
    jobParameters = new JobParametersBuilder().addString("fail", "true").toJobParameters();
    JobExecution jobExecution = jobLauncher.run(job1, jobParameters);
    assertNotNull(jobExecution);/*from  w w  w  .j  a v a2s. c  o  m*/
    assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
    int after = SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
    assertEquals(before + 1, after);
}

From source file:com.enterra.batch.admin.sample.BootstrapTests.java

@Test
public void testServletConfiguration() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml" },
            parent);/* ww w.ja va  2s. co  m*/

    assertTrue(context.containsBean("jobRepository"));
    String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(),
            JobController.class);
    assertEquals(1, beanNames.length);

    Job job = context.getBean(JobRegistry.class).getJob("job1");
    final JobExecution jobExecution = parent.getBean(JobLauncher.class).run(job,
            new JobParametersBuilder().addString("fail", "false").toJobParameters());

    new DirectPoller<BatchStatus>(100).poll(new Callable<BatchStatus>() {
        public BatchStatus call() throws Exception {
            BatchStatus status = jobExecution.getStatus();
            if (status.isLessThan(BatchStatus.STOPPED) && status != BatchStatus.COMPLETED) {
                return null;
            }
            return status;
        }
    }).get(2000, TimeUnit.MILLISECONDS);

    context.close();
    parent.close();

    assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

}

From source file:kr.okplace.job.launch.JobLauncherDetails.java

private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {

    JobParametersBuilder builder = new JobParametersBuilder();

    for (Entry<String, Object> entry : jobDataMap.entrySet()) {

        String key = entry.getKey();
        Object value = entry.getValue();

        if (value instanceof String && !key.equals(JOB_NAME)) {
            builder.addString(key, (String) value);
        } else if (value instanceof Float || value instanceof Double) {
            builder.addDouble(key, ((Number) value).doubleValue());
        } else if (value instanceof Integer || value instanceof Long) {
            builder.addLong(key, ((Number) value).longValue());
        } else if (value instanceof Date) {
            builder.addDate(key, (Date) value);
        } else {/*w  ww  .j a  v  a  2 s. co m*/
            log.debug("JobDataMap contains values which are not job parameters (ignoring).");
        }

        builder.addLong("run.id", System.currentTimeMillis() / MIN_INTERVAL);
    }

    return builder.toJobParameters();
}