Search in sources :

Example 1 with MetaSchedule

use of com.axelor.meta.db.MetaSchedule 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);
    }
}
Also used : JobDetail(org.quartz.JobDetail) MetaSchedule(com.axelor.meta.db.MetaSchedule) MetaScheduleRepository(com.axelor.meta.db.repo.MetaScheduleRepository) Model(com.axelor.db.Model) AbstractBatchService(com.axelor.apps.base.service.administration.AbstractBatchService) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

AbstractBatchService (com.axelor.apps.base.service.administration.AbstractBatchService)1 Model (com.axelor.db.Model)1 MetaSchedule (com.axelor.meta.db.MetaSchedule)1 MetaScheduleRepository (com.axelor.meta.db.repo.MetaScheduleRepository)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 JobDetail (org.quartz.JobDetail)1