Spring整合Quartz开发代码实例

 更新时间:2020年04月24日 14:49:55   作者:海之浪子  
这篇文章主要介绍了Spring整合Quartz开发代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

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 https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util.xsd">


  <!--加载数据库连接的配置文件-->
  <!--<context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
  <!-- c3p0:数据源配置 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/quartz?Unicode=true&amp;characterEncoding=UTF-8"/>
    <property name="user" value="root"/>
    <property name="password" value="root"/>
    <property name="initialPoolSize" value="3"/>
    <property name="minPoolSize" value="2"/>
    <property name="maxPoolSize" value="10"/>
    <property name="maxIdleTime" value="60"/>
    <property name="acquireRetryDelay" value="1000"/>
    <property name="acquireRetryAttempts" value="10"/>
    <property name="preferredTestQuery" value="SELECT 1"/>
  </bean>
  
  <bean id="quartzScheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="quartz.properties"></property>
    <!--  <property name="triggers"></property>-->
  </bean>


</beans>
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<String, Object> 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 一文解决pom.xml报错Dependency "xxx" not found的问题

    一文解决pom.xml报错Dependency "xxx" not f

    我们在使用maven进行jar包管理时有时会遇到pom.xml中报错Dependency “XXX” not found,所以在本文中将给大家介绍一下pom.xml报错Dependency "xxx" not found的解决方案,需要的朋友可以参考下
    2024-01-01
  • 解决SpringBoot返回结果如果为null或空值不显示处理问题

    解决SpringBoot返回结果如果为null或空值不显示处理问题

    这篇文章主要介绍了解决SpringBoot返回结果如果为null或空值不显示处理问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • SpringBoot中的定时任务和异步调用详解

    SpringBoot中的定时任务和异步调用详解

    这篇文章主要介绍了SpringBoot中的定时任务和异步调用详解,SpringBoot 定时任务是一种在SpringBoot应用中自动执行任务的机制,通过使用Spring框架提供的@Scheduled注解,我们可以轻松地创建定时任务,需要的朋友可以参考下
    2023-10-10
  • mybatis-plus 通用字段自动化(如逻辑删除和更新时间等)

    mybatis-plus 通用字段自动化(如逻辑删除和更新时间等)

    这篇文章主要介绍了mybatis-plus 通用字段自动化(如逻辑删除和更新时间等),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • 例题详解Java dfs与记忆化搜索和分治递归算法的使用

    例题详解Java dfs与记忆化搜索和分治递归算法的使用

    递归指函数调用自身。常用的递归算法有dfs(深度优先搜索)、记忆化搜索和分治,接下来将用几个算法题来带你熟练掌握它
    2022-04-04
  • 【MyBatis源码全面解析】MyBatis一二级缓存介绍

    【MyBatis源码全面解析】MyBatis一二级缓存介绍

    下面小编就为大家带来一篇【MyBatis源码全面解析】MyBatis一二级缓存介绍。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Java 逻辑控制全面详解

    Java 逻辑控制全面详解

    程序的逻辑主要分为三种结构:顺序结构、分支结构、循环结构,顺序结构的所有的代码都是从前向后执行的。有些时候顺序是由“{}”为界限的,下文将全面详细的介绍
    2021-10-10
  • JAVA 根据设置的概率生成随机数的方法

    JAVA 根据设置的概率生成随机数的方法

    本篇文章主要介绍了JAVA 根据设置的概率生成随机数的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Java多线程 自定义线程池详情

    Java多线程 自定义线程池详情

    这篇文章主要介绍了Java多线程 自定义线程池,文章主要是学习代码,没有过多解析,需要的朋友可以参考一下文章的具体内容
    2021-10-10
  • java读取http请求中的body实例代码

    java读取http请求中的body实例代码

    下面小编就为大家带来一篇java读取http请求中的body实例代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09

最新评论