前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring任务调度之scheduler

spring任务调度之scheduler

作者头像
week
发布2018-08-27 09:29:20
3550
发布2018-08-27 09:29:20
举报
文章被收录于专栏:用户画像用户画像

一、 前言

1. 在使用spring task scheduler 之前,需要先学习搭建springmvc框架,了解cron表达式

① springmvc框架搭建可参考

http://blog.csdn.net/jxq0816/article/details/76084911

② cron表达式可参考

http://blog.csdn.net/jxq0816/article/details/51620400

2. github源代码

https://github.com/jxq0816/task_scheduler

3. 运行结果图

二、搭建过程

1. web.xml 配置应用上下文信息context-param,这里提供了三种方式,根据实际需求,选择一种方式即可

①spring-context-task.xml通过xml配置cron

②spring-context-task-pool.xml添加任务线性池的相关配置信息

③spring-context-task-pool-annotation.xml添加注解方式来设置cron

代码语言:javascript
复制
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <context-param>
   <!-- applicationContext.xml默认地址目录是/WEB-INF-->
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:/applicationContext.xml
      classpath:/spring-context-task.xml
      classpath:/spring-context-task-pool.xml
      classpath:/spring-context-task-pool-annotation.xml
    </param-value>
  </context-param>

  <!-- 加入ContextLoaderListener 启动Web容器时,自动装配ApplicationContext的配置信息-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--DispatcherServlet是前置控制器,拦截匹配的请求,Servlet拦截匹配规则要自己定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理-->
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/dispatcherServlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2. 应用上下文信息

必要信息:

①在xmlns中添加

代码语言:javascript
复制
    xmlns:task="http://www.springframework.org/schema/task"

②在xsi:schemaLocation中添加内容

代码语言:javascript
复制
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd

xml样例

① spring-context-task.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        ">

    <bean id="segmentTask" class="com.week7i.share.SegmentTask"/>

    <task:scheduled-tasks>
        <task:scheduled ref="segmentTask" method="segment" cron="*/1 * * * * ?"/>
    </task:scheduled-tasks>

</beans>

② spring-context-task-pool.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="segmentTask" class="com.week7i.share.SegmentTask"/>

    <!-- 配置任务线性池 -->
    <!--参数pool主要解决,多个调度并行的问题-->
    <!--如果有5个scheduled-task任务,建议设置3到5个调度-->
    <!--如果配置参数为1,5个task任务会依次执行,如果一个时间超出,后面的任务一直在等待,影响业务-->
    <task:scheduler id="sgScheduler" pool-size="10"/>

    <task:scheduled-tasks scheduler="sgScheduler">
        <task:scheduled ref="segmentTask" method="segmentPool" cron="*/1 * * * * ?"/>
    </task:scheduled-tasks>

</beans>

 ③ spring-context-task-pool-annotation.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.week7i.share"/>
    <!-- 配置任务线性池 -->

    <!--task:scheduler 参数pool主要解决,多个调度并行的问题-->
    <!--如果有5个scheduled-task任务,建议设置3到5个调度-->
    <!--如果配置参数为1,5个task任务会依次执行,如果一个时间超出,后面的任务一直在等待,影响业务-->

    <!--The size of the ScheduledExecutorService's thread pool. The default is 1.-->
    <task:scheduler id="annotationScheduler" pool-size="10"/>
    <!--The size of the executor's thread pool as either a single value or a range-->
    <!--(e.g. 5-10). If no bounded queue-capacity value is provided, then a max value-->
    <!--has no effect unless the range is specified as 0-n. In that case, the core pool-->
    <!--will have a size of n, but the 'allowCoreThreadTimeout' flag will be set to true.-->
    <!--If a queue-capacity is provided, then the lower bound of a range will map to the-->
    <!--core size and the upper bound will map to the max size. If this attribute is not-->
    <!--provided, the default core size will be 1, and the default max size will be-->
    <!--Integer.MAX_VALUE (i.e. unbounded).-->
    <task:executor id="annotationExecutor" pool-size="10"/>

    <!-- 启用annotation方式,在方法添加 @Scheduler,可以配置cron定时任务 -->
    <task:annotation-driven scheduler="annotationScheduler" executor="annotationExecutor" proxy-target-class="true"/>
    <!-- 如果不需要配置线性池,直接添加<task:annotation-driven />即可 -->

</beans>

4. 任务方法

 ① SegmentTask.java

代码语言:javascript
复制
package com.week7i.share;

import org.springframework.stereotype.Component;

public class SegmentTask {

    public void segment(){

        System.out.println("spring scheduled task!");
    }

    public void segmentPool(){
        
        System.out.println("spring scheduled task pool!");
    }

}

 ② SegmentAnnotationTask.java

代码语言:javascript
复制
package com.week7i.share;

import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Lazy(value=false)

public class SegmentAnnotationTask {

    @Scheduled(cron = "*/1 * * * * ?")//每隔1秒执行一次
    public void segment(){
        
        System.out.println("spring  scheduled task annocation!");
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年06月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档