spring 定时器的使用

因为最近一直在我们组搞后台,但是最近遇到的一个问题就是说。

每天都要给数据库动态的建立 一张表,然后每天动态的给每天的新表插入数据,因为以后这些数据 可能公司搞云平台那边会需要。

所以就笨笨的开始弄, 反正现在对后台是已经忘记好多辣,就知道有个定时器能定时执行任务,再就是想的办法。看看sqlserver 有没有办法定时执行一个任务什么的,最后结果是用了 sping的定时器,用到的是 quartz,所以就刚好总结一下所有的

现在是有3种 定时器

  1. java 自带的 TimerTask
  2. quartz
  3. spring 3.0 以后自带的 task

    2.直接开始说quartz(我就用的这个)

用法有2种
2.1 一种是要继承quazrtz
2.2 另外一种是不继承 quazrtz

每隔指定时间则触发一次,在Quartz中对应的触发器为:org.springframework.scheduling.quartz.SimpleTriggerBean
每到指定时间则触发一次,在Quartz中对应的调度器为:org.springframework.scheduling.quartz.CronTriggerBean

2.3.1 继承quazrtz

public class AAA extends QuartzJobBean { 
public void setAge(int age,String name) {  
this.age= age;  
this.name = name;
}  

@Override  
protected void executeInternal(JobExecutionContext context)  
throws JobExecutionException {  
  System.out.println("定时任务执行中…");  
}  
}  

2.3.2 Spring 配置文件中 定时器的配置代码,先配置工作类

<bean name="AAA" class="org.springframework.scheduling.quartz.JobDetailBean">  
<!--jobclass 工作类 指向的是你的继承quzrtz的com.xx.AAA的这个类--!>
<property name="jobClass" value="com.xx.AAA" />  

<!-- 配置的你的com.xx.AAA 这个类需要注入的属性值 --!>
<property name="jobDataAsMap">  
<map>  
<entry key="age" value="20" />  
<entry key="name" value="LL" /> 
</map>  
</property>  
</bean>  

2.3.3 上面说过的2种触发器效果会不一样,配置触发器
SimpleTriggerBean

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
<!--这个jobDetail 后属性的就是指向你上面配置好的bean name--!>
<property name="jobDetail" ref="AAA" />  
<!--这个人物启动后,延迟多长时间开始执行--!>
<property name="startDelay" value="0" />
<!-- 执行的循环周期, 每2秒执行一次哦 -->  
<property name="repeatInterval" value="2000" />
</bean>

CronTriggerBean

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
<property name="jobDetail" ref="AAA" />  
<!—每天12:00运行一次 -->  
<property name="cronExpression" value="0 0 12 * * ?" />  
</bean>  

2.3.4 配置调度工厂让这个触发器启动

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
<property name="triggers">  
<list>  
<!-- 你的上面配置好的触发器的名字哦--!>
<ref bean="cronTrigger" />  
</list>  
</property>  
</bean>  

2.4.1 不继承特定的类了

public class AAA{  
public void doAAA() {  
System.out.println("逻辑执行代码...");  
}  
} 

2.4.2 在spring配置文件中配置作业类

<!-- 给你的这个配置 bean 起名字 随便起 --!>
<bean id="AAA"  
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  

<!-- targetObject属性, 里面是指向你的工作类哦--!>
<property name="targetObject">  
<bean class="com.xx.AAA" />  
</property>  
<!-- targetMethod属性, 里面是指向你的工作类中执行哪个方法--!>
<property name="targetMethod" value="doAAA" />  
<!-- 作业不并发调度 -->  
<property name="concurrent" value="false" />
</bean> 

2.4.3 配置触发器(这个跟上面的基本一样了。注释很清楚辣2种)

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
<property name="jobDetail" ref="AAA" />  
<property name="startDelay" value="0" />
<property name="repeatInterval" value="2000" />
</bean>  
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
<property name="jobDetail" ref="job2" />  
<!—每天12:00运行一次 -->  
<property name="cronExpression" value="0 0 12 * * ?" />  
</bean>  

2.4.4 配置调度工厂

bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
<property name="triggers">  
<list>  
<ref bean="cronTrigger" />  
</list>  
</property>  
</bean>  

3.Spring 自带的 Task 工作类

3.1

import org.springframework.stereotype.Service;  
@Service  
public class TaskJob {  

    public void job1() {  
        System.out.println(“任务进行中。。。”);  
    }  
}  

spring中配置,首先要在头文件加命名空间

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:task="http://www.springframework.org/schema/task"   
    。。。。。。  
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 

spring中配任务

 <task:scheduled-tasks>   
        <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   
</task:scheduled-tasks>  
<context:component-scan base-package=" com.gy.mytask " />  

context:component-scan base-package
这个配置不消多说了,spring扫描注解用的。

3.2 注解的形式

import org.springframework.scheduling.annotation.Scheduled;    
import org.springframework.stereotype.Component;  

@Component(“taskJob”)  
public class TaskJob {  
    @Scheduled(cron = "0 0 3 * * ?")  
    public void job1() {  
        System.out.println(“任务进行中。。。”);  
    }  
}  

配置

<?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:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:task="http://www.springframework.org/schema/task"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
        http://www.springframework.org/schema/context   
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"  
    default-lazy-init="false">  


    <context:annotation-config />  
    <!—spring扫描注解的配置   -->  
    <context:component-scan base-package="com.gy.mytask" />  

<!—开启这个配置,spring才能识别@Scheduled注解   -->  
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/>  

注解为什么要那样配。原文件

@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface Scheduled  
{  
  public abstract String cron();  

  public abstract long fixedDelay();  

  public abstract long fixedRate();  
}  

可以看出该注解有三个方法或者叫参数,分别表示的意思是:
cron:指定cron表达式
fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

cronExpression 表达式的配置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值