Search code examples
javamysqlspringhibernatequartz-scheduler

Nullpointer exception quartz integration with spring


am working on a spring hibernate project Am am trying to connect to the database and get the values from the quartzjob. but i am getting a null pointer exception. Whent i tried to use a ApplicationContext object and get the bean it is connecting to the database.why am not able to do the other way

public class JobScheduler extends QuartzJobBean {
    @Autowired
    private SourceDaoImpl sourceDao;
    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        Client client = new Client();
        client.setClientKey(300);

        sourceDao.getSourceByClient(client); **//error**
    }
    public SourceDaoImpl getSourceDao() {
        return sourceDao;
    }
    public void setSourceDao(SourceDaoImpl sourceDao) {
        this.sourceDao = sourceDao;
    }
}

this is my applicationcontext.xml

<!-- scheduler -->
<bean id="jobScheduler" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="com.dca.scheduling.JobScheduler" />
    <property name="jobDataAsMap">
        <map>
            <entry key="timeout" value="5" />
        </map>
    </property>
</bean>
<bean id="cronTriggerjobScheduler" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="jobScheduler" />
    <property name="cronExpression" value="0/10 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="jobScheduler" />
        </list>
    </property>
    <property name="triggers">
        <list>
            <ref bean="cronTriggerjobScheduler" />
        </list>
    </property>
</bean>

<bean id="jobClass"
    class="com.dca.scheduling.JobScheduler">

</bean>

this is the exception

ERROR 04-06 07:22:55,009 - Job DEFAULT.jobScheduler threw an unhandled Exception: 
java.lang.NullPointerException
    at com.dca.scheduling.JobScheduler.executeInternal(JobScheduler.java:21)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
ERROR 04-06 07:22:55,010 - Job (DEFAULT.jobScheduler threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:227)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: java.lang.NullPointerException
    at com.dca.scheduling.JobScheduler.executeInternal(JobScheduler.java:21)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
    ... 1 more

hibernate.xml

<bean id="sourceInstanceDao" class="com.dca.dao.impl.SourceInstanceDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

Solution

  • Quartz runs in its own context, even if started by the Spring convenience methods, so you actually don't end up getting your whole Spring application context, unless you explicitly at a JobDataMap to pass in beans

    <bean id="jobScheduler" class="org.springframework.scheduling.quartz.JobDetailBean">
            <property name="jobClass" value="com.vxl.appanalytix.dca.scheduling.JobScheduler" />
            <property name="jobDataAsMap">
                <map>
                  <entry key ="sourceDao" value-ref="sourceDao"/>
                  <entry key="timeout" value="5" />
                </map>
            </property>
        </bean>
    

    my job scheduler class

    protected void executeInternal(JobExecutionContext jobContext)
                throws JobExecutionException {
            Client client = new Client();
            client.setClientKey(300);
            sourceDao= (SourceDaoImpl) jobContext.getJobDetail()
                    .getJobDataMap().get("sourceDao");
    }