Spring Task 定时任务

Stella981
• 阅读 537

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

     //首先要定义schema 空间
     <?xml version="1.0" encoding="UTF-8"?>  
        <beans xmlns="http://www.springframework.org/schema/beans"  
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframe            work.org/schema/task"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.o            rg/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring            -task-3.0.xsd">  
        
     //先来一种 比较笨重的方法  ...要是多个定时任务 就有点吐啦
             <bean id="schedule" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="testTrigger" />
                </list>
            </property>
        </bean>

         <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail" ref="testJobDetail" />
            <property name="cronExpression" value="0 0/1 * * * ?" />
        </bean>

        <bean id="testJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="targetTestService" />  <!--定时任务的类-->
            <property name="targetMethod" value="job1" /> <!--要执行的方法-->
        </bean>

       <bean id="targetTestService" class="com.tepusoft.web.TaskJob"
            scope="prototype">
      </bean>

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

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

基于注解的方式:

        <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />

        // 定义  POJO 定时任务类 (基于注解的)  一定要spring容器扫描到哟
        /**
         * Spring3 @Scheduled 演示
         * @author GIE 2015年8月10日22:11:17
         */
        @Component
        public class SpringTaskDemo {
        @Scheduled(cron = "0/5 * * * * *")
        void doSomethingWith(){
        System.out.println("I'm doing with cron now!");
    }
    }
点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
ssm框架整合
applicationContext.xml<beansxmlns"http://www.springframework.org/schema/beans"xmlns:context"http://www.springframework.org/schema/context"xmlns:p"http://www.springframew
Stella981 Stella981
2年前
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
Stella981 Stella981
2年前
Spring 使用注解方式进行事务管理
使用步骤:步骤一、在spring配置文件中引入<tx:命名空间<beansxmlns"http://www.springframework.org/schema/beans" xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance" xmlns:tx"http
Stella981 Stella981
2年前
Spring Bean 枚举属性注入
1、名称空间配置<beans xmlns"http://www.springframework.org/schema/beans"       xmlns:util"http://www.springframework.org/schema/util"       xsi:schemaLocation"http://ww
Stella981 Stella981
2年前
Dubbo通过注解实现RPC调用
启动Dubbo服务有2个方式,1是通过xml配置,2是通过注解来实现,这点和Spring相似。采用XML配置如下:<?xmlversion"1.0"encoding"UTF8"?<beansxmlns"http://www.springframework.org/schema/beans"
Easter79 Easter79
2年前
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:/
Wesley13 Wesley13
2年前
Spring中hibernateTemplate的使用
1.<?xmlversion\"1.0"encoding\"UTF8"?2.<beansxmlns\"http://www.springframework.org/schema/beans"3.xmlns:xsi\"http://www.w3.org/2001/XMLSchemainstance"xmlns:con
Stella981 Stella981
2年前
Redis在SSM框架的使用(三)
Spring整合springdataredis1、springredis.xml<?xmlversion"1.0"encoding"UTF8"?<beansxmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http
Easter79 Easter79
2年前
Spring之级联属性
新建XML文件<?xmlversion"1.0"encoding"UTF8"?<beansxmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http://www.w3.org/2001/XMLSchemainsta
Wesley13 Wesley13
2年前
ActiveMQ的spring配置文件
从官网copy出来,做了部分修改:1<?xmlversion"1.0"encoding"UTF8"?2<beansxmlns"http://www.springframework.org/schema/beans"3xmlns:xsi"http://www.w3.org/2