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);
}
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;
}
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);
}
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();
}
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();
}
Aggregations