zoukankan      html  css  js  c++  java
  • 任务调度SpringTask

    一、什么是任务调度

      在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作。常见的任务调度框架有Quartz和SpringTask等。spring task,我们可以将它比作一个轻量级的Quartz,使用简单方便,除spring相关的包外不需要额外的包。

    二、SpringTask入门案例

      (1)创建maven工程,并引入spring的相关依赖,同时配置tomcat7插件

      (2)添加配置文件applicationContext-task.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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">
        <context:component-scan base-package="com.pinyougou.task"/>
        <task:annotation-driven/>
    </beans>

      (3)添加web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">
      <!-- 加载spring容器 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext-*.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    </web-app>

      (4)编写类

    @Component
    public class SeckillTask {
        /**
         * 刷新秒杀商品
         */
        @Scheduled(cron="* * * * * ?")
        public void refreshSeckillGoods(){
            System.out.println("执行了任务调度"+new Date());        
        }        
    }
  • 相关阅读:
    灾备架构图
    Dashboard有什么用
    第一次使用Docker的完整过程
    网页端的消息接收方式
    我理解中的应用架构
    数字化转型模型
    工业4.0的下一个十年
    一文读懂华为全屋智能一文读懂华为全屋智能
    支付总架构解析
    spring boot:shardingsphere+druid整合seata分布式事务(spring boot 2.3.3)
  • 原文地址:https://www.cnblogs.com/yft-javaNotes/p/10691803.html
Copyright © 2011-2022 走看看