从Quartz作业查询数据库时出现空指针异常

时间:2013-06-03 11:49:54

标签: java hibernate quartz-scheduler

我的调度程序正在触发,但我无法连接到数据库。当我尝试使用测试用例查询数据库时它起作用,所以我尝试使用Quartz实现它,但它给出了NullPointerException

public class JobScheduler extends QuartzJobBean {
    @Autowired
    ISourceService sourcedao;
@Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        Client client = new Client();
        client.setClientKey(300);
        Source sourceobj = sourcedao.getSourceByClient(client);
        String sourcetype = sourceobj.getSourceType();
        System.out.println(sourcetype);
    }
}

我的应用程序context.xml

<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/15 0 * * * ?" />
    </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>

我查了很多例子,但没有得到任何想法。

2 个答案:

答案 0 :(得分:1)

JobScheduler也需要是一个Spring bean。您没有显示如何注释它。我会把它变成一个组件,看看你是否更好。

答案 1 :(得分:0)

在应用程序上下文中我添加了一个地图

<bean id="jobScheduler" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.dca.scheduling.JobScheduler" />
        <property name="jobDataAsMap">
            <map>
                 <entry key ="DATA_MANAGER_MAP_KEY" value-ref="sourceDao"/>**i have added the bean id here**
                <entry key="timeout" value="5" />
            </map>
        </property>
    </bean>

并在 Jobscheduler类

sourceDao= (SourceDaoImpl)jobContext.getJobDetail().getJobDataMap().get("DATA_MANAGER_MAP_KEY");