Reputation: 51
I've implemented my own quartz job classes that take in a service method . However, the service doesn't seem to be injecting properly into my job class as i always get a nullpointer exception when accessing a service.
How do i get the services to be injected during the trigger of the job .
Any suggestion on how to invoke the method in the service from the Job class
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobDataMap;
import com.unitrac.app.reportcentre.report.ReportService;
// Logging
import org.apache.commons.logging.LogFactory;
class ReportJob implements Job {
def reportService;
def grailsApplication;
// Logging
private static final log = LogFactory.getLog(this);
void execute(JobExecutionContext context) throws JobExecutionException {
// execute job
JobDataMap jobMapData = context.getMergedJobDataMap();
try
{
log.debug("In Report Job - executing its JOB at "
+ new Date() + " by " + context.getTrigger().getName());
String groupName = context.getTrigger().getJobKey().getName();
log.debug("group Name : " + groupName);
reportService.invokeMethod();
}
catch( Exception e )
{
log.error("Exception: $e");
}
}
}
| |
Server running. Browse to http://localhost:8080/ReportCentre
2014-04-22 14:50:00,087 [UnitracJobScheduler_Worker-1] DEBUG reportcentre.ReportJob - In Report Job - executing its JOB at Tue Apr 22 14:50:00 CAT 2014 by CCtrigger
2014-04-22 14:50:00,104 [UnitracJobScheduler_Worker-1] DEBUG reportcentre.ReportJob - group Name : DevGroup
2014-04-22 14:50:00,106 [UnitracJobScheduler_Worker-1] ERROR reportcentre.ReportJob - Exception: java.lang.NullPointerException: Cannot invoke method invokeMethod() on null object
Upvotes: 4
Views: 2101
Reputation: 3996
For what it's worth. I tried all the suggestions above, and it' didn't work for me. I eventually settled for Injecting the service manually from the Grails ApplicationContext.
def ss = Holders.grailsApplication.mainContext.getBean(SampleService.class)
This is better than creating a service using new()
Upvotes: 3
Reputation: 198
You actually don't need to remove "implements Job". Ran into the same sort of problem...
I am guessing that you are creating the job with JobBuilder.newJob(...)
Using newJob breaks the injection (it is bypassing standard object creation patterns). You can, usually, just put the things that you were loading into JobDetail into the trigger.
Then use: ReportJob.schedule(trigger)
Doesn't even need an instance to work. All injection is happy. Yay, Reports!
Upvotes: 1
Reputation: 21
I added @Autowired()
annotation to the fields I need wired in and this worked for me.
Upvotes: 0
Reputation: 187529
Make sure your job class in in grails-app/jobs
and try removing implements Job
.
Upvotes: 1