Spring 集成Quartz Scheduler 定时调度任务 Example

8 篇文章 0 订阅
1 篇文章 0 订阅

<---------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;



<========================================================================================================>




/**

 * @ClassName: AnotherBean

 * @Description:

 * @author L

 * @date 2015年10月11日 上午9:31:14

 * 

 */

@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;




<========================================================================================================>



/**

 * @ClassName: ScheduledJob

 * @Description:

 * @author L

 * @date 2015年10月11日 上午9:27:11

 * 

 */

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;

}



}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了您的问题。在Spring Cloud集成Quartz时,需要进行以下数据库配置: 1. 首先,在您的Spring Boot项目中添加Quartz的依赖项。您可以在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 2. 接下来,在application.properties文件中配置Quartz的数据库属性,例如: ``` spring.quartz.job-store-type=jdbc spring.quartz.jdbc.initialize-schema=always spring.datasource.url=jdbc:mysql://localhost:3306/quartz spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 其中,spring.quartz.job-store-type设置为jdbc,表示Quartz将使用数据库存储作业和触发器,spring.quartz.jdbc.initialize-schema设置为always表示每次启动应用程序时都会初始化Quartz数据库,spring.datasource.url、spring.datasource.username、spring.datasource.password和spring.datasource.driver-class-name为您的数据库连接属性。 3. 最后,定义您的定时任务,例如: ``` @Configuration public class QuartzConfiguration { @Bean public JobDetail myJobDetail() { return JobBuilder.newJob(MyJob.class) .withIdentity("myJob") .storeDurably() .build(); } @Bean public Trigger myTrigger() { return TriggerBuilder.newTrigger() .forJob(myJobDetail()) .withIdentity("myTrigger") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); } } @Component public class MyJob implements Job { @Override public void execute(JobExecutionContext context) { // 任务逻辑 } } ``` 其中,定时任务使用Cron表达式配置,该表达式将在每分钟的第0秒开始,每5秒执行一次任务。 以上就是Spring Cloud集成Quartz数据库配置定时任务的方法。希望能够帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值