美文网首页
spring 定时器 cron

spring 定时器 cron

作者: 咖啡机an | 来源:发表于2018-09-13 09:56 被阅读0次

1. 注解配置quartz应用

一、引入jar包


二、spring配置文件中配置 (applicationContext.xml)

  1) xmlns和 xsi:schemaLocation配置
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd "

 2)task任务扫描**注解**
  <!-- 加载定时任务 -->
    <task:executor id="executor" pool-size="1" />
    <task:scheduler id="scheduler" pool-size="1" />
    <task:annotation-driven executor="executor" scheduler="scheduler"/>
 3)扫描的位置
      <context:component-scan base-package=""/>

2.spring cron 时间从配置文件获取

@Service
@PropertySource(value = "classpath:config/config.properties")
public class CataLogSchedule {
    @Scheduled(cron = "${equi.off.line.cycle}")
    public void decOfflineAlarm() {
     
            }
}
#config.properties配置文件 每分钟执行一次
equi.off.line.cycle=0 0/1 * * * ?

3.cron 表达式简单说明

 1.实例 
     0 0/1 * * * ? 
     每分钟执行一次  如 10:32:00  10:33:00  10:34:00
     0/5 * * * * ? 
     每5秒执行一次  如 10:32:05  10:33:00  10:34:10
 2.时间格式
     s m h d m w(?) y(?)    分别对应:  秒 分 小时 日 月 周 年,
     支持的具体数值 秒(0-59)  分(0-59)
     斜杠 (/) 表示时间的间隔

相关文章

网友评论

      本文标题:spring 定时器 cron

      本文链接:https://www.haomeiwen.com/subject/uvoigftx.html