0

我的调度程序正在触发,但我无法连接到数据库。当我尝试使用测试用例查询数据库时,它起作用了,所以我尝试使用 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>

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

4

2 回答 2

1

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

于 2013-06-03T11:58:21.633 回答
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"); 
于 2013-06-05T06:05:21.580 回答