我所知道的java定时任务的几种常用方式:


1、spring schedule注解的方式;
2、spring schedule配置文件的方式;
3、java类继承TimerTask;

第一种方式的实现:


1、使用maven创建spring项目,schedule在spring-context.jar的包下边,因此需要导入与之相关的包;同时,我配的是spring
web项目,也同时导入了spring-web和spring-webmvc的包,如下:
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

maven导包配置只需要如下就好,其他相关的依赖,maven会自行解决。


2、配置spring项目的基础文件spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:task="http://www.springframework.org/schema/task"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">

    <!-- 开启定时任务 -->
    <task:annotation-driven />
    <!-- 开启注解 -->
    <context:annotation-config />
    <!-- 指定相关的包路径 -->
    <context:component-scan base-package="scheduleTest"/>

</beans>


3、编写java业务代码,需要在类声明上边添加@Component注解,并在需要定时任务执行的方法声明上添加@Scheduled(cron = "0/5 * * * * ?")注解以及相关的参数。
参数使用可以参考http://blog.csdn.net/isnotsuitable/article/details/7464556,我示例中表示每五秒执行一次:
package scheduleTest;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * spring定时器1
 *
 * @author tuzongxun123
 *
 */
@Component
public class ScheduleTest {

    @Scheduled(cron = "0/5 * * * * ?")
    public void schTest1() {
        Date date = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sim.format(date);
        System.out.println("这是spring定时器1,每五秒执行一次,当前时间:" + dateStr);
    }
}

4、web项目的基础配置文件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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>appversion</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <listener>
    <description>spring监听器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

上边的配置中使用了spring的上下文监听器,在这种情况下项目启动后,spring.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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>appversion</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

这里是吧spring的监听器换成了mvc的调度器,在调度器加载的时候就运行spring.xml中的定时任务配置,因此启动项目后看到效果一样。如果把上边的监听器和mvc的调度器一起配在这里,会看到启动项目后同一时间内这个定时任务需要执行的业务会执行两次。

spring schedule定时任务(一):注解的方式的更多相关文章

  1. spring实现定时任务的两种方式之spring @scheduled注解方式

    1.使用spring的 scheduled使用注解的方式 这种方法的好处是:使用方便,配置少,提高开发效率: 缺点是:如果使用服务器集群部署方式的时候,其自身无法解决定时任务重复执行的问题. 2.首先 ...

  2. spring定时任务的注解实现方式

    STEP 1:在spring配置文件中添加相应配置,以支持定时任务的注解实现 (一)在xml里加入task的命名空间 <!-- beans里添加:--> xmlns:task=" ...

  3. Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志

    一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...

  4. spring中aop的注解实现方式简单实例

    上篇中我们讲到spring的xml实现,这里我们讲讲使用注解如何实现aop呢.前面已经讲过aop的简单理解了,这里就不在赘述了. 注解方式实现aop我们主要分为如下几个步骤(自己整理的,有更好的方法的 ...

  5. Spring练习,使用注解的方式,完成模拟用户的正常登录。要求如下: 使用注解方式开发模拟用户的正常登录。

    相关 知识 >>> 相关 练习 >>> 实现要求: 在该实践案例中,使用注解的方式,完成模拟用户的正常登录. 要求如下: 使用注解方式开发模拟用户的正常登录. 实现 ...

  6. spring schedule定时任务(二):配置文件的方式

    接着上一篇,这里使用spring配置文件的方式生成spring定时任务. 1.相应的web.xml没有什么变化,因此便不再罗列.同样的,相应的java代码业务逻辑改动也不大,只是在原来的基础上去掉@C ...

  7. Spring定时任务@Scheduled注解使用方式

    1.开篇 spring的@Scheduled定时任务相信大家都是十分熟悉.最近在使用过程中发现了一些问题,写篇文章,和大家分享一下.结论在最后,不想看冗长过程的小伙伴可以直接拉到最后看结论. 2.简单 ...

  8. spring实现定时任务的两种方式

    本文为博主原创,未经允许不得转载 项目中要经常事项定时功能,在网上学习了下用spring的定时功能,基本有两种方式,在这里进行简单的总结, 以供后续参考,此篇只做简单的应用. 1.在spring-se ...

  9. 浅谈spring配置定时任务的几种方式

    网上看到好多关于定时任务的讲解,以前只简单使用过注解方式,今天项目中看到基于配置的方式实现定时任务,自己做个总结,作为备忘录吧. 基于注解方式的定时任务 首先spring-mvc.xml的配置文件中添 ...

随机推荐

  1. python监控微信报警

    微信接口调用代码: #coding=utf8 import itchat from flask import Flask, request itchat.auto_login(enableCmdQR= ...

  2. Nginx几种负载均衡算法及配置实例

    本文装载自: https://yq.aliyun.com/articles/114683 Nginx负载均衡(工作在七层"应用层")功能主要是通过upstream模块实现,Ngin ...

  3. js基本语法与变量

    1.//声明变量,js是弱类型语言,变量类型声明示没有意义的.变量类型是随时可以变化的.var a = 1;      a = "haha";    a = false;2.注释与 ...

  4. codeM美团编程大赛初赛B轮E题

    题目描述 给出一个正整数n,我们把1..n在k进制下的表示连起来记为s(n,k),例如s(16,16)=123456789ABCDEF10, s(5,2)=11011100101.现在对于给定的n和字 ...

  5. 插上腾飞的翅膀:为asp.net core添加protobuf支持

    没时间解释了,快上车. 通过NuGet获取Zaabee.AspNetCoreProtobuf Install-Package Zaabee.AspNetCoreProtobuf 在Startup.cs ...

  6. ACE-6.1.0 linux 下的编译与安装步骤

    ACE-6.1.0 linux 下的编译与安装步骤  引用至http://www.cnblogs.com/liangxiaxu/archive/2013/03/07/2948417.html 1.从  ...

  7. LeetCode - 654. Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  8. Angular+ionic2+Echarts 实现图形制作,以饼图为例

    step1:添加插件echart; npm install echarts --save package.json文件中会在dependencies中添加echarts,如下图: step2:运行cm ...

  9. 处理springMvc中responsebody返回中文乱码

    法一:   @RequestMapping(value="/getUsersByPage",produces = public String getUsers  法二:在sprin ...

  10. 05-Git

    [Git]   [安装git] $ yum install git  #安装git $ ssh-keygen  #遇到输入符直接回车 $ cat ~/.ssh/id_rsa.pub #将这里的信息添加 ...