Search in sources :

Example 11 with JobParameters

use of org.springframework.batch.core.JobParameters in project camel by apache.

the class SpringBatchProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    JobParameters jobParameters = prepareJobParameters(exchange.getIn().getHeaders());
    String messageJobName = jobParameters.getString(SpringBatchConstants.JOB_NAME);
    Job job2run = this.job;
    if (messageJobName != null) {
        if (jobRegistry != null) {
            job2run = jobRegistry.getJob(messageJobName);
        } else {
            job2run = CamelContextHelper.mandatoryLookup(getEndpoint().getCamelContext(), messageJobName, Job.class);
        }
    }
    if (job2run == null) {
        exchange.setException(new CamelExchangeException("jobName was not specified in the endpoint construction " + " and header " + SpringBatchConstants.JOB_NAME + " could not be found", exchange));
        return;
    }
    JobExecution jobExecution = jobLauncher.run(job2run, jobParameters);
    exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
    exchange.getOut().setBody(jobExecution);
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) CamelExchangeException(org.apache.camel.CamelExchangeException) JobParameters(org.springframework.batch.core.JobParameters) Job(org.springframework.batch.core.Job)

Example 12 with JobParameters

use of org.springframework.batch.core.JobParameters in project camel by apache.

the class SpringBatchProducer method prepareJobParameters.

/**
     * Helper method converting the Camel message headers into the Spring Batch parameters map. Date, Long and Double
     * header values are converted to the appropriate types. All the other header values are converted to string
     * representation.
     *
     * @param headers Camel message header to be converted
     * @return Camel message headers converted into the Spring Batch parameters map
     */
protected JobParameters prepareJobParameters(Map<String, Object> headers) {
    JobParametersBuilder parametersBuilder = new JobParametersBuilder();
    for (Map.Entry<String, Object> headerEntry : headers.entrySet()) {
        String headerKey = headerEntry.getKey();
        Object headerValue = headerEntry.getValue();
        if (headerValue instanceof Date) {
            parametersBuilder.addDate(headerKey, (Date) headerValue);
        } else if (headerValue instanceof Long) {
            parametersBuilder.addLong(headerKey, (Long) headerValue);
        } else if (headerValue instanceof Double) {
            parametersBuilder.addDouble(headerKey, (Double) headerValue);
        } else if (headerValue != null) {
            parametersBuilder.addString(headerKey, headerValue.toString());
        } else {
            // if the value is null we just put String with null value here to avoid the NPE
            parametersBuilder.addString(headerKey, null);
        }
    }
    JobParameters jobParameters = parametersBuilder.toJobParameters();
    log.debug("Prepared parameters for Spring Batch job: {}", jobParameters);
    return jobParameters;
}
Also used : JobParametersBuilder(org.springframework.batch.core.JobParametersBuilder) JobParameters(org.springframework.batch.core.JobParameters) Map(java.util.Map) Date(java.util.Date)

Example 13 with JobParameters

use of org.springframework.batch.core.JobParameters in project pinpoint by naver.

the class AlarmJobTest method getParameters.

private static JobParameters getParameters() {
    Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
    parameters.put("schedule.scheduledFireTime", new JobParameter(new Date()));
    return new JobParameters(parameters);
}
Also used : HashMap(java.util.HashMap) JobParameters(org.springframework.batch.core.JobParameters) JobParameter(org.springframework.batch.core.JobParameter) Date(java.util.Date)

Example 14 with JobParameters

use of org.springframework.batch.core.JobParameters in project spring-boot by spring-projects.

the class BatchAutoConfigurationTests method testDefinesAndLaunchesLocalJob.

@Test
public void testDefinesAndLaunchesLocalJob() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.names:discreteLocalJob");
    this.context.register(NamedJobConfigurationWithLocalJob.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
    this.context.getBean(JobLauncherCommandLineRunner.class).run();
    assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("discreteLocalJob", new JobParameters())).isNotNull();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobParameters(org.springframework.batch.core.JobParameters) JobRepository(org.springframework.batch.core.repository.JobRepository) Test(org.junit.Test)

Example 15 with JobParameters

use of org.springframework.batch.core.JobParameters in project spring-boot by spring-projects.

the class BatchAutoConfigurationTests method testUsingJpa.

@Test
public void testUsingJpa() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    // The order is very important here: DataSource -> Hibernate -> Batch
    this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    PlatformTransactionManager transactionManager = this.context.getBean(PlatformTransactionManager.class);
    // It's a lazy proxy, but it does render its target if you ask for toString():
    assertThat(transactionManager.toString().contains("JpaTransactionManager")).isTrue();
    assertThat(this.context.getBean(EntityManagerFactory.class)).isNotNull();
    // Ensure the JobRepository can be used (no problem with isolation level)
    assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("job", new JobParameters())).isNull();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobParameters(org.springframework.batch.core.JobParameters) JobRepository(org.springframework.batch.core.repository.JobRepository) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Aggregations

JobParameters (org.springframework.batch.core.JobParameters)20 Test (org.junit.Test)12 Date (java.util.Date)5 JobRepository (org.springframework.batch.core.repository.JobRepository)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 RunIdIncrementer (org.springframework.batch.core.launch.support.RunIdIncrementer)4 Job (org.springframework.batch.core.Job)3 JobParameter (org.springframework.batch.core.JobParameter)3 JobParametersBuilder (org.springframework.batch.core.JobParametersBuilder)3 StepContribution (org.springframework.batch.core.StepContribution)3 ChunkContext (org.springframework.batch.core.scope.context.ChunkContext)3 Tasklet (org.springframework.batch.core.step.tasklet.Tasklet)3 HashMap (java.util.HashMap)2 JobExecution (org.springframework.batch.core.JobExecution)2 JobInstance (org.springframework.batch.core.JobInstance)2 Map (java.util.Map)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 DateTimeService (org.mifos.framework.util.DateTimeService)1 CronTrigger (org.quartz.CronTrigger)1 SimpleTrigger (org.quartz.SimpleTrigger)1