Spring Task 定时任务

推荐使用基于配置XML的形式 !!!

1     //首先要定义schema 空间 2     <?xml version="1.0" encoding="UTF-8"?>   3        <beans xmlns="http://www.springframework.org/schema/beans"   4            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframe            work.org/schema/task"   5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.o            rg/schema/beans/spring-beans.xsd   6        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring            -task-3.0.xsd">   7         8     //先来一种 比较笨重的方法  ...要是多个定时任务 就有点吐啦 9             <bean id="schedule" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 10         <property name="triggers"> 11 <list> 12 <ref bean="testTrigger" /> 13 </list> 14 </property> 15 </bean> 16 17      <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 18 <property name="jobDetail" ref="testJobDetail" /> 19 <property name="cronExpression" value="0 0/1 * * * ?" /> 20     </bean> 21 22     <bean id="testJobDetail" 23 class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 24 <property name="targetObject" ref="targetTestService" />  <!--定时任务的类--> 25 <property name="targetMethod" value="job1" /> <!--要执行的方法--> 26 </bean> 27 28    <bean id="targetTestService" class="com.tepusoft.web.TaskJob" 29 scope="prototype"> 30   </bean>

 再来一个比较简洁的多定时任务的配置(推荐)

1    <task:annotation-driven /> <!-- 定时器开关,一定要加-->   2    <bean id="taskTest" class="com.tepusoft.web.TaskJob"></bean>  <!--定时任务的类--> 3    <task:scheduled-tasks>   <!--这里面可以定义多个定时任务--> 4        <task:scheduled ref="taskTest" method="job1" cron="5/3 * * * * ?" />   5    </task:scheduled-tasks>

基于注解的方式:

1        <task:executor id="executor" pool-size="5" /> 2 <task:scheduler id="scheduler" pool-size="10" /> 3 <task:annotation-driven executor="executor" scheduler="scheduler" /> 4 5        // 定义  POJO 定时任务类 (基于注解的)  一定要spring容器扫描到哟 6        /** 7         * Spring3 @Scheduled 演示 8         * @author GIE 2015年8月10日22:11:17 9         */ 10        @Component 11        public class SpringTaskDemo { 12     @Scheduled(cron = "0/5 * * * * *") 13     void doSomethingWith(){ 14 System.out.println("I'm doing with cron now!"); 15 } 16    }
点赞
收藏

评论区

加载中...

相关推荐

ssm框架整合

applicationContext.xml<beansxmlns"http://www.springframework.org/schema/beans"xmlns:context"http://www.springframework.org/schema/context"xmlns:p"http://www.springframew

Spring MVC 配置文件dispatcher

1.<?xmlversion\"1.0"encoding\"UTF8"?2.<beansxmlns\"http://www.springframework.org/schema/beans"3.xmlns:mvc\"http://www.springframework.org/schema/mvc"4.xml

Spring 使用注解方式进行事务管理

使用步骤:步骤一、在spring配置文件中引入<tx:命名空间<beansxmlns"http://www.springframework.org/schema/beans" xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance" xmlns:tx"http

Spring Bean 枚举属性注入

1、名称空间配置<beans xmlns"http://www.springframework.org/schema/beans"       xmlns:util"http://www.springframework.org/schema/util"       xsi:schemaLocation"http://ww

Dubbo通过注解实现RPC调用

启动Dubbo服务有2个方式,1是通过xml配置,2是通过注解来实现,这点和Spring相似。采用XML配置如下:<?xmlversion"1.0"encoding"UTF8"?<beansxmlns"http://www.springframework.org/schema/beans"

Spring错误——Spring AOP——org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException

背景:学习切面,测试前置通知功能,xml配置如下<?xmlversion"1.0"encoding"UTF8"?<beansxmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http:/