Scheduler Job throwing exceptions | Community
Skip to main content
Level 4
December 12, 2023
Solved

Scheduler Job throwing exceptions

  • December 12, 2023
  • 1 reply
  • 700 views

I tried writing a scheduler job using the Job class but somehow it won't execute. It keeps throwing null as an exception. This is what I'm getting in logs :

This is the code that I've written :

package com.mysite.core.schedulers; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import org.apache.sling.commons.scheduler.Job; import org.apache.sling.commons.scheduler.JobContext; import org.apache.sling.commons.scheduler.ScheduleOptions; import org.apache.sling.commons.scheduler.Scheduler; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; import org.osgi.service.metatype.annotations.Designate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Component(immediate = true, service = Job.class) @Designate(ocd = SchedulerConfigThree.class) public class PracticeSchedulerDayThree implements Job { private static final Logger LOG = LoggerFactory.getLogger(PracticeSchedulerDayThree.class); private int schedulerId; @Reference private Scheduler scheduler; @Activate protected void activate(SchedulerConfigThree configThree) { schedulerId = configThree.schedulerDayThreeName().hashCode(); ScheduleOptions in = scheduler.EXPR("0 56 23 1/1 * ? *"); Map<String, Serializable> inMap = new HashMap<>(); inMap.put("country", "India"); inMap.put("URL", "www.india.com"); scheduler.schedule(this, in); ScheduleOptions de = scheduler.EXPR("0 59 23 1/1 * ? *"); Map<String, Serializable> deMap = new HashMap<>(); deMap.put("country", "Denmark"); deMap.put("URL", "www.de.com"); scheduler.schedule(this, de); } @Deactivate protected void deactivate() { scheduler.unschedule(String.valueOf(schedulerId)); } @Override public void execute(JobContext jobContext) { LOG.info("\n ===> COUNTRY : URL {} : {}", jobContext.getConfiguration().get("country"), jobContext.getConfiguration().get("URL")); } }

Can someone please let me know what am I doing wrong over here?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by DPrakashRaj

It seems you forget to put the map config inside scheduleOptions object.

so basically you should be doing this one in.config(inMap) and de.config(deMap) before calling scheduler.schedule() in your class.

1 reply

DPrakashRaj
Community Advisor
DPrakashRajCommunity AdvisorAccepted solution
Community Advisor
December 12, 2023

It seems you forget to put the map config inside scheduleOptions object.

so basically you should be doing this one in.config(inMap) and de.config(deMap) before calling scheduler.schedule() in your class.

Level 4
December 13, 2023

Thank you @dprakashraj . Yes, I missed out that step. Thanks once again 🙂