Quartz 集成Spring实现简单的任务调度Demo
2017-11-03 22:31
645 查看
代码:
#配置文件
#需要调度的服务层
#任务调度执行类
#简单测试类
#配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="quartzService" class="com.haiyi.service.QuartzService"/> <!-- 配置一个Job --> <bean id="printInfoJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.haiyi.bean.PrintInfoJob"/> <property name="jobDataAsMap"> <map> <entry key="quartzService" value-ref="quartzService"></entry> </map> </property> </bean> <!-- 简单的触发器 --> <bean id="simplePrintInfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"> <ref bean="printInfoJob"/> </property> <property name="startDelay"> <value>6000</value> </property> <property name="repeatInterval"> <value>6000</value> </property> </bean> <!--复杂的触发器(Cron表达式) --> <bean id="MyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="printInfoJob"/> </property> <property name="cronExpression"> <!-- <value>0 0/1 * * * ?</value> --> <value>0/5 * 21,22 * * ?</value> </property> </bean> <!-- spring触发工厂 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="MyTrigger"/> </list> </property> </bean> </beans>
#需要调度的服务层
package com.haiyi.service; import java.text.SimpleDateFormat; import java.util.Date; public class QuartzService { public void Print(){ Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("任务执行时间为: " + sf.format(date)); } }
#任务调度执行类
package com.haiyi.bean; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.stereotype.Component; import com.haiyi.service.QuartzService; @Component(value="printInfoJob") public class PrintInfoJob extends QuartzJobBean{ private QuartzService quartzService; public QuartzService getQuartzService() { return quartzService; } public void setQuartzService(QuartzService quartzService) { this.quartzService = quartzService; } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub this.quartzService.Print(); } }
#简单测试类
package com.haiyi.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.haiyi.service.QuartzService; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("测试开始......"); ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("测试结束......"); } }
相关文章推荐
- Spring与Quartz集成配置实现简单定时任务调度
- Spring与Quartz集成实现定时调度任务的简单使用
- Spring与Quartz集成配置实现简单定时任务调度
- spring整合quartz实现简单的任务调度
- 使用Spring来实现任务计划服务三:集成quartz任务调度框架
- Spring 4.2集成 Quartz Scheduler 2.2.2 任务调度简单实现示例
- Spring整合Quartz实现任务调度
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- 【springboot】 springboot集成quartz实现定时任务
- Spring集成Quartz定时器实现定时作业任务
- 使用Spring + quartz实现定时任务调度
- Quartz任务调度[Spring+Quartz结合]_实现任务的动态添加、修改和删除
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- quartz简单demo(一个简单易用的任务调度开源框架)
- Spring与Quartz的整合实现定时任务调度
- Spring与Quartz的整合实现定时任务调度
- 使用Spring + quartz实现定时任务调度
- spring整合Quartz实现动态任务调度