spring3不支持quartz2
1.业务调度的类(就是你想要做的事情的逻辑部门) public class QuartzTest { public void quartzTest(){ System.out.println("I Love You, zhuzhu"); } } 2.写一个job public class RunJob extends QuartzJobBean{ private QuartzTest quartztest; public QuartzTest getQuartztest() { return quartztest; } public void setQuartztest(QuartzTest quartztest) { this.quartztest = quartztest; } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { quartztest.quartzTest(); } } 3.在xml中配置 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> //注册一个调度 <bean id="quartztest" class="quartz.test.manager.QuartzTest" /> <!-- Spring Quartz --> //配置一个job,注意class中的类 <bean name="quartzJob" class="org.springframework.scheduling.quartz.JobDetailBean"> //name="jobClass"这个名字不能随便更改, value值就是写的job <property name="jobClass" value="quartz.test.job.RunJob" /> <property name="jobDataAsMap"> <map>//key的值要与job的字段名称对应, value-ref是你注册的调度的id名称 <entry key="quartztest" value-ref="quartztest" /> </map> </property> </bean> <!-- Cron Trigger, run every 5 seconds --> //配置触发器,有两种方式,这是我喜欢的一种方式 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> //name="jobDetail"一样的name的值不可以随意改变, ref的值是注册的job的名字 <property name="jobDetail" ref="quartzJob" /> //配置的时间规则,每五秒钟跑一次任务 <property name="cronExpression" value="0/5 * * * * ?" /> </bean> //配置调度工厂 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- <property name="jobDetails"> <list> <ref bean="runMeJob" /> </list> </property> --> //name的值同样不要更改,把触发器的名字配置上 <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> </beans> 4.测试类 public class QuartzTestRun { public static void main(String [] args){ new ClassPathXmlApplicationContext("Spring-Quartz.xml"); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10106.html