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());        
        }        
    }
  • 相关阅读:
    Linux 常用命令大全
    冒犯了,问大家一个问题,会linux的进来帮我解决一下
    Linux 软件安装
    Ubuntu 12.04出现“device not managed”错误
    Linux 系统下vi编辑器的使用方法(copy其他人网站的,留着自己查用的)
    tar的打包压缩与解压缩,并解压到指定的目录
    Ubuntu文件的复制、移动和删除命令
    当Ubuntu12.04 如何获取root权限
    windows是用vnc远程连接ubuntu的方法
    Ubuntu 12.04 在root登陆之后没有声音的解决方法
  • 原文地址:https://www.cnblogs.com/yft-javaNotes/p/10691803.html
Copyright © 2011-2022 走看看