Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injection in my quartz job

I would like to know how can I use injection in my job using guice. As I can't use @Inject on the default constructor may I use it directly on the attribute as follow (I always got a NullPointerException with PersonDAO) ? I know that a DAO has nothing to do here but it's just for testing.

public class SimpleQuartzJob implements Job {

  @Inject PersonDao Person;

  private static Logger logger = Logger.getLogger(SimpleQuartzJob.class.getName());

  public SimpleQuartzJob() {
  }

  @Override
  public void execute(JobExecutionContext context) throws JobExecutionException {
    if (logger.isDebugEnabled()) logger.debug("In SimpleQuartzJob - executing its JOB at " 
        + new Date() + " by " + context.getTrigger().getName());

        // ... LOGIC ...
  }

} 

In my module I have the following declaration:

bind(PersonDao.class).to(HibernatePersonDaoImpl.class);

Actually I use the PersonDao here 'cause I know it works within another class with injection (but the injection is done on the constructor level there).

Can someone give me an advice?

Here is more info about quartz config:

Web.xml:

  <servlet>  
    <servlet-name>QuartzInitializer</servlet-name>  
    <display-name>Quartz Initializer Servlet</display-name>  
    <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
    <init-param>  
        <param-name>config-file</param-name>  
        <param-value>/quartz.properties</param-value>  
    </init-param>  
    <init-param>  
        <param-name>shutdown-on-unload</param-name>  
        <param-value>true</param-value>  
    </init-param>  
    <init-param>  
        <param-name>start-scheduler-on-load</param-name>  
        <param-value>true</param-value>  
    </init-param>  
</servlet>

quartz.properties:

org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames=quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 5

quartz-config.xml :

<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
  version="1.8">

    <schedule>
        <job>
            <name>mail-job</name>
            <group>MYJOB_GROUP</group>

            <description>The job description</description>
            <job-class>svc.data.server.quartz.SimpleQuartzJob</job-class>
        </job>

        <trigger>
            <cron>
                <name>my-trigger</name>
                <group>MYTRIGGER_GROUP</group>
                <job-name>mail-job</job-name>

                <job-group>MYJOB_GROUP</job-group>
                <cron-expression>10 * * * * ?</cron-expression>

            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>
like image 263
AbstractMan Avatar asked Nov 23 '10 08:11

AbstractMan


People also ask

What is a job in quartz?

A Job is a class that implements the Job interface. It has only one simple method: public class SimpleJob implements Job { public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("This is a quartz job!"

What is SpringBeanJobFactory?

The SpringBeanJobFactory provides support for injecting the scheduler context, job data map, and trigger data entries as properties into the job bean while creating an instance. However, it lacks support for injecting bean references from the application context.


1 Answers

Apparently the injector was not available at this moment. Here is the solution, I tested and it works: http://codesmell.wordpress.com/2009/01/11/quartz-fits/

like image 177
AbstractMan Avatar answered Oct 20 '22 00:10

AbstractMan