Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service not injecting properly into quartz job class With quartz scheduler plugin (v1.0.1) for grails 2.3.6

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

Update 1 : Added the job code and log messages

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 
like image 924
rjvargh Avatar asked Oct 12 '25 11:10

rjvargh


2 Answers

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()

like image 142
greenkode Avatar answered Oct 16 '25 07:10

greenkode


Make sure your job class in in grails-app/jobs and try removing implements Job.

like image 26
Dónal Avatar answered Oct 16 '25 07:10

Dónal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!