1.定时任务的几种实现可以看这里:http://gong1208.iteye.com/blog/1773177

2.需要导入spring的jar包,可以参看之前的【spring】相关文章

3.这里使用的是基于注解的方式完成定时任务,在spring的配置文件中配置如下 applicationContext.xml:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<context:component-scan base-scan="com.raipeng.work.spring.task"/>
<task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true"/>
<task:executor id="executor" pool-size="5"/>
<task:scheduler id="scheduler" pool-size="10"/>
</beans>

4.需要定时的 HelloWorldTask.java

package com.raipeng.work.spring.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* Created by 111 on 2015/11/23.
*/
@Component
public class HelloWorldTask { @Scheduled(cron = "0 * 16 * * ?") //每天从下午四点到四点59分每分钟执行一次
public void sayHello(){
System.out.println("hello world!");
}
}

5.控制台打印结果

INFO: Root WebApplicationContext: initialization completed in 930 ms
[2015-11-23 04:46:12,680] Artifact work-test:war exploded: Artifact is deployed successfully
[2015-11-23 04:46:12,680] Artifact work-test:war exploded: Deploy took 2,397 milliseconds
十一月 23, 2015 4:46:19 下午 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\Program Files\apache-tomcat-7.0.59-windows-x64\apache-tomcat-7.0.59\webapps\manager
十一月 23, 2015 4:46:19 下午 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.59-windows-x64\apache-tomcat-7.0.59\webapps\manager has finished in 83 ms
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!

6.cronExpression(时间表达式)具体使用方法可以看这里:cronExpressiion表达式

7.以上只是一个简单的使用,定时任务可以用于日志分析,选择定时在午夜完成。

【spring】task 任务调度(定时任务)的更多相关文章

  1. 【Spring Task】定时任务详解实例-@Scheduled

    Spring的任务调度,采用注解的形式 spring的配置文件如下,先扫描到任务的类,打开spirng任务的标签 <beans xmlns="http://www.springfram ...

  2. Spring task任务调度详解

    spring内部有一个task是Spring自带的一个设定时间自动任务调度 task使用的时候很方便,但是他能做的东西不如quartz那么的多! 可以使用注解和配置两种方式,配置的方式如下 引入Spr ...

  3. spring task的定时任务突然断了

    spring定时任务只开启一个线程去工作也就是串行工作,定时调度任务出现阻塞导致线程终止 加上这个试试 <!-- <task:annotation-driven /> --> ...

  4. Spring Task 任务调度(定时器)

    1.任务调度SpringTask 1.1什么是任务调度 在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作.常见的 ...

  5. spring task:annotation-driven 定时任务

    1.配置文件加上<task:annotation-driven/> 2.要运行的方法前加上 @Scheduled(cron="0 00 12 1 * ?")  //每月 ...

  6. 任务调度 Spring Task 4(一)

    深入浅出spring task定时任务 在工作中有用到spring task作为定时任务的处理,spring通过接口TaskExecutor和TaskScheduler这两个接口的方式为异步定时任务提 ...

  7. Spring 使用介绍(十二)—— Spring Task

    一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...

  8. 任务调度的方式:Timer、ScheduledExecutorService、spring task、quartz、XXL-JOB、Elastic-Job

    任务调度 定时任务调度:基于给定的时间点.给定的时间间隔.给定的执行次数自动执行的任务. Timer 介绍 Timer,简单无门槛,一般也没人用. Timer位于java.util包下,其内部包含且仅 ...

  9. 基于Spring Task的定时任务调度器实现

    在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便. 只要跟需要定时执行的方法加上类似 @Schedu ...

随机推荐

  1. Android.mk学习

    2019-03-31 学习变量 $(call my-dir) /usr/bin2/android-ndk-r16/build/core $(CLEAR_VARS) /usr/bin2/android- ...

  2. springCloud笔记

    分布式和集群的理解:比如在一个厨房有两个厨师,一个炒菜,一个洗菜,各自做不同的事情,但是却在合作,这种叫做分布式,两个都在炒菜或者都在做菜,就叫做集群. eureka的是springCloud的注册中 ...

  3. C#事务提交

    using (System.Transactions.TransactionScope transcope = new System.Transactions.TransactionScope()) ...

  4. Vmware Workstation - linux系统下 VmTools 安装

    程序版本 : VMware® Workstation 14 Pro 系统环境 : win10 64位下 ubuntu-14.04.5-desktop-amd64 问题:在运行linux系统过程中,de ...

  5. ScrollView无法滚动

    ScrollView视图无法滚动 箭头所指地方应该用dp单位的数值

  6. shell awk处理过滤100万条数据

    背景: 100万条数据.格式如下: ID 地址 1895756546931805 安徽省六安市裕安区固镇镇佛俺村柳树队5758 安徽省蒙城县岳坊镇胡寨村小组小胡寨庄6号 183494167409969 ...

  7. 关于n维和n-1维欧式空间

    我们从小就说,"点动成线,线动成面,面动成体",其中的空间的概念到底是啥?之前没有好好想过,在机器学习中多次遇到"空间"."超平面",&qu ...

  8. Py福利,基于uiautomatorviewer 的Python 自动化代码自动生成工具分享(jar已发布GitHub,欢迎Star)

    前言做UI自动化无论你用SDK自带的uiautomatorviewer还是Macaca还是Appium自动的inspector,代码最多的就是那些繁琐重复的找元素后点击,输入,长按.....等.现在偷 ...

  9. Python学习过程中各个难点---函数篇

    对于函数,我一直分不清局部变量与全局变量,今天又好好研究了下,终于搞清楚了. 例子: 其次对于global这个关键字我也是一知半解的状态,之前整个人都是懵懵的,现在搞明白了 匿名函数: 匿名函数使用关 ...

  10. ES6中的箭头函数和普通函数有什么区别?

    1.普通函数中的this总是指向调用它的那个对象, 箭头函数没有自己的this,他的this永远指向其定义环境,任何方法都改变不了其指向,如call().bind().apply().(正是因为它没有 ...