spring定时器

合集下载

Spring配置定时器(注解+xml)方式—整理

Spring配置定时器(注解+xml)方式—整理

Spring配置定时器(注解+xml)⽅式—整理⼀、注解⽅式1. 在Spring的配置⽂件ApplicationContext.xml,⾸先添加命名空间1 xmlns:task="/schema/task"2 /schema/task3 /schema /task/springtask3.1.xsd42. 最后是我们的task任务扫描注解1<task:annotation-driven/>3. spring扫描位置1<context:annotation-config/>2<context:component-scan base-package="com.test"/>4.写⾃⼰的定时任务1 @Component //import ponent;2public class MyTestServiceImpl implements IMyTestService {3 @Scheduled(cron="0/5 * * * * ? ") //每5秒执⾏⼀次4public void myTest(){5 System.out.println("进⼊测试");6 }7 }♦注意:定时器的任务⽅法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定⼀个proxytargetclass的某个值为true)⼆、XML⽅式1.在spring配置⽂件中创建bean,创建schedule1<bean id="schedule"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">3<property name="triggers">4<list>5<ref bean="testTrigger"/>6</list>7</property>8</bean>2. 在spring配置⽂件中创建bean,创建你的triggers1<bean id="testTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">3<property name="jobDetail" ref="testJobDetail"/>4<property name="cronExpression" value="0 1/5 * * * ?"/>5</bean>3. 在spring配置⽂件中创建bean,指定定时器作⽤在那个类那个⽅法上⾯1<bean id="testJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">3<property name="targetObject" ref="targetTestService"/>4<property name="targetMethod" value="timerTest"/>5</bean>♦注明:把定时器作⽤在targetTestService对象中的timerTest⽅法上⾯4. 当然还得把你作⽤的对象交Spring来管理,所以在spring配置⽂件中创建作⽤类的 bean1<bean id="targetTestService" class=".service.TargetTestService" scope="prototype"></bean>♦这是时间的设置规则org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运⾏时间,只需要设置其cronExpression属性。

Spring提供的三种定时任务机制及其比较

Spring提供的三种定时任务机制及其比较

Spring提供的三种定时任务机制及其比较定时任务的需求在众多应用系统中广泛存在,在Spring中,我们可以使用三种不同的定时机制,下面一一描述并加以比较1. 基于Quartz的定时机制下面详细解释这个类图中涉及的关键类及其使用场景1.1. SchedulerFactoryBean这是Spring中基于Quartz的定时机制入口,只要Spring容器装载了这个类,Quartz定时机制就会启动,并加载定义在这个类中的所有triggerSpring配置范例:[xhtml]view plaincopy1.<bean id="sfb"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">2.<!-- 添加触发器 -->3.<property name="triggers">4.<list>5.<ref local="appSubscTrigger"/>6.</list>7.</property>8.9.<!-- 添加listener -->10.<property name="globalTriggerListeners">11.<list>12.<ref local="myTaskTriggerListener"/>13.</list>14.</property>15.</bean>1.2. CronTriggerBean实现了Trigger接口,基于Cron表达式的触发器这种触发器的好处是表达式与linux下的crontab一致,能够满足非常复杂的定时需求,也容易配置Spring配置范例:[xhtml]view plaincopy1.<bean id="notifyTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">2.<property name="jobDetail"ref="notifyJobDetail"/>3.<property name="cronExpression"value="${notify_trigger_cron_expression}"/>4.</bean>1.3. SimpleTriggerBean该类也实现了Trigger接口,基于配置的定时调度这个触发器的优点在于很容易配置一个简单的定时调度策略Spring配置范例:[xhtml]view plaincopy1.<bean id="simpleReportTrigger"class="org.springframework.scheduling.quartz.SimpleTriggerBean">2.<property name="jobDetail">3.<ref bean="reportJob"/>4.</property>5.<property name="startDelay">6.<value>3600000</value>7.</property>8.<property name="repeatInterval">9.<value>86400000</value>10.</property>11.</bean>1.4. JobDetailBeanJobDetail类的简单扩展,能够包装一个继承自QuartzJobBean的普通Bean,使之成为定时运行的Job缺点是包装的Bean必须继承自一个指定的类,通用性不强,对普通Job的侵入性过强,不推荐使用1.5. MethodInvokingJobDetailFactoryBeanSpring提供的一个不错的JobDetail包装工具,能够包装任何bean,并执行类中指定的任何stati或非static的方法,避免强制要求bean去实现某接口或继承某基础类Spring配置范例:[xhtml]view plaincopy1.<bean id="notifyJobDetail"parent="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">2.<property name="targetObject"ref="notifyServerHandler"/>3.<property name="targetMethod"value="execute"/>4.</bean>1.6. 关于TriggerListener和JobListenerQuartz中提供了类似WebWork的拦截器的功能,系统执行任务前或任务执行完毕后,都会检查是否有对应的Listener需要被执行,这种AOP的思想为我们带来了灵活的业务需求实现方式。

spring定时器定时任务到时间未执行问题的解决

spring定时器定时任务到时间未执行问题的解决

spring定时器定时任务到时间未执⾏问题的解决⽬录spring定时器定时任务到时间未执⾏应⽤场景原因分析解决⽅式解决修改系统时间后Spring 定时任务不执⾏问题描述起因错误解决问题spring定时器定时任务到时间未执⾏应⽤场景⼀个定时器类中有n个定时任务,有每30秒执⾏⼀次的还有每1分钟执⾏⼀次的,出现问题的定时任务是0点整时执⾏的定时任务到了0点没有执⾏。

原因分析spring定时器任务scheduled-tasks默认配置是单线程串⾏执⾏的,当某个定时任务出现阻塞,或者执⾏时间过长,则线程就会被占⽤,其他定时任务排队执⾏,导致后⾯的定时任务未能准时执⾏。

解决⽅式开启多线程定时任务执⾏/*** 多线程执⾏定时任务*/@Configurablepublic class ScheduleConfig implements SchedulingConfigurer {private static final int FIVE = 5;@Overridepublic void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(FIVE));}}解决修改系统时间后Spring 定时任务不执⾏问题描述Spring 定时任务不执⾏事情起因是这样的,我们有⼀个spring定时任务,每隔半⼩时要执⾏⼀次。

起因由于种种原因,昨晚上这台服务器被关机了,今早【重启服务器】和【启动定时任务服务】。

机器重启后,发现服务器机器系统时间和实际北京时间不⼀致,相差10个⼩时。

于是乎,我使⽤date -s 10:35:35 设置和北京时间保持⼀致。

错误本以为这样,时间已经⼀致了,定时任务应该能正常执⾏了!等了好⼏个⼩时,定时任务依然没有执⾏。

Springboot定时任务Scheduled重复执行操作

Springboot定时任务Scheduled重复执行操作

Springboot定时任务Scheduled重复执⾏操作今天⽤scheduled写定时任务的时候发现定时任务⼀秒重复执⾏⼀次,⽽我的cron表达式为 * 0/2 * * * * 。

在源码调试的过程中,发现是我的定时任务执⾏过程太短导致的。

于是我另外写了个简单的定时任务@Componentpublic class TestJob {@Scheduled(cron = "* 0/2 * * * *")public void test() {System.out.println("测试开始");System.out.println("测试结束");}}上述任务在启动之后⼀直执⾏。

然后我在任务后⾯加⼊线程睡眠1分钟。

@Componentpublic class TestJob {@Scheduled(cron = "* 0/2 * * * *")public void test() {System.out.println("测试开始");System.out.println("测试结束");try {Thread.sleep(60000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("睡眠结束");}}上述任务执⾏⼀次就没有再执⾏了。

所以我继续深⼊查看源码,发现问题在于CronSequenceGenerator.class的next⽅法。

public Date next(Date date) {Calendar calendar = new GregorianCalendar();calendar.setTimeZone(this.timeZone);calendar.setTime(date);//1.设置下次执⾏时间的毫秒为0,如上次任务执⾏过程不⾜1秒,则calendar的时间会被设置成上次任务的执⾏时间calendar.set(14, 0);long originalTimestamp = calendar.getTimeInMillis();this.doNext(calendar, calendar.get(1));//2.由于有上⾯⼀步,执⾏时间太短,会导致下述条件为trueif(calendar.getTimeInMillis() == originalTimestamp) {//3.calendar在原来的时间上增加1秒calendar.add(13, 1);//CronSequenceGenerator的doNext算法从指定时间开始(包括指定时间)查找符合cron表达式规则下⼀个匹配的时间//注意第⼀个匹配符是*,由于增加了1秒,依然符合cron="* 0/2 * * * *",所以下⼀个执⾏时间就是在原来的基础上增加了⼀秒this.doNext(calendar, calendar.get(1));}return calendar.getTime();}请查看代码中的注释,由于任务执⾏时间太短了,代码会进⼊if语句,并设置执⾏时间在原来的基础上增加⼀秒。

springboot使用定时器@Scheduled不管用的解决

springboot使用定时器@Scheduled不管用的解决

springboot使⽤定时器@Scheduled不管⽤的解决⽬录使⽤定时器@Scheduled不管⽤多个@Scheduled定时器不执⾏解决⽅法使⽤定时器@Scheduled不管⽤如果是⼀开始就不能⽤就是没写@EnableScheduling注解,如果是⽤着⽤着不管⽤了是因为@Scheduled是单线程,有定时器在⼯作或者没有运⾏完毕,所以造成了线程堵塞所以导致下⼀个定时器不能运⾏增加⼀个⽅法类package com.llt;import org.springframework.boot.autoconfigure.batch.BatchProperties;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import ng.reflect.Method;import java.util.concurrent.Executors;@Configurationpublic class ScheduleConfig implements SchedulingConfigurer {@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {Method[] methods = BatchProperties.Job.class.getMethods();int defaultPoolSize = 3;int corePoolSize = 0;if (methods != null && methods.length > 0) {for (Method method : methods) {Scheduled annotation = method.getAnnotation(Scheduled.class);if (annotation != null) {corePoolSize++;}}if (defaultPoolSize > corePoolSize)corePoolSize = defaultPoolSize;}taskRegistrar.setScheduler(Executors.newScheduledThreadPool(corePoolSize));}}就好了!多个@Scheduled定时器不执⾏最近项⽬中经常有⽤到@Scheduled注解,在内测时由于数据量⼩(没有进⾏压⼒测)所以每个线程执⾏都很快,但线上后发现部分功能⽆法使⽤,最后定位是部分的定时器没有执⾏,后查阅资料和Springboot源码后ScheduledTaskRegistrar在启动时,如果没有指定线程池的⼤⼩,默认会创建核⼼线程数为1的默认线程池,故⽽当项⽬中出现多个@Scheduled线程时,只能⼀个个的执⾏,从⽽导致个别线程执⾏时间过长(或长期执⾏)时,其他定时器不能按照指定的规则进⾏执⾏。

java中spring与Quartz 整合定时任务

java中spring与Quartz 整合定时任务

现在想要获得在特定时间或者指定时间执行相应功能有两种做法在applicationContext.xml中写入如下Bean<bean id="repeatingTrigger"class="org.springframework.scheduling.timer.ScheduledTimerTask"> <!--启动一秒后执行 --><property name="delay"><value>1000</value></property><!--每隔一小时执行一次 --><property name="period"><value>3600000</value></property><!--注入要监控的javaBean --><property name="timerTask"><ref bean="task" /></property><!--类型是否为fixedRate型,默认为fixedDelay--><property name="fixedRate"><value>true</value></property></bean><bean id="scheduler"class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"><list><ref bean="repeatingTrigger" /></list></property></bean><bean id="task" class="com.css.wam.portlet.SampleTask"><property name="workService"><ref bean="workService" /></property><property name="work"><ref bean="work" /></property></bean>然后写SampleTask类,如下:package com.css.wam.portlet;import java.util.ArrayList;import java.util.Calendar;import java.util.Iterator;import java.util.List;import java.util.TimerTask;import javax.servlet.ServletContext; 字串2import org.apache.jetspeed.security.SecurityException; 字串9 import com.css.wam.service.WorkService; 字串1@SuppressWarnings("unused")class SampleTask extends TimerTask{ 字串2private static final int C_SCHEDULE_HOUR = 23;//设置指定时间private WorkService workService;private List users;private List teams;private WorkPortlet work;public void setWorkService(WorkService workService) {this.workService = workService;} 字串7public void setWork(WorkPortlet work) {this.work = work;} 字串4public SampleTask(){}@SuppressWarnings("unchecked")public void run() {Calendar cal = Calendar.getInstance();try {users = work.getUsers();teams = new ArrayList();for(Iterator it = users.iterator(); it.hasNext();){String teamname = work.getGroupsByUser((String)it.next()); teams.add(teamname);}//查看当前时间与指定是否一致,一致则执行任务if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY) ) 字串6workService.autoWorkOff(users, teams); 字串8} catch (SecurityException e) {e.printStackTrace();}}}使用Quartz定时<bean id="methodInvokingJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="changeService"/></property><property name="targetMethod"><value>changeAll</value></property></bean><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property><property name="cronExpression"><!--<value>0 0 6,12,20 * * ?</value>--><value>0 0 23 * * ?</value></property></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="cronTrigger"/></list></property></bean>简单定时<bean id="methodInvokingJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="sgService"/></property><property name="targetMethod"><value>updateNowSgList</value></property></bean><bean id="simpleTrigger"class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property><property name="startDelay"><value>10000</value> <!-- 10 s--></property><property name="repeatInterval"><value>1296000000</value> <!-- 1296000000 6 hours--></property></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="simpleTrigger"/></list></property></bean>一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。

Spring中的线程池和定时任务功能

Spring中的线程池和定时任务功能1.功能介绍Spring框架提供了线程池和定时任务执⾏的抽象接⼝:TaskExecutor和TaskScheduler来⽀持异步执⾏任务和定时执⾏任务功能。

同时使⽤框架⾃⼰定义的抽象接⼝来屏蔽掉底层JDK版本间以及Java EE中的线程池和定时任务处理的差异。

另外Spring还⽀持集成JDK内部的定时器Timer和Quartz Scheduler框架。

2.线程池的抽象:TaskExecutorTaskExecutor涉及到的相关类图如下:TaskExecutor接⼝源代码如下所⽰:public interface TaskExecutor extends Executor {/*** Execute the given {@code task}.* <p>The call might return immediately if the implementation uses* an asynchronous execution strategy, or might block in the case* of synchronous execution.* @param task the {@code Runnable} to execute (never {@code null})* @throws TaskRejectedException if the given task was not accepted*/@Overridevoid execute(Runnable task);}此接⼝和Executor⼏乎完全⼀样,只定义了⼀个接收Runnable参数的⽅法,据Spring官⽅介绍此接⼝最初是为了在其他组建中使⽤线程时,将JKD抽离出来⽽设计的。

在Spring的⼀些其他组件中⽐如ApplicationEventMulticaster,Quartz都是使⽤TaskExecutor来作为线程池的抽象的。

spring定时器的说明

<!-- 注入 用户自定义的 任务-->
<property name="jobClass" value="com.zb.job.PlanJob"/>
<!-- 注入参数 -->
<property name="jobDataAsMap"&;entry key="planService" value-ref="planService"></entry>
<entry key="name" value="王二"></entry>
</map>
</property>
</bean>
7.注入触发器
第一种: 间隔时间发生事情
<bean id="mytrig" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- 间隔时间 -->
<property name="repeatInterval" value="3000"></property>
</bean>
第二种 : 每天的某个时间发生
<bean id="mytrig" class="org.springframework.scheduling.quartz.CronTriggerBean">

spring定时器的使用(注解方式)

spring定时器的使⽤(注解⽅式)Spring定时器注解⽅式的实现Spring定时器注解⽅式的实现需要添加⼀下配置1.⾸先要配置我们的spring.xmlXmlns中需要配置:xmlns:task="/schema/task"然后xsi:schemaLocation多加下⾯的内容:2.配置是我们的task任务扫描注解<task:annotation-driven/>3.指定扫描位置<context:annotation-config/><bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/><context:component-scan base-package="com.sxt.mvc.quartz"/>base-package:⾥⾯配置的是你要扫描的位置4.下⾯我们就要编写我们java的⽅法@Scheduled(cron="0/5 * * * * ? ")的意思是:没5秒钟执⾏⼀次这⾥需要注意的是:1、spring的@Scheduled注解需要写在实现上、2、定时器的任务⽅法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定⼀个 proxytargetclass的某个值为true、具体就去百度google吧)3、实现类上要有组件的注解@Component,不然不会执⾏启动容器后⾃动扫描执⾏:关于corn表达式CRON表达式含义"0 0 12 * * ?" 每天中午⼗⼆点触发"0 15 10 ? * *" 每天早上10:15触发"0 15 10 * * ?" 每天早上10:15触发"0 15 10 * * ? *" 每天早上10:15触发"0 15 10 * * ? 2005" 2005年的每天早上10:15触发"0 * 14 * * ?" 每天从下午2点开始到2点59分每分钟⼀次触发"0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟⼀次触发"0 0/5 14,18 * * ?" 每天的下午2点⾄2:55和6点⾄6点55分两个时间段内每5分钟⼀次触发"0 0-5 14 * * ?" 每天14:00⾄14:05每分钟⼀次触发"0 10,44 14 ? 3 WED" 三⽉的每周三的14:10和14:44触发"0 15 10 ? * MON-FRI" 每个周⼀、周⼆、周三、周四、周五的10:15触发。

使用spring的@Scheduled注解执行定时任务,启动项目不输出警告

使⽤spring的@Scheduled注解执⾏定时任务,启动项⽬不输出警告在applicationContext.xml中添加:xmlns:task="/schema/task"xsi:schemaLocation="/schema/task/schema/task/spring-task-4.0.xsd"><task:annotation-driven executor="myExecutor" scheduler="myScheduler"/><task:executor id="myExecutor" pool-size="5"/><task:scheduler id="myScheduler" pool-size="10"/>java代码:@Componentpublic class CleanExpireTokenTask {private Logger logger = LoggerFactory.getLogger(LogTag.BUSINESS);@Scheduled(cron = "0 * * * * ?")public void startUpdateSaleThread(){try{System.out.println("check token expire");}catch(Exception e){logger.error("Make salesReport faild",e);}}}注意:实现类上要加注解@Component定时器的任务⽅法不能有返回值配置及启动报错问题参考⾃2016-11-22⽇安全关闭spring定时任务线程池java代码@Resource(name = "myScheduler")private ThreadPoolTaskScheduler threadPoolTaskScheduler;/*** 等待正在执⾏的定时任务执⾏完毕,不再执⾏新的定时任务,*/public void shutdown(){ threadPoolTaskScheduler.shutdown(); // 等待任务执⾏完毕 while(threadPoolTaskScheduler.getActiveCount() > 0){ try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } }}注:1.根据测试,通过此⽅式创建的定时任务,⽐如每分钟0秒执⾏任务,如果上⼀分钟0秒执⾏的任务还没执⾏完,则这次任务就不会启动。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Spring 中使用 Quartz 的 2种方法(extends QuartzJobBean开发环境:Spring2.0。

如果包类没有 Quartz 包,则需要加入到类路径。

A:exte nds QuartzJobBea n 1 :定义任务(extends QuartzJobBean )p ackage onlyfun.bb.Quartz;import org.quartz.JobExecutionContext;import org.s pringframework.scheduling.quartz.QuartzJobBean;p ublic class QuartzDemo extends QuartzJobBean {private JobData jobData; //JobData 为自定义类p ublic void executelnternalQobExecutionContext context){System.out. pnntln(jobData.getData()+" is executed");p ublic JobData getJobData() {return jobData;p ublic void setJobDataQobData jobData) {this.jobData = jobData;JobData.java: p ackage onlyfun.bb.Quartz;import java.util.Date;p ublic class JobData {p ublic String getData(){return "Data from JobData at "+new Date().toString();2:配置 <!-- use Quartz --> <bean id="someData" class="onlyfun.bb.Quartz.JobData">v/bean> <bean id="jobDetailBean" class="org.s pringframework.scheduling.quartz.JobDetailBean"〉vprop erty name="jobClass" value-'onlyfun.bb.Quart z. QuartzDemo"></prop erty>与使用 Methodi nvok in...方法vprop erty name="jobDataAsMa p"><ma p><entry key="jobData" value-ref="someData">v/entry></ma p>v/prop erty> </bean> vbean id="si mpl eTriggerBean" class="org.s pringframework.scheduling.quartz.Si mpl eTriggerBean"〉vprop erty name="jobDetail" ref="jobDetailBean">v/prop erty>vprop erty name="re peatinterval" value="2000">v/prop erty>vprop erty name="startDelay" value="1000">v/prop erty>v/bean>id="scheduledFactoryBean"class="org.s pringframework.scheduling.quartz.SchedulerFactoryBean">vproperty n ame="triggers">vlist>vref bean="si mpl eTriggerBean"/〉v/list>v/prop erty> v/bean> 的jobClass 属性必须提供Job 的类名称,而不是 Job 类的实例。

Job 所需的SimpleTriggerBean 类来指定,与 TimerTask 指定方式类似。

排定 Job 用 SchedulerFactoryBean 类。

完成设置后,只要在启动 Spring 并读取定义文件后,计划任务就会执行。

3 :测试P ackage onlyfun.bb.Quartz;import java.io.*;import org.quartz.Scheduler;import org.quartz.SchedulerExce ption;import org.s pringframework.context.A pp licationContext;import org.s pringframework.context.s uppo rt.Class PathXmlA pp licationContext; p ublic class TestQuartzDemo {p ublic static void main(Stnng[] args) throws lOExce ption {// TODO Auto-generated method stubApp licationContext context=new Class PathXmlA pp licationContext("a pp licationContext.xml");System.out.printing 启动 Task:");System.out.println("请输入 exit 结束 Task:");数据可以在jobDataAsMap 属性中设置。

vbean注意:在 JobDetailBean在任务的周期指定上,使用//A处BufferedReader reader=new BufferedReader(new Inpu tStreamReader(System.in));while(true){if(reader.readLine().equals("exit")) break; }//此句可以放在A处Scheduler scheduler=(Scheduler)context.getBean("scheduledFactoryBean");try {scheduler.shutdown();} catch (SchedulerExce ption e) {// TODO Auto-generated catch block e.p rintStackTrace();4 :测试分析: 使用SimpleTriggerBean类只能做简单Job与Job之间的执行周期指定,如果需要在指定时间执行,可以使用CronTriggerBean 类。

修改配置文件如下:<!-- use Quartz --> <bean id="someData" class="onlyfun.bb.Quartz.JobData"></bean> <bean id="jobDetailBean" class="org.s pringframework.scheduling.quartz.JobDetailBean"〉vprop erty name="jobClass" value="onlyfun.bb.Quart z. QuartzDemo"></prop erty>vprop erty name="jobDataAsMa p"><ma p><entry key="jobData" value-ref="someData"></entry></ma p>v/prop erty> </bean>vbean id-'cronTriggerBean" class="org.s pringframework.scheduling.quartz.CronTriggerBean"〉vprop erty name="jobDetail" ref="jobDetailBean">v/prop erty>vproperty name="cronEx pression" value="0 37-38 15 * * ?"/> v/bean>vbean id="scheduledFactoryBean" class="org.s pringframework.scheduling.quartz.SchedulerFactoryBean"〉vproperty n ame="triggers">vlist>vref bean="cronTriggerBean"/>v/list>v/prop erty> v/bean>注意:CronTriggerBean 的cronExpression 属性指定格式:至少6个时间元素,上面表示每天的15 : 37-38分执行1次任务。

时间元素(按照顺序)秒(0-59 ): 分(0-59 ): 小时(0-23 ): 每月第几天(1-31 ): 月(1-12 或JAN-DEC ):每星期第几天(1-7或SUN-SAT ): 年(1970-2099): 其中:不用设置的用” ”.(1)每月第几天和每星期第几天是互斥的,两个只能设置1个。

相关文档
最新文档