2

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>
4

2 回答 2

2

Quartz 在它自己的上下文中运行,即使是由 Spring 便利方法启动的,所以实际上你最终不会得到整个 Spring 应用程序上下文,除非你明确地在 JobDataMap 中传递 bean

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

我的作业调度程序类

protected void executeInternal(JobExecutionContext jobContext)
            throws JobExecutionException {
        Client client = new Client();
        client.setClientKey(300);
        sourceDao= (SourceDaoImpl) jobContext.getJobDetail()
                .getJobDataMap().get("sourceDao");
}
于 2013-06-05T09:43:24.250 回答
1

不幸的是,答案很简单:sourceDao为空。

Quartz 自己实例化作业类。将参数传递给该类的唯一方法是使用JobExecutionContextin executeInternal()

首先,当您scheduleJob()必须设置JobDetails.

JobDetail thisJobDetail = new SimpleJobDetail<T>(this.getClass().getName(), (Class<? extends AbstractQuartzJob<T>>) this.getClass());
thisJobDetail.getJobDataMap().put(DATA_MANAGER_MAP_KEY,
                dataManager);

sched.scheduleJob(thisJobDetail ,aTrigger);

然后,当您执行作业时,您可以访问如下详细信息:

this.dataManager = (DataManager) jobContext.getJobDetail()
                                 .getJobDataMap().get(DATA_MANAGER_MAP_KEY);

在您的情况下sourceDao用作dataManager此处。

于 2013-06-04T11:52:24.160 回答