1、:Spring中的定时器的使用场景
这个定时器 就相当于是js中的 setInteval 值不过这个定时器的功能可能更加的强大
定时任务一般用在哪里呢?
1>:每个月月末的时候要统计这个月的销量问题(30号晚上12点生成报表 + 报名表的名字 就是时间)
2>:商城系统(用户下单完成 15分钟之后 还没有 付款 自动解锁这个库存)通知用户
3>:做那种库存预警(当库存的预警数量大于了商品的实际能卖的数量的时候 就要通知用户进货)
2、Spring中定时器的使用
1>:导入Spring的相关包
2>:编写我们的定时任务
@Component
public class MyTask {
/**
* @Scheduled:就表示的是是一个定时任务
* cron:用来填写啥时候触发这个事件
*/
@Scheduled(cron="0/5 * * * * ?")
public void timer(){
System.out.println("任务执行了....");
}
}
3>:编写配置文件启动定时任务(bean-timer.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!--配置定时任务开关-->
<task:annotation-driven />
<!--配置的是Spring的扫描包-->
<context:component-scan base-package="com.qf" />
</beans>
3、分布式下的定时任务Quartz的使用(一般用在分布式下)
1>:导包(Quartz的包)
<!-- quartz 的jar -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
2>:编写定时任务
public class MyTask2 implements Job{
/**
* 这个方法就是任务调度的时候执行的方法
*/
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//在这里你就可以写业务逻辑了
System.out.println("任务执行了。。。。我是 JOB");
}
}
3>:编写配置文件
(1)接入编写好的定时任务
(2)申明触发器
(3)申明调度器
<!--定义这个工作的任务-->
<bean id="jobDetailFactoryBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!--这个叫做给任务取名字-->
<property name="name" value="myTask1"></property>
<!--这个叫做给任务分组-->
<property name="group" value="myTaskGroup1"></property>
<!--如果设置为false的话 没有活动的任务将会被删除-->
<property name="durability" value="true"></property>
<!--设置这个ob -->
<property name="jobClass" value="com.qf.task.MyTask2"></property>
<!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 -->
<property name="applicationContextJobDataKey" value="applicationContext"/>
</bean>
<!--定义的是定时任务的触发器-->
<bean id="cronTriggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!--给这个触发器取名字-->
<property name="name" value="myTaskTrigger"></property>
<!--定义触发器的分组-->
<property name="group" value="myTaskTriggerGroup"></property>
<!--注入触发器的工作任务-->
<property name="jobDetail" ref="jobDetailFactoryBean"></property>
<!--定义的是触发器的事件格式的表达式-->
<property name="cronExpression" value="0/3 * * * * ?"></property>
</bean>
<!--定义的是调度器-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTriggerFactoryBean"/>
</list>
</property>
</bean>