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());        
        }        
    }
  • 相关阅读:
    asp.net Forms验证跨域页面不能访问的问题
    JavaScript创建命名空间
    DataTable转换成JSON字符串的函数
    javascript 正确截取单字节和双字节混和字符串的方法
    异常详细信息: 不能通过已删除的行访问该行的信息
    HttpUtility.ParseQueryString直接从字符串URL中提取参数
    支持函数,变量的算术表达式计算(三、加入函数)
    mp3 分类管理工具
    我好累
    电饭煲是如何知道饭已熟了的
  • 原文地址:https://www.cnblogs.com/yft-javaNotes/p/10691803.html
Copyright © 2011-2022 走看看