spring2.0 job Quartz 和Time一样的定时任务
package com.tht.common.job.spring;import java.util.Date;/** * Created by IntelliJ IDEA. * User: liuwen * Date: 2010-11-6 * Time: 20:30:42 * To change this template use File | Settings | File Templates. */public class JobData { public String getDate(){ return "Data from "+new Date().toString(); }}?
package com.tht.common.job.spring;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;/** * Created by IntelliJ IDEA. * User: liuwen * Date: 2010-11-6 * Time: 20:35:02 * To change this template use File | Settings | File Templates. */public class DemoJob extends QuartzJobBean{ private JobData jobData; @Override protected void executeInternal(org.quartz.JobExecutionContext jobExecutionContext) throws JobExecutionException { //To change body of implemented methods use File | Settings | File Templates. System.out.println(jobData.getDate()+"执行了"); } public JobData getJobData() { return jobData; } public void setJobData(JobData jobData) { this.jobData = jobData; }}?
beans-config.xml
?
<?xml version="1.0" encoding="UTF-8"?> <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-2.0.xsd"> <bean id="someData" value="com.tht.common.job.spring.DemoJob"></property> <property name="jobDataAsMap"> <map> <entry key="jobData" value-ref="someData"></entry> </map> </property> </bean> <bean id="simpleTriggerBean" ref="jobDetailBean"/> <property name="repeatInterval" value="5000"/><!--5000毫秒 即每隔5秒执行定时任务 --> <property name="startDelay" value="1000"/><!--1000毫秒 即1秒后 执行定时任务 --> </bean> <bean id="schedulerFactoryBean" name="code">package com.tht.common.job.spring;import org.apache.log4j.Logger;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Timer;/** * Created by IntelliJ IDEA. * User: liuwen * Date: 2010-11-6 * Time: 19:43:29 * To change this template use File | Settings | File Templates. * 启动类,并控制何时关闭时钟任务 */public class TimerTaskDemo { static Logger log=Logger.getLogger(TimerTaskDemo.class); public static void main(String[] args){ ApplicationContext context=new ClassPathXmlApplicationContext("beans-config.xml"); log.info("启动任务。。。。。。"); log.info("请输入exit,关闭任务"); BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); while(true){ try { if(reader!=null && "exit".equals(reader.readLine())){ break; } } catch (IOException e) { log.error(e.getMessage(), e.fillInStackTrace()); } } Scheduler scheduler =(Scheduler)context.getBean("schedulerFactoryBean"); try { scheduler.shutdown(); } catch (SchedulerException e) { log.error(e.getMessage(), e.fillInStackTrace()); } }}?