谈一谈Spring中的定时任务

作者: 有效栈 | 来源:发表于2017-10-18 12:48 被阅读114次

    通常来说,如果要执行一个定时任务,基本上的操作是这样的:

    public class Task3 {  
        public static void main(String[] args) {  
            Runnable runnable = new Runnable() {  
                public void run() {  
                    // task to run goes here  
                    System.out.println("Hello !!");  
                }  
            };  
            ScheduledExecutorService service = Executors  
                    .newSingleThreadScheduledExecutor();  
            // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间  
            service.scheduleAtFixedRate(runnable, 10, 1, TimeUnit.SECONDS);  
        }  
    }  
    

    这种情况要main方法中显式的调用scheduleAtFixedRate方法才能运行。但是在Spring环境中或者Web环境中没有main方法,或者没有显式的调用入口,该如何调用调用这个方法呢?

    1.使用InitializingBean,当类被初始化时scheduleAtFixedRate被显式调用:

    public class ScheduleTask  implements InitializingBean,Runnable{
        @Override
        public void run() {
            System.out.println(  new Date() +"任务执行");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
    
            ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
            // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
            service.scheduleAtFixedRate(this, 10, 1, TimeUnit.SECONDS);
    
        }
    }
    

    beans.xml:

    <bean id="task"  class="com.alibaba.test.scheduling.ScheduleTask"/>
    

    2. 使用 @Scheduled注解:

    public class ScheduleTask{
        @Scheduled(fixedRate=1)
        public void run() {
            System.out.println(  new Date() +"任务执行");
        }
    }
    

    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"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:sofa="http://schema.alipay.com/sofa/schema/service"
        xmlns:context="http://www.springframework.org/schema/context"
           xmlns:task="http://www.springframework.org/schema/task"
    
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
             http://schema.alipay.com/sofa/schema/service http://schema.alipay.com/sofa/sofa-service-4-0-0.xsd
             http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
        default-autowire="byName">
        <bean id="task"  class="com.alibaba.test.scheduling.ScheduleTask"/>
      <task:annotation-driven>
      </task:annotation-driven>
    </beans>
    

    当然也可以指定定时任务线程池·。

    3.只使用使用XML配置:

    public class ScheduleTask{
    
        public void run() {
            System.out.println(  new Date() +"任务执行");
        }
    }
    
    <?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:p="http://www.springframework.org/schema/p"
        xmlns:sofa="http://schema.alipay.com/sofa/schema/service"
        xmlns:context="http://www.springframework.org/schema/context"
           xmlns:task="http://www.springframework.org/schema/task"
    
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
             http://schema.alipay.com/sofa/schema/service http://schema.alipay.com/sofa/sofa-service-4-0-0.xsd
             http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
        default-autowire="byName">
        <bean id="task"  class="com.alibaba.test.scheduling.ScheduleTask"/>
        <task:scheduler id="scheduler" pool-size="10"  />  <!--配置定时器-->
        <task:scheduled-tasks scheduler="scheduler">
            <task:scheduled ref="task" method="run" fixed-delay="1000" />
        </task:scheduled-tasks>
    </beans>
    

    相关文章

      网友评论

        本文标题:谈一谈Spring中的定时任务

        本文链接:https://www.haomeiwen.com/subject/bsbsuxtx.html