zoukankan      html  css  js  c++  java
  • Spring的Task任务

    转自:http://liuna718-163-com.iteye.com/blog/2215076

    Spring Task提供两种方式进行配置,一种是annotation(标注),而另外一种就是XML配置了。因为任务调度这样的需求,通常改动都是比较多的,如果用annotation的方式的话,改动就变得麻烦了,必须去重新编译。所以,我只是选择用XML配置的方式。

    1.XML方式

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
     4     xsi:schemaLocation="
     5           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
     7 
     8     <bean id="reminderProcessor" class="com.foo.task.ReminderProcessor">
     9         <property name="workers">
    10             <array value-type="com.foo.task.Worker">
    11                 <ref bean="projectScheduleRemindWorker" />
    12             </array>
    13         </property>
    14     </bean>
    15 
    16     <!-- 配置任务线性池 -->
    17      <task:executor id="executor" pool-size="3" />
    18     <task:scheduler id="scheduler" pool-size="3" />
    19     <!-- 启用annotation方式 -->
    20     <task:annotation-driven scheduler="scheduler"
    21         executor="executor" proxy-target-class="true" />
    22 
    23     <task:scheduled-tasks scheduler="scheduler">
    24         <task:scheduled ref="reminderProcessor" method="process"
    25             cron="0 0 12 * * ?" />
    26     </task:scheduled-tasks>
    27 </beans>

     2.注解

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:sec="http://www.springframework.org/schema/security"
     6     xmlns:task="http://www.springframework.org/schema/task"
     7     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
     8         http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
     9         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    10         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
    11         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    12         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
    13         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
    14     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx">
    15     <!—spring扫描注解的配置   -->
    16     <context:component-scan base-package="com.test.cache" />
    17     <!-- 配置任务线性池 -->
    18     <task:scheduler id="scheduler" pool-size="10" />
    19     <task:executor id="executor" pool-size="10" />
    20     <!—开启这个配置,spring才能识别@Scheduled注解   -->
    21     <task:annotation-driven scheduler="scheduler" executor="executor" />
    22 </beans>

     java的类

     1 package com.test.cache;
     2 import org.springframework.scheduling.annotation.Scheduled;
     3 import org.springframework.stereotype.Component;
     4 
     5 @Component
     6 public class CrontabTask {
     7     
     8     @Scheduled(cron = "0 0/5 * * * *")
     9     public void autoBid() {
    10         System.out.println("执行定时任务");
    11     }
    12     
    13 }
  • 相关阅读:
    chrome安装插件
    如何在JIRA中有效使用关注和@提及 我正在关注的问题 提及我的问题 在仪表板上显示
    SQL Server中查找包含某个文本的存储过程 SQL 查找存储过程中出现过的文字怎么查询 查询整个数据库中出现的文本 sql 全局搜索
    Postman中添加真实请求(Chrome Networks中的全部请求,含https)copy as har
    SCOPE_IDENTITY 和 @@IDENTITY
    The underlying connection was closed: An unexpected error occurred on a rece
    mybatis中参数为list集合时使用 mybatis in查询
    如何在Mybatis的xml文件调用java类的方法
    Java中List, Integer[], int[]的相互转换
    ASP .NET Core HTTP Error 502.5 – Process Failure
  • 原文地址:https://www.cnblogs.com/sharpest/p/10021197.html
Copyright © 2011-2022 走看看