Spring整合Quartz开发代码实例

网友投稿 397 2023-06-09

Spring整合Quartz开发代码实例

Spring整合Quartz开发代码实例

我们使用Spring整合Quartz开发,本实例采用数据库模式的demo。

xml文件配置如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation="

http://springframework.org/schema/beans https://springframework.org/schema/beans/spring-beans.xsd

http://springzeyzyagframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop https://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd">

&zeyzyaglt;property name="acquireRetryAttempts" value="10"/>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation="

http://springframework.org/schema/beans https://springframework.org/schema/beans/spring-beans.xsd

http://springzeyzyagframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop https://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd">

&zeyzyaglt;property name="acquireRetryAttempts" value="10"/>

public class SimpleJob extends QuartzJobBean {

@Override

protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

System.out.println(new Date()+"执行SimpleJob");

}

}

public class ApplicationContextTest {

public static Scheduler scheduler;

public static void main(String[] args) throws Exception {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext-trigger.xml");

scheduler = (Scheduler) applicationContext.getBean("quartzScheduler");

//SimpleJob simpleJob = new SimpleJob();

scheduler.start();

//从数据库中获取相应的job及调度信息

//JobDetail jobDetail = scheduler.getJobDetail(new JobKey("trigger1", "trigger1"));

//resumeJob(jobDetail.getKey().getName(), jobDetail.getKey().getGroup());

//添加job执行

addJob("trigger1", "trigger1", "job1", "job2", "0/20 * * * * ?", SimpleJob.class, new HashMap<>());

Thread.sleep(60 * 1000);

//重新设置调度时间

System.out.println("重新设置调度时间");

rescheduleJob("trigger1","trigger1","0/10 * * * * ?");

Thread.sleep(60 * 1000);

//暂停调度

System.out.println("暂停调度");

pauseJob("trigger1","trigger1");

Thread.sleep(60 * 1000);

System.out.println("恢复调度");

resumeJob("trigger1","trigger1");

Thread.sleep(60 * 1000);

System.out.println("删除调度");

removeJob("trigger1","trigger1");

Thread.sleep(60 * 1000);

System.out.println(scheduler);

}

/**

* 添加job执行

*

* @param triggerKeyName

* @param triggerKeyGroup

* @param jobName

* @param jobGroup

* @param cronExpression

* @param jobClass

* @param jobData

* @return

* @throws Exception

*/

public static boolean addJob(String triggerKeyName, String triggerKeyGroup, String jobName, String jobGroup, String cronExpression,

Class extends Job> jobClass, Map jobData) throws Exception {

JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(triggerKeyName, triggerKeyGroup).build();

Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).withIdentity(triggerKeyName, triggerKeyGroup).build();

if (jobData != null && jobData.size() > 0) {

JobDataMap jobDataMap = jobDetail.getJobDataMap();

jobDataMap.putAll(jobData); // JobExecutionContext context.getMergedJobDataMap().get("mailGuid");

}

scheduler.scheduleJob(jobDetail, trigger);

// if (!scheduler.isShutdown()) {

// scheduler.start();

// }

return true;

}

/**

* 重新设置job执行

* @param triggerKeyName

* @param triggerKeyGroup

* @param cronExpression

* @return

* @throws SchedulerException

*/

public static boolean rescheduleJob(String triggerKeyName, String triggerKeyGroup, String cronExpression) throws SchedulerException {

TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup);

if (scheduler.checkExists(triggerKey)) {

Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).withIdentity(triggerKey).build();

scheduler.rescheduleJob(triggerKey, trigger);

}

return true;

}

/**

* 删除job

* @param triggerKeyName

* @param triggerKeyGroup

* @return

* @throws SchedulerException

*/

public static boolean removeJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException {

// TriggerKey : name + group

TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup);

boolean result = false;

if (scheduler.checkExists(triggerKey)) {

result = scheduler.unscheduleJob(triggerKey);

}

return result;

}

/**

* 暂停job

* @param triggerKeyName

* @param triggerKeyGroup

* @return

* @throws SchedulerException

*/

public static boolean pauseJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException {

// TriggerKey : name + group

TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup);

boolean result = false;

if (scheduler.checkExists(triggerKey)) {

scheduler.pauseTrigger(triggerKey);

result = true;

} else {

}

return result;

}

/**

* 重启job

* @param triggerKeyName

* @param triggerKeyGroup

* @return

* @throws SchedulerException

*/

public static boolean resumeJob(String triggerKeyName, String triggerKeyGroup) throws SchedulerException {

TriggerKey triggerKey = TriggerKey.triggerKey(triggerKeyName, triggerKeyGroup);

boolean result = false;

if (scheduler.checkExists(triggerKey)) {

scheduler.resumeTrigger(triggerKey);

result = true;

} else {

}

return result;

}

}

quart.properties正常配置信息,然后点击运行即可。

本实例中当运行的任务在暂停的情况下,一旦重新恢复,会将暂停期间的任务运行如图:

源码链接:  https://github.com/albert-liu435/springquartz

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:云原生和原生容器的区别深度解析
下一篇:云原生和容器化:现代软件开发的必备技术
相关文章

 发表评论

暂时没有评论,来抢沙发吧~