use of com.axelor.apps.base.service.administration.AbstractBatchService in project axelor-open-suite by axelor.
the class BatchJob method executeInThread.
@Override
public void executeInThread(JobExecutionContext context) {
JobDetail jobDetail = context.getJobDetail();
MetaSchedule metaSchedule = Beans.get(MetaScheduleRepository.class).findByName(jobDetail.getKey().getName());
String batchServiceClassName = metaSchedule.getBatchServiceSelect();
Class<? extends AbstractBatchService> batchServiceClass;
try {
batchServiceClass = Class.forName(batchServiceClassName).asSubclass(AbstractBatchService.class);
} catch (ClassNotFoundException e) {
throw new UncheckedJobExecutionException(e);
}
AbstractBatchService batchService = Beans.get(batchServiceClass);
String batchCode = metaSchedule.getBatchCode();
Model batchModel = batchService.findModelByCode(batchCode);
if (batchModel == null) {
String msg = String.format("Batch %s not found with service %s", batchCode, batchServiceClassName);
throw new UncheckedJobExecutionException(msg);
}
// Apply job's parameters to the batch.
Map<String, Object> originalProperties = applyBeanPropertiesWithScriptHelper(batchModel, jobDetail.getJobDataMap());
try {
batchService.run(batchModel);
} catch (Exception e) {
throw new UncheckedJobExecutionException(e);
} finally {
if (!JPA.em().contains(batchModel)) {
batchModel = batchService.findModelByCode(batchCode);
}
// Restore original values on the batch.
applyBeanProperties(batchModel, originalProperties);
}
}
Aggregations