JMIX 1.2 - Quartz AddOn - Logging

Hi,

will there be any logging like in the Cuba scheduler?
I cannot find any logs in the quartz tables.

Or do we have to do it on our own?
If yes, is there a JMIX howto for that?

Regards
Roland

Hi,

thank you for good question! You are right, there is no OOB loging and storing execution history in new Quartz add-on, because it intentionally was made so lighweight. If you noticed, it doesn’t bring any additional DB tables except QUARTZ tables.

So, if you have a requirement to track somehow execution of your jobs it is needed to use standard Quartz possibilities, please refere to documentation - Quartz :: Jmix Documentation

Regards,
Artem

Just an addition to documentation, if you plan to store execution history in data store in order to show it somehow in UI you can introduce an entity for that, like

@JmixEntity
@Table(name = "SCHEDULED_TASK_EXECUTION")
@Entity
public class ScheduledTaskExecution {

    @JmixGeneratedValue
    @Column(name = "ID", nullable = false)
    @Id
    private UUID id;

    @Column(name = "JOB_KEY", nullable = false)
    private String jobName;

    @Column(name = "JOB_GROUP")
    private String jobGroup;

    @Column(name = "START_DATE")
    @Temporal(TemporalType.DATE)
    private Date startDate;

    @Column(name = "END_DATE")
    @Temporal(TemporalType.DATE)
    private Date endDate;

    @Column(name = "STATUS_")
    private String status;

    @Column(name = "RESULT_")
    private String result;
    
    //getter & setters

and introduce your project JobListenerSupport, like

@Component("ScheduledTaskExecutionListener")
public class ScheduledTaskExecutionListener extends JobListenerSupport {

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private UnconstrainedDataManager dataManager;

    @PostConstruct
    public void registerMe() {
        try {
            scheduler.getListenerManager().addJobListener(this);
        } catch (SchedulerException e) {
            //do nothing
        }
    }

    @Override
    public String getName() {
        return "ScheduledTaskExecutionListener";
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext context) {
        ScheduledTaskExecution scheduledTaskExecution = dataManager.create(ScheduledTaskExecution.class);
        scheduledTaskExecution.setJobName(context.getJobDetail().getKey().getName());
        scheduledTaskExecution.setJobGroup(context.getJobDetail().getKey().getGroup());
        scheduledTaskExecution.setStartDate(context.getScheduledFireTime());
        scheduledTaskExecution.setStatus("EXECUTING");
        dataManager.save(scheduledTaskExecution);
    }

    @Override
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        ScheduledTaskExecution scheduledTaskExecution = dataManager.load(ScheduledTaskExecution.class)
                .condition(LogicalCondition.and(
                                PropertyCondition.equal("jobName", context.getJobDetail().getKey().getName()),
                                PropertyCondition.equal("jobGroup", context.getJobDetail().getKey().getGroup()),
                                PropertyCondition.equal("status", "EXECUTING")
                        )
                )
                .one();
        scheduledTaskExecution.setEndDate(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));

        if (jobException != null) {
            scheduledTaskExecution.setResult(jobException.getMessage());
            scheduledTaskExecution.setStatus("FAILED");
        } else {
            scheduledTaskExecution.setResult(context.getResult() != null ? context.getResult().toString() : "");
            scheduledTaskExecution.setStatus("FINISHED");
        }

        dataManager.save(scheduledTaskExecution);
    }
}

Hope, it was helpful for you.

Regards,
Artem

1 Like