您的位置:首页 > 其它

Quartz与Srping结合简单示例

2016-09-20 16:06 627 查看
Job类

package net.xsbiz.common;

public class MakeHtml {
// 调用的方法
public void execute() throws InterruptedException{
// 需要做的事情
System.out.print(new Date());
}

}


Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd"> 
<!-- Timer schedule -->

<!--要调度的对象-->
<bean id="jobBean" class="net.xsbiz.common.MakeHtml" />

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="jobBean" />
<!-- 执行"jobBean"中的execute方法 -->
<property name="targetMethod" value="execute" />
<!--将并发设置为false-->
<property name="concurrent" value="true" />
</bean>

<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 定义 Job 何时执行 -->
<property name="jobDetail" ref="jobDetail" />

<!--表达式,我的是每 30 执行一次-->
<property name="cronExpression" value="0/1 * * * * ?" /><!-- “ 0 0 12 * * ? ”会在每天中午 12 点触发 执行;“ 0 15 10 ? * 6L ”会在每个月的最后一个星期五的早上 10:15 触发 Job 执行 -->
</bean>

<!--  总管理类如果将lazy-init='false'那么容器启动就会执行调度程序   -->
<bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" >
<property name="autoStartup" value="true"></property>

<property name="triggers">
<list>
<!--作业调度器,list下可加入其他的调度器-->
<ref bean="trigger" />
</list>
</property>
</bean>

</beans>


测试结果:




参考资料:


QuartZ Cron表达式

源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: