spring使用quartz调度

本文介绍如何使用Spring结合Quartz实现任务调度,包括配置JobDetail、Trigger,并利用SchedulerFactoryBean进行调度。同时展示了如何使用MethodInvokingJobDetailFactoryBean简化任务定义。

 使用OpenSymphony Quartz 调度器
<!--
JobDetail 对象保存运行一个任务所需的全部信息
Spring提供一个叫作JobDetailBean的类让JobDetail能对一些有意义的初始值进行初始化
-->
    <bean id="myJobDetail"
       class="org.springframework.scheduling.quartz.JobDetailBean">
       <property name="jobClass"
           value="example.timertask.TimeTaskUsingQuartz" />
   </bean>
<!--
注意:使用name和group属性,你可以分别修改job在哪一个组下运行和使用什么名称。默认情况下,job的名称等于job detail bean的名称(在上面的例子中为myJobDetail)。
--> 
  <!--
Quartz自带一些可供使用的triggers
Spring提供两个子类triggers,分别为CronTriggerBean和SimpleTriggerBean -->
<!-- 使用SimpleTriggerBean, CronTriggerBean来包装任务 -->
    <bean id="simpleTrigger"
    class="org.springframework.scheduling.quartz.SimpleTriggerBean">
       <property name="jobDetail" ref="myJobDetail" />
<!-- 延时1m 执行任务 -->
       <property name="startDelay" value="1000" />
<!-- 任务执行周期 3m -->
       <property name="repeatInterval" value="3000" />
       </bean>
    <bean id="cronTrigger"
       class="org.springframework.scheduling.quartz.CronTriggerBean">
       <property name="jobDetail" ref="myJobDetail" />
<!-- cronExpression表达式请参见附录
      每天早上6点钟运行
-->
       <property name="cronExpression" value="0 0 6 * * ?" />
    </bean>
<!-- Triggers也需要被调度
Spring提供SchedulerFactoryBean来暴露一些属性来设置triggers SchedulerFactoryBean负责调度那些实际的triggers
更多的属性你可以通过SchedulerFactoryBean来设置
例如job details使用的Calendars, 用来订制Quartz的一些属性以及其它相关信息你可以查阅相应的JavaDOC(http://www.springframework.org/docs/api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html)来了解进一步的信息
-->
<!-- 使用SchedulerFactoryBean来实现任务 -->
    <bean id="scheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
       <property name="triggers" ref="cronTrigger" />
    </bean>
示例代码如下
package example.timertask;

import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

publicclass TimeTaskUsingQuartz extends QuartzJobBean {
    protectedvoid executeInternal(JobExecutionContext ctx)
           throws JobExecutionException {
       System.out.println("现在时间: " + new Date());
    }

}
applicationContext.xml设置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!—
JobDetail 对象保存运行一个任务所需的全部信息
-->
    <bean id="myJobDetail"
       class="org.springframework.scheduling.quartz.JobDetailBean">
       <property name="jobClass"
           value="example.timertask.TimeTaskUsingQuartz" />
    </bean>
<!-- 使用SimpleTriggerBean, CronTriggerBean来包装任务 -->
    <bean id="simpleTrigger"
    class="org.springframework.scheduling.quartz.SimpleTriggerBean">
       <property name="jobDetail" ref="myJobDetail" />
<!-- 延时1m 执行任务 -->
       <property name="startDelay" value="10000" />
<!-- 任务执行周期 3m -->
       <property name="repeatInterval" value="50000" />
    </bean>
    <bean id="cronTrigger"
       class="org.springframework.scheduling.quartz.CronTriggerBean">
       <property name="jobDetail" ref="myJobDetail" />
<!-- 每天早上6点钟运行 -->
       <property name="cronExpression" value="0 0 6 * * ?" />
    </bean>
<!-- 现在我们创建了两个triggers,其中一个开始延迟10秒以后每50秒运行一次,另一个每天早上6点钟运行。我们需要创建一个SchedulerFactoryBean来最终实现上述的一切
-->
    <bean id="scheduler"
    class="org.springframework.scheduling.quartz.
SchedulerFactoryBean">
       <property name="triggers">
             <list>
                  <ref name="simpleTrigger" />
                  <ref name="cronTrigger" />
</list>
</property>
    </bean>

</beans>
使用 MethodInvokingJobDetailFactoryBean,调用特定对象上的一个方法即可实现任务调度.通常情况下,你只需要调用特定对象上的一个方法即可实现任务调度。你可以使用MethodInvokingJobDetailFactoryBean准确的做到这一点:
示例代码如下
package example;

import java.util.Date;

publicclass BusinessObject {
    publicvoid doIt() {
       System.out.println("现在时间: " + new Date());
    }
}
applicationContext.xml设置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="businessObjectExample" class="example.BusinessObject" />
    <!--
       使用MethodInvokingJobDetailFactoryBean你不需要创建只有一行代码且只调用一个方法的job,
        你只需要创建真实的业务对象来包装具体的细节的对象
        默认情况下,Quartz Jobs是无状态的,可能导致jobs之间互相的影响。
       如果你为相同的JobDetail指定两个Trigger, 很可能当第一个job完成之前,第二个job就开始了。
       如果JobDetail对象实现了Stateful接口,就不会发生这样的事情。第二个job将不会在第一个job完成之前开始。
       为了使得jobs不并发运行,设置MethodInvokingJobDetailFactoryBean中的concurrent标记为false。
-->
    <bean id="myjobDetail"
class="org.springframework.scheduling.quartz.
MethodInvokingJobDetailFactoryBean" >
       <property name="targetObject" ref="businessObjectExample" />
       <property name="targetMethod" value="doIt" />
       <property name="concurrent" value="false" />
    </bean>
<!-- 注意:默认情况下,jobs在并行的方式下运行 -->
附录:关于cronExpression的介绍:
字段   允许值   允许的特殊字符
秒   0-59   , - * /
分   0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - * ? / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - * ? / L C #
年(可选)   留空, 1970-2099   , - * /

 
如上面的表达式所示:
详细说明如下:
The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。
The '?' character is allowed for the mother day-of-month and mother day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。
The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".
“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。
The ',' character is used to specify additional values. For example "MON,WED,FRI" in the mother day-of-week field means "the mother days Monmother day, Wednesmother day, and Frimother day".
“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.

【电能质量扰动】基于ML和DWT的电能质量扰动分类方法研究(Matlab实现)内容概要:本文研究了一种基于机器学习(ML)和离散小波变换(DWT)的电能质量扰动分类方法,并提供了Matlab实现方案。首先利用DWT对电能质量信号进行多尺度分解,提取信号的时频域特征,有效捕捉电压暂降、暂升、中断、谐波、闪变等常见扰动的关键信息;随后结合机器学习分类器(如SVM、BP神经网络等)对提取的特征进行训练与分类,实现对不同类型扰动的自动识别与准确区分。该方法充分发挥DWT在信号去噪与特征提取方面的优势,结合ML强大的模式识别能力,提升了分类精度与鲁棒性,具有较强的实用价值。; 适合人群:电气工程、自动化、电力系统及其自动化等相关专业的研究生、科研人员及从事电能质量监测与分析的工程技术人员;具备一定的信号处理基础和Matlab编程能力者更佳。; 使用场景及目标:①应用于智能电网中的电能质量在线监测系统,实现扰动类型的自动识别;②作为高校或科研机构在信号处理、模式识别、电力系统分析等课程的教学案例或科研实验平台;③目标是提高电能质量扰动分类的准确性与效率,为后续的电能治理与设备保护提供决策依据。; 阅读建议:建议读者结合Matlab代码深入理解DWT的实现过程与特征提取步骤,重点关注小波基选择、分解层数设定及特征向量构造对分类性能的影响,并尝试对比不同机器学习模型的分类效果,以全面掌握该方法的核心技术要点。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值