项目框架: SpringMVC、MyBatis、JSP

1. 首先配置spring.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: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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!-- 注解定时任务 -->
<task:annotation-driven/>

2. 配置定时任务类

@Component
public class MessageSendService { /**
* 记录日志类
*/
public Log logger = LogFactory.getLog(this.getClass()); /**
* 定时任务方法
*/
@Scheduled(cron = "0/10 * * * * ? ")
public void send(){
logger.info("start.....");
} }

@Component 注解是让Spring可以扫描到并初始化,第一步的配置就是做这个用的

@Scheduled 注解是配置定时任务的执行时间,上面的配置是让定时任务每10执行一次send()方法

3. 实际的项目中定时任务的执行时间可能要在配置文件中配置,就需要用到另一个注解 @PropertySource

@Component
@PropertySource(value="classpath:application.properties")
public class MessageSendService { /**
* 记录日志类
*/
public Log logger = LogFactory.getLog(this.getClass()); /**
* 方法
*/
@Scheduled(cron = "${jobs.message}")
public void send(){
logger.info("start....."); } }

@PropertySource 指定配置文件的位置,和配置文件的名称

@Scheduled 注解做相应的修改

4. application.properties 文件配置

## 轮询时间配置
jobs.message=0/10 * * * * ?

这个定时任务时间配置和linux的crontab差不多

5. Scheduled 参数1

@Scheduled(fixedDelay = 5000)
public void doSomething() {
// something that should execute periodically
}

这个方法将以一个固定延迟时间5秒钟调用一次执行,这个周期是以上一个调用任务的完成时间为基准,在上一个任务完成之后,5s后再次执行

当方法执行超过5秒,下一个轮询发现有正在执行的方法,直接跳过

6. Scheduled 参数2

@Scheduled(fixedRate = 5000)
public void doSomething() {
// something that should execute periodically
}

这个方法将以一个固定速率5s来调用一次执行,这个周期是以上一个任务开始时间为基准,从上一任务开始执行后5s再次调用

当方法执行时间超过5秒,下一个轮询会阻塞,上一个任务执行完成,立即执行此次轮询方法

SpringMVC基于注解方式的quartz的更多相关文章

  1. SpringMVC入门(基于注解方式实现)

    ---------------------siwuxie095                             SpringMVC 入门(基于注解方式实现)         SpringMVC ...

  2. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  3. springMVC基于注解的控制器

    springMVC基于注解的控制器 springMVC基于注解的控制器的优点有两个: 1.控制器可以处理多个动作,这就允许将相关操作写在一个类中. 2.控制器的请求映射不需要存储在配置文件中.使用re ...

  4. Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存

    一  基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 -->    <bean cl ...

  5. Elasticsearch-mapper 基于注解方式生成mapping(2.0以上)

    Elasticsearch生成mapping的方式上有多种方式,我们可以把mapping做成配置文件,也可以用spring-data-elasticsearch基于注解生成. 在基于注解生成这种方式上 ...

  6. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  7. SpringMVC的注解方式

    mvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  8. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  9. 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例

    之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...

随机推荐

  1. jQuery table tr隔行变色,鼠标移入移出变色,鼠标点击变色

    .trover { background: #f9f9f9; } .trclick { background: #c4e8f5; } .treven{ background:#CCFFCC; } .t ...

  2. Spring WebSocket实现消息推送

    第一步: 添加Spring WebSocket的依赖jar包 (注:这里使用maven方式添加 手动添加的同学请自行下载相应jar包放到lib目录) <!-- 使用spring websocke ...

  3. Nagle's algorithm

    w41字节的数据包只有1字节的可用信息.以减少数据包发送量来提高TCP/IP网络性能. https://en.wikipedia.org/wiki/Nagle's_algorithm https:// ...

  4. xpath-grab english name

    from scrapy.spider import Spider from scrapy.crawler import CrawlerProcess import pymysql conn = pym ...

  5. Linux常用软件(以及特殊命令)清单(ubuntu)

    LibreOffice 解压缩命令 unar .tar 创建新文档命令:touch.vi/vim 浏览器:google chrome.firefox

  6. php面向对象--继承

    继承 extends 关键字来继承类 被继承的类,我们称之为父类 继承后的类,我们称之为子类 子类继承父类非私有的属性和方法 public 在本类,子类,以及类的外部都访问 protected 保护型 ...

  7. window7修改hosts文件

    以管理员身份登录系统 ,修改 C:\Windows\System32\drivers\etc\hosts文件, 在最下面加入类似 192.168.80.10 master192.168.80.11 s ...

  8. java基础05 选择结构

    选择结构 public class Demo01Change { public static void main(String[] args) { /** * 实现等量的转换 */ int a = 5 ...

  9. UVA11426 GCD - Extreme (II)---欧拉函数的运用

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. Flask之session相关

    Flask的session简介 除请求对象之外,还有一个 session 对象.它允许你在不同请求间存储特定用户的信息.它是在 Cookies 的基础上实现的,并且对 Cookies 进行密钥签名要使 ...