怎样在Spring中配置定时任务.docx
文本预览下载声明
如何在Spring中配置定时任务 首先,如果你在web.xml文件中的配置是这样的:?context-param param-namecontextConfigLocation/param-name?param-value /WEB-INF/classes/applicationContext-*.xml /param-value?/context-param那么需要在工程下创建一个以applicationContext- 为开头的xml文件eg:applicationContext-jobconfig.xmlxml的头和结尾部分跟其他spring配置文件相似,就不赘述,正文如下:?bean id=youJobName(类别名) class=com.******.YourJobClassLocation(类的定位) /?bean id=doYourJob(别名) class=org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean property name=targetObject?ref bean=youJobName(类别名)/ /property property name=targetMethod?valuerunMethodName(定时执行的方法名)/value /property ?/bean??bean id=youJobNameTrigger(触发器别名) class=org.springframework.scheduling.quartz.CronTriggerBean property name=jobDetail ref bean=doYourJob(别名)/ /property property name=cronExpression?value0 0/20 * * * ?(定时的时间配置)/value /property?/bean?bean id=doScheduler class=org.springframework.scheduling.quartz.SchedulerFactoryBean property name=triggers list?ref local=youJobNameTrigger(触发器别名)/? /list /property?/bean这样的配置几本就可以运转了,但是有一个地方可能是你需要根据你的需求来确定的,那就是触发时间。下面有一些关于时间配置的说明:字段顺序?允许值?允许的特殊字符秒0-59, - * /分0-59, - * /小时0-23, - * /日期1-31, - * ? / L W C月份1-12?或者 JAN-DEC, - * /星期1-7?或者 SUN-SAT, - * ? / L C #年(可选)留空, 1970-2099, - * /The * character is used to specify all values. For example, * in the minute field means every minute. “*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。 The ? character is allowed for the day-of-month and day-of-week fields. It is used to specify no specific value. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification. “?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。 月份中的日期和星期中的日期这两个元素时互斥的一起应该通过设置一个问号(?)来表明不想设置那个字段The - character is used to specify ranges For example 10-12 in the hour field means the hours 10, 11 and 12. “-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。 The , character is used to specify additional values. For example MON,WED,FRI in the day-of-week field me
显示全部