Springmvc中配置Quartz,实现任务实时调度
2017-08-08 15:49
267 查看
第一次接触Quartz,做个记录
简单介绍:
Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。Quartz 允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz 的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业。虽然可以通过属性文件(在属性文件中可以指定 JDBC 事务的数据源、全局作业和/或触发器侦听器、插件、线程池,以及更多)配置 Quartz,但它根本没有与应用程序服务器的上下文或引用集成在一起。结果就是作业不能访问
Web 服务器的内部函数;例如,在使用 WebSphere 应用服务器时,由 Quartz 调度的作业并不能影响服务器的动态缓存和数据源。
<?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:util="http://www.springframework.org/schema/util"
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- <context:property-placeholder location="classpath:job.properties" ignore-unresolvable="true"/> -->
<description>Quartz计划任务调度配置</description>
<!-- 数据分析定时下发工作类 -->
<bean id="MyFunction1" class="com.nis.common.MyFunction"/>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="MyFunction1"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
<property name="concurrent" value="false"/>
</bean>
<!-- 定义触发时间和触发的bean -->
<bean id="jobTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobTask"/>
</property>
<!-- cron表达式 每2秒触发一次 -->
<property name="cronExpression">
<value>*/2 * * * * ?</value>
</property>
</bean>
<!-- 第二个任务 -->
<bean id="MyFunction2" class="com.nis.common.MyFunction"/>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="MyFunction2"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work2</value>
</property>
<property name="concurrent" value="false"/>
</bean>
<!-- 定义触发时间 -->
<bean id="jobTaskTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobTask2"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>*/1 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="taskManager" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
947c
<ref bean="jobTaskTrigger"/>
<ref bean="jobTaskTrigger2"/>
</list>
</property>
</bean>
</beans> 简单的测试类
public class MyFunction {
public void work(){
System.out.println("Function2 work");
}
public void work2(){
System.out.println("Function2 work2");
}
}
简单介绍:
Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。Quartz 允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz 的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业。虽然可以通过属性文件(在属性文件中可以指定 JDBC 事务的数据源、全局作业和/或触发器侦听器、插件、线程池,以及更多)配置 Quartz,但它根本没有与应用程序服务器的上下文或引用集成在一起。结果就是作业不能访问
Web 服务器的内部函数;例如,在使用 WebSphere 应用服务器时,由 Quartz 调度的作业并不能影响服务器的动态缓存和数据源。
导入所需的包
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
在xml中配置Quartz
配置多个定时器<?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:util="http://www.springframework.org/schema/util"
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- <context:property-placeholder location="classpath:job.properties" ignore-unresolvable="true"/> -->
<description>Quartz计划任务调度配置</description>
<!-- 数据分析定时下发工作类 -->
<bean id="MyFunction1" class="com.nis.common.MyFunction"/>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="MyFunction1"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
<property name="concurrent" value="false"/>
</bean>
<!-- 定义触发时间和触发的bean -->
<bean id="jobTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobTask"/>
</property>
<!-- cron表达式 每2秒触发一次 -->
<property name="cronExpression">
<value>*/2 * * * * ?</value>
</property>
</bean>
<!-- 第二个任务 -->
<bean id="MyFunction2" class="com.nis.common.MyFunction"/>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="MyFunction2"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work2</value>
</property>
<property name="concurrent" value="false"/>
</bean>
<!-- 定义触发时间 -->
<bean id="jobTaskTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobTask2"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>*/1 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="taskManager" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
947c
<ref bean="jobTaskTrigger"/>
<ref bean="jobTaskTrigger2"/>
</list>
</property>
</bean>
</beans> 简单的测试类
public class MyFunction {
public void work(){
System.out.println("Function2 work");
}
public void work2(){
System.out.println("Function2 work2");
}
}
相关文章推荐
- Springmvc中配置Quartz使用,实现任务实时调度。
- Spring与Quartz集成配置实现简单定时任务调度
- Spring配置Quartz实现定时调度任务
- Quartz.NET 2.0 学习笔记(3) :通过配置文件实现任务调度
- java计划任务调度框架quartz结合spring实现调度的配置实例代码分享
- SpringMVC整合Quartz实现定时任务和Crontrigger时间配置和版本问题
- Quartz实现定时任务的配置方法(纯java作业调度框架)
- java计划任务调度框架quartz结合spring实现调度的配置实例代码分享
- Quartz.NET 2.0 学习笔记(3) :通过配置文件实现任务调度
- springmvc中实现quartz定时任务(每分钟的第3秒执行任务调度方法)
- Quartz.NET 2.0 学习笔记(3) :通过配置文件实现任务调度
- springmvc中实现quartz定时任务(每分钟的第3秒执行任务调度方法)
- Quartz.NET 2.0 学习笔记(3) :通过配置文件实现任务调度
- quartz任务调度框架实现任务定时执行,不传参的配置(一)
- java计划任务调度框架quartz结合spring实现调度的配置实例代码
- Quartz.NET 通过配置文件实现任务调度
- java计划任务调度框架quartz结合spring实现调度的配置实例代码分享
- springMVC quartz任务调度框架配置(xml)
- Spring与Quartz集成配置实现简单定时任务调度
- Spring整合quartz框架实现任务定时调度