spring 整合maven 定时任务调度
来源:互联网 发布:2016淘宝客pid怎么看 编辑:程序博客网 时间:2024/05/21 15:46
这里特别对版本作一下说明,是因为spring和quartz的整合对版本是有要求的。
spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持quartz 2.x,不然会出错。
至于原因,则是spring对于quartz的支持实现,org.springframework.scheduling.quartz.CronTriggerBean继承了org.quartz.CronTrigger,在quartz1.x系列中org.quartz.CronTrigger是个类,而在quartz2.x系列中org.quartz.CronTrigger变成了接口,从而造成无法用spring的方式配置quartz的触发器(trigger)。
1、spring-quartz.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:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"default-lazy-init="false"><!-- 定义目标bean和bean中的方法 --> <!-- =====================日常任务job========================== --> <bean id="MyTask" class="com.jn.commons.quartz.AutoTask"> </bean> <bean id="MyTask1" class="com.jn.commons.quartz.AutoTask"> </bean> <bean id="MyTaskMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="MyTask"/> </property> <property name="targetMethod"> <!-- 要执行的方法名称 --> <value>execute</value> </property> </bean> <bean id="MyTaskMethod1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="MyTask1"/> </property> <property name="targetMethod"> <!-- 要执行的方法名称 --> <value>auto</value> </property> </bean> <!-- ======================== 调度触发器 ======================== --> <bean id="DailyTaskCronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="MyTaskMethod"></property> <!-- 每分钟的第一秒触发 --> <property name="cronExpression" value="1 * * * * ?"></property> <!-- 每小时的第一分钟触发 --> <!-- <property name="cronExpression" value="0 1 * * * ?"></property> --> </bean> <bean id="DailyTaskCronTriggerBean1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="MyTaskMethod1"></property> <!-- 每分钟的第一秒触发 --> <property name="cronExpression" value="1 * * * * ?"></property> <!-- 每小时的第一分钟触发 --> <!-- <property name="cronExpression" value="0 1 * * * ?"></property> --> </bean> <!-- ======================== 调度工厂 ======================== --> <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="DailyTaskCronTriggerBean"/> <ref bean="DailyTaskCronTriggerBean1"/> </list> </property> </bean> </beans>
2、在sping主配置文件中引入
<import resource="classpath:spring/spring-quartz.xml" />3、在pom.xm文件中引入需要下载的jar
<dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>1.8.5</version></dependency>
4、编写测试java文件
package com.jn.commons.quartz;import java.lang.management.ManagementFactory;import java.lang.management.MemoryMXBean;import java.lang.management.MemoryUsage;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.logging.Logger;/** * ClassName: AutoTask <br/> * Function: TODO ADD FUNCTION. <br/> * Reason: TODO ADD REASON(可选). <br/> * date: 2016-5-23 上午10:13:40 <br/> * * @author weber * @version * @since JDK 1.6 */public class AutoTask {private static Logger logger = Logger.getLogger(AutoTask.class.getName());protected void execute() throws ParseException {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String date = format.format(new Date());System.out.println("这是第一个定时任务:" + date);logger.info("任务1:每分钟定时获取当前系统时间");}public void auto() {MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage(); //椎内存使用情况long totalMemorySize = memoryUsage.getInit(); //初始的总内存long maxMemorySize = memoryUsage.getMax(); //最大可用内存long usedMemorySize = memoryUsage.getUsed(); //已使用的内存 System.out.println("这是第二个定时任务"+"\r"+"椎内存使用情况:"+"1.初始的总内存:"+totalMemorySize+"2.最大可用内存:"+maxMemorySize+"3.已使用的内存:"+usedMemorySize); logger.info("任务2:后台监控椎内存使用情况");}}
5、测试结果
1 0
- spring 整合maven 定时任务调度
- Spring整合TimerTask实现定时任务调度
- Spring整合Quartz实现定时任务调度
- spring整合quartz实现定时任务调度
- Spring整合Quartz实现定时任务调度
- Spring整合Quartz实现定时任务调度
- Spring整合TimerTask实现定时任务调度
- Spring整合Quartz实现定时任务调度
- Spring 定时调度任务
- Spring整合定时任务调度框架Quartz实战
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- UVA 12656 Almost Palindrome
- linux驱动---用I/O命令访问PCI总线设备配置空间
- css reset
- 引水工程 河南省ACM2015年省赛D 题(最小生成树)
- LeetCode 349 Intersection of Two Arrays
- spring 整合maven 定时任务调度
- Android 拍照、选择图片并裁剪
- 深度卷积网络CNN与图像语义分割
- css定位
- VirtualBox 官网下载+安装(win7)
- VMware12+Ubuntu16.04 安装 以及全屏的实现
- Android N的介绍
- c++ primer(第五版)学习笔记及习题答案代码版(第十一章)关联容器
- NIO buffer