使用spring整合Quartz实现—定时器功能

网友投稿 218 2023-08-07

使用spring整合Quartz实现—定时器功能

使用spring整合Quartz实现—定时器(Maven项目做演示)

不基于特定的基类的方法

一,开发环境以及依赖的jar包

Spring 4.2.6.RELEASE

Maven 3.3.9

Jdk 1.7

Idea 15.04

二,不可少的jar依赖(添加在maven项目里面的pom.xml文件里面)

org.springframework

spring-context-support

4.2.6.RELEASE

org.quartz-scheduler

quartz

2.2.1

三,实现定时器时使用到的文件:

planWorkExcute.java    --定时器执行的类

spring-plan.xml    --配置定时器信息的xml

四,实现定时器步骤:

1,创建 planWorkExcute.java文件  ,在   cc.royao.plantask   包下。

package cc.royao.plantask;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import org.apache.log4j.Logger;//可以删除

import org.springframework.beans.factory.annotation.Autowired;

public class PlanWorkExecute {

Logger logger = Logger.getLogger(this.getClass());//logger打印日志,可以去掉

/**

* 定时器执行的方法

*/

public synchronized void withdrawNoAuditTask() {

SimpleDateFormat outFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

System.out.println("开始提现免审核任务-------------------------------" + outFormat.format(new Date()));

logger.info("开始提现免审核任务-------------------------------");

System.out.println("结束提现免审核任务-------------------------------" + outFormat.format(new Date()));

logger.info("结束提现免审核任务-------------------------------");

}

}

2,创建spring-plan.xml  配置文件  注:创建一个定时器的配置文件就行,如果需要多个定时器,直接在spring-plan.xml添加 bean和定义定时器类的方法就行,不需要创建多个xml,

关于那个定时器多久执行的   Cron表达式 可以参考://jb51.net/article/138900.htm

有在线生成表达式的网址:http://cron.qqe2.com/

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.5.xsd"

default-lazy-init="false">

class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

withdrawNoAuditTask

class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

0/2 * * * * ?

class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.5.xsd"

default-lazy-init="false">

class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

withdrawNoAuditTask

class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

withdrawNoAuditTask

class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

0/2 * * * * ?

class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

0/2 * * * * ?

class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

3,需要在  applicationContext.xml 中引入  spring-plan.xml    以下代码重点在最下

xmlns:jee="http://springframework.org/schema/jee"

xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop" xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.2.xsd"

default-lazy-init="true">

WEB-INF/jdbc.properties

${jdbc.driverClass}

jdbc:mysql://192.168.14.239:3306/test?useUnicode=true&characterEncoding=utf-8

${jdbc.username}

${jdbc.password}

20

60

20000

true

6000000

cc.royao.mana.auth.mapper.*

  

xmlns:jee="http://springframework.org/schema/jee"

xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop" xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.2.xsd"

default-lazy-init="true">

WEB-INF/jdbc.properties

${jdbc.driverClass}

jdbc:mysql://192.168.14.239:3306/test?useUnicode=true&characterEncoding=utf-8

${jdbc.username}

${jdbc.password}

20

60

20000

true

6000000

${jdbc.driverClass}

jdbc:mysql://192.168.14.239:3306/test?useUnicode=true&characterEncoding=utf-8

${jdbc.username}

${jdbc.password}

20

60

20000

true

6000000

cc.royao.mana.auth.mapper.*

  

总结

以上所述是给大家介绍的使用spring整合Quartz实现—定时器功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue 中filter的多种用法
下一篇:java实现验证码小程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~