user3501569
user3501569

Reputation: 127

Hybris cronjob - start cronjob if not running

I'm working on JMS queues. My requirement is to monitor the queue size and send email once there are no messages in Queue. Cronjob job will monitor the queue and triggered on first message in the queue. Below is the code to trigger the job.

CronJobModel CronJobModel = new CronJobModel();
CronJobModel.setCode("Job1");
CronJobModel = flexibleSearchService
        .getModelByExample(CronJobModel);

modelService.save(CronJobModel);

LOG.debug(" Check and start job");

if (!cronJobService.isRunning(CronJobModel)) {               
    LOG.info("Job initiated on first message in the Queue. ");
    cronJobService.performCronJob(CronJobModel);
    
        LOG.info("Job status::" + cronJobService.isRunning(CronJobModel));
    
}

console output

Job initiated on first message in the Queue.

Job status::false

As a result the job is triggered more than once. Why the cronjob didn't start and change to running state immediately? Is there a better way to identify the job has triggered and ensure not to trigger again?

Upvotes: 0

Views: 1857

Answers (1)

Sebastian
Sebastian

Reputation: 1783

You could try to do a

modelService.refresh(CronJobModel)

so e.g.:

if (!cronJobService.isRunning(CronJobModel)) {               
    LOG.info("Job initiated on first message in the Queue. ");
    cronJobService.performCronJob(CronJobModel);
    modelService.refresh(CronJobModel);
    LOG.info("Job status::" + cronJobService.isRunning(CronJobModel));
}

This will fetch the latest information from the db (in case e.g. another thread made the status change to RUNNING). Not sure if this is a good approach overall though.

Upvotes: 1

Related Questions