Spring 集成Quartz Scheduler 定时调度任务 Example
2016-01-07 09:10
435 查看
<---------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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="test.spring.quartz" /> <!--配置 task.printInfo 将要执行的业务执行逻辑 --> <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="firstTask" /> <property name="targetMethod" value="printInfo" /> </bean> <!-- 配置作业类complexJobDetail --> <bean name="complexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- jobClass属性即我们在java代码中定义的任务实现类 --> <property name="jobClass" value="test.spring.quartz.ScheduledJob" /> <!-- jobDataAsMap属性即该任务类中需要注入的属性值 --> <property name="jobDataMap"> <map> <entry key="secondTask" value-ref="secondTask" /> </map> </property> <!-- 任务完成之后是否依然保留到数据库,默认false --> <property name="durability" value="true" /> <!-- 任务的名称 --> <!-- <property name="name"></property> --> <!-- 任务所在组 --> <!-- <property name="group"></property> --> </bean> <!-- 简单的触发器,通过它我们可以定义触发的时间,并选择性的设定重复的次数和间隔时间 | 只支持按照一定频度调用任务,如每隔2秒运行一次 --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="simpleJobDetail" /> <property name="startDelay" value="1000" /> <!-- 调度工厂实例化后,经过1秒开始执行调度 --> <property name="repeatInterval" value="2000" /> <!-- 每2秒调度一次 --> </bean> <!-- 这个触发器的功能非常强大,而且非常灵活,需要掌握有关的Cron表达式知识 到指定时间运行一次 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 触发器名称 --> <!-- <property name="name"></property> --> <!-- 触发器组的名称 --> <!-- <property name="group"></property> --> <property name="jobDetail" ref="complexJobDetail" /> <!-- 规则表达式 --> <property name="cronExpression" value="0/5 * * ? * SAT-SUN" /><!--每5秒运行一次 --> <!-- 开始时间,默认当前时间 --> <!-- <property name="startTime"></property> --> <!-- 延迟时间 --> <!-- <property name="startDelay"></property> --> <!-- 时区 --> <!-- <property name="timeZone"></property> --> </bean> <!-- 配置调度工厂 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="simpleJobDetail" /> <ref bean="complexJobDetail" /> </list> </property> <property name="triggers"> <list> <ref bean="simpleTrigger" /> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>
/** * @Title: MyBean.java * @Package test.spring.quartz.scheduling * @Description: * @author L * @date 2015年10月11日 上午9:34:15 * @version V1.0 */ package test.spring.quartz.scheduling; import org.springframework.stereotype.Component; /** * @ClassName: outTask * @Description: * @author L * @date 2015年10月11日 上午9:34:15 * */ @Component("firstTask") public class FirstTask { public void printInfo() { System.out.println("隔2秒运行一次"); } }
/** * @Title: AnotherBean.java * @Package test.spring.quartz.scheduling * @Description: * @author L * @date 2015年10月11日 上午9:31:14 * @version V1.0 */ package test.spring.quartz.scheduling; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; @Component("secondTask") public class SecondTask { /** * @Title: printAnotherMessage * @Description: */ public void printSecondTaskInfo() { System.out.println("每5秒运行一次"); } @SuppressWarnings("unused") public static void main(String args[]) { @SuppressWarnings("resource") AbstractApplicationContext context = new ClassPathXmlApplicationContext("quartz-context.xml"); } }
/** * @Title: ScheduledJob.java * @Package test.spring.quartz * @Description: * @author L * @date 2015年10月11日 上午9:27:11 * @version V1.0 */ package test.spring.quartz; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import test.spring.quartz.scheduling.SecondTask; public class ScheduledJob extends QuartzJobBean { private SecondTask secondTask; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { secondTask.printSecondTaskInfo();// 要调度的具体任务 } /** * @param secondTask * the secondTask to set */ public void setSecondTask(SecondTask secondTask) { this.secondTask = secondTask; } }
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式
- 解析Java的Spring框架的BeanPostProcessor发布处理器
- Java开发框架spring实现自定义缓存标签
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- spring boot实战之内嵌容器tomcat配置
- 浅析Java的Spring框架中IOC容器容器的应用
- 基于Spring框架的Shiro配置方法