Spring整合quartz两种方式以及Spring轮询定时使用
2017-05-12 14:54
666 查看
原文地址,转载请注明出处:
http://blog.csdn.net/qq_34021712/article/details/71728002
©王赛超
前提条件
第一种方法:MethodInvokingJobDetailFactoryBean
第一步:编写一个普通的java类,类中随便写几个方法
第一步:编写一个普通的java类继承QuartzJobBean
第一步:写一个类,给Spring管理,类上标注@Service
第二步:在spring.xml上添加头信息
http://blog.csdn.net/qq_34021712/article/details/71728002
©王赛超
前提条件
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.3.RELEASE</version> </dependency>使用方法分为MethodInvokingJobDetailFactoryBean方式和JobDetailBean方式,推荐使用第一种:完整项目下载
第一种方法:MethodInvokingJobDetailFactoryBean
第一步:编写一个普通的java类,类中随便写几个方法
package com.quartz.component; import org.springframework.beans.factory.annotation.Autowired; import com.quartz.Student; import com.quartz.mapper.StudentMapper; public class QuartzAutoTask { @Autowired private StudentMapper studentMapper; public void eat(){ System.out.println("5秒吃饭"); } public void sleep(){ int insert = studentMapper.insert(new Student(null, "张三", 18, "男")); System.out.println(insert); } }第二步:applicationContext-quartz.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--作业的具体实现 --> <bean id="autoTask" class="com.quartz.component.QuartzAutoTask" /> <!-- task --> <!-- 定义了一个任务JobDetail --> <bean id="quartzEat" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 指定要执行的类 --> <property name="targetObject" ref="autoTask" /> <!-- 指定执行的方法 --> <property name="targetMethod" value="eat" /> </bean> <bean id="quartzSleep" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 指定要执行的类 --> <property name="targetObject" ref="autoTask" /> <!-- 指定执行的方法 --> <property name="targetMethod" value="sleep" /> </bean> <!-- 调度定时任务 的触发器--> <!-- 这种配置可以精确几点执行定时任务 --> <bean id="cronQuartzEat" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="quartzEat"></ref> </property> <property name="cronExpression"> <!--每隔5秒触发一次 --> <value>*/5 * * * * ?</value> </property> </bean> <bean id="cronQuartzSleep" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="quartzSleep"></ref> </property> <property name="cronExpression"> <!--每隔10秒触发一次 --> <value>*/10 * * * * ?</value> </property> </bean> <!--启动定时任务的调度器,注意这里的ref bean --> <bean id="schedulerFactoryBean" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronQuartzEat"></ref> <ref bean="cronQuartzSleep"></ref> </list> </property> </bean> </beans>第三步:在web.xml中添加扫描applicationContext-quartz.xml
<!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>第二种方法:JobDetailBean方式
第一步:编写一个普通的java类继承QuartzJobBean
package com.quartz.component; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.QuartzJobBean; import com.quartz.Student; import com.quartz.mapper.StudentMapper; public class QuartzDetail extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("开始执行。。。"); //无法使用注解注入Bean,所以在配置文件中注入spring上下文,然后使用JobExecutionContext获得 ApplicationContext applicationContext = (ApplicationContext)context.getJobDetail().getJobDataMap().get("applicationContext"); StudentMapper studentMapper= (StudentMapper) applicationContext.getBean("studentMapper"); int insert = studentMapper.insert(new Student(null, "李四", 20, "男")); System.out.println(insert); } }第二步:applicationContext-quartz.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 定义JobDetail的Bean --> <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- 指定任务Bean的实现类 --> <property name="jobClass" value="com.quartz.component.QuartzDetail"/> <!-- 指定job的名称 --> <property name="name" value="myJob"/> <!-- 指定job的分组 --> <property name="group" value="jobs"/> <!-- 必须设置为true,如果为false 当没有活动的触发器与之关联时会在调度器中删除该任务 --> <property name="durability" value="true"/> <!-- 测试是无法注入spring的Bean 所以注入spring上下文 --> <property name="applicationContextJobDataKey" value="applicationContext"/> </bean> <!-- 定义触发器的Bean --> <bean id="quartzDetail" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 指定jobDetail --> <property name="jobDetail" ref="jobDetail"/> <!-- 指定Cron表达式:每10秒执行一次 --> <property name="cronExpression" value="*/10 * * * * ?"/> </bean> <!-- 执行实际的调度器--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="quartzDetail"/> </list> </property> </bean> </beans>第三步:在web.xml中添加扫描applicationContext-quartz.xml
<!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>Spring轮询的配置使用
第一步:写一个类,给Spring管理,类上标注@Service
package com.quartz.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class StudentServiceImpl { @Scheduled(cron="*/1 * * * * ?") public void execute(){ System.out.println("执行了。。。。。。"); } }
第二步:在spring.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:mvc="http://www.springframework.org/schema/mvc" 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:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">[/code]第三步:在spring.xml中添加以下注解<!-- 任务发送 --> <task:annotation-driven />
相关文章推荐
- spring实现定时任务的两种方式之Spring 3整合Quartz 2实现定时任务
- Spring中使用quartz执行定时任务的两种方式
- Spring中使用quartz执行定时任务的两种方式
- SpringBoot之旅 -- 定时任务两种(Spring Schedule 与 Quartz 整合 )实现
- SpringBoot定时任务两种(Spring Schedule 与 Quartz 整合 )实现方法
- spring boot 集成quartz 2.0 实现前端动态配置(获取spring上下文)的两种方式,启动数据库中已开启定时任务
- Spring 整合Quartz两种方式(JobDetailBean和MethodInvokingJobDetailFactoryBean方式)
- WebService 的原始使用方式以及同spring 整合后的使用
- springboot动态配置定时任务2种方式,整合Quartz多线程并发执行多个定时任务配置
- SpringBoot之旅 -- 定时任务两种(Spring Schedule 与 Quartz 整合 )实现
- Quartz与Spring的整合使用——创建JobDetail的方式及Trigger
- 使用Spring(17)Spring中的定时调度(Scheduling)--传统方式创建Quartz任务
- 两种方式实现java定时器,使用quartz定时器框架和java自带Timer定时器,编写定时任务
- Spring 整合Quartz两种方式比较(JobDetailBean和MethodInvokingJobDetailFactoryBean方式)
- spring集成定时任务框架 Quartz的两种方式
- Spring中使用Quartz的两种方式
- spring和quartz整合实现定时任务(配置文件配置方式
- 使用Spring整合Quartz轻松完成定时任务
- spring boot 集成quartz 2.0 实现前端动态配置(获取spring上下文)的两种方式,启动数据库中已开启定时任务
- 使用System.Threading的Timer&Quartz.net两种方式实现定时执行任务,防止IIS释放timer对象