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 
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()
Make sure your job class in in grails-app/jobs and try removing implements Job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With