Expand my Community achievements bar.

SOLVED

Scheduler Job throwing exceptions

Avatar

Level 5

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 :

arindam6600_0-1702405819134.png

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?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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.

Avatar

Level 5

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