论坛首页 入门技术论坛

spring quartz任务调度示例

浏览 4030 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-04-28   最后修改:2008-11-28

package com.xzl.quartz;

import org.apache.log4j.Logger;

public class TJob {
    private Logger logger = Logger.getLogger(this.getClass().getName());
    public void doAuth(){
        logger.info("开始进行任务调度,验证信息:");
    }
}1.创建一个类,类中方法doAuth(),对这个方法进行触发
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!--要调度的对象-->

    <bean id="job" class="com.xzl.quartz.TJob"></bean>
<!-- 定义目标bean和bean中的方法 -->
    <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref local="job"/>
        </property>
        <property name="targetMethod">
            <value>doAuth</value>
        </property>
    </bean>
<!-- 定义触发的时间 -->
    <bean id = "cron" class = "org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="jobtask"/>
        </property>
        <property name="cronExpression">
            <value>* * 18 * * ?</value>
        </property>
    </bean>
<!-- 总管理 -->
    <bean autowire = "no" class = "org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref local ="cron"/>
            </list>
        </property>
    </bean>

</beans>2.对应的xml,名字JobQuartz.xml   [18点的每一分,每一秒都进行doAuth()方法的触发]
package com.xzl.quartz;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {
   
    /** *//**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        System.out.println("测试开始");
        ApplicationContext ctx = new ClassPathXmlApplicationContext("JobQuartz.xml");
        System.out.println("测试结束");
       
    }

}

   发表时间:2008-04-10  
我照着你的例子复制过去,运行的时候有错啊,不知道是什么原因啊,我也是才接触quartz,可能是包不全吧,错误如下:
测试开始
2008-04-10 20:51:55,078  INFO (AbstractApplicationContext.java:290) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab853b: display name [org.springframework.context.support.ClassPathXmlApplicationContext@ab853b]; startup date [Thu Apr 10 20:51:55 CST 2008]; root of context hierarchy
2008-04-10 20:51:55,125  INFO (XmlBeanDefinitionReader.java:293) - Loading XML bean definitions from class path resource [jobQuartz.xml]
2008-04-10 20:51:55,234  INFO (AbstractApplicationContext.java:302) - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@ab853b]: org.springframework.beans.factory.support.DefaultListableBeanFactory@12cc95d
2008-04-10 20:51:55,250  INFO (DefaultListableBeanFactory.java:272) - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12cc95d: defining beans [job,jobtask,cron,org.springframework.scheduling.quartz.SchedulerFactoryBean]; root of factory hierarchy
2008-04-10 20:51:55,312  INFO (DefaultSingletonBeanRegistry.java:274) - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12cc95d: defining beans [job,jobtask,cron,org.springframework.scheduling.quartz.SchedulerFactoryBean]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean' defined in class path resource [jobQuartz.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at org.quartz.impl.StdSchedulerFactory.initialize(StdSchedulerFactory.java:376)
at org.quartz.impl.StdSchedulerFactory.getScheduler(StdSchedulerFactory.java:1340)
at org.springframework.scheduling.quartz.SchedulerFactoryBean.createScheduler(SchedulerFactoryBean.java:674)
at org.springframework.scheduling.quartz.SchedulerFactoryBean.afterPropertiesSet(SchedulerFactoryBean.java:574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1143)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1110)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:431)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:254)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:163)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:281)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
at com.zh.Test.Test.main(Test.java:16)
2 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics