欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring整合Quartz開發(fā)代碼實(shí)例

 更新時(shí)間:2020年04月24日 14:49:55   作者:海之浪子  
這篇文章主要介紹了Spring整合Quartz開發(fā)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

我們使用Spring整合Quartz開發(fā),本實(shí)例采用數(shù)據(jù)庫(kù)模式的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">


  <!--加載數(shù)據(jù)庫(kù)連接的配置文件-->
  <!--<context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
  <!-- c3p0:數(shù)據(jù)源配置 -->
  <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()+"執(zhí)行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();
  
    //從數(shù)據(jù)庫(kù)中獲取相應(yīng)的job及調(diào)度信息
    //JobDetail jobDetail = scheduler.getJobDetail(new JobKey("trigger1", "trigger1"));
    //resumeJob(jobDetail.getKey().getName(), jobDetail.getKey().getGroup());
    //添加job執(zhí)行
    addJob("trigger1", "trigger1", "job1", "job2", "0/20 * * * * ?", SimpleJob.class, new HashMap<>());
    Thread.sleep(60 * 1000);
    //重新設(shè)置調(diào)度時(shí)間
    System.out.println("重新設(shè)置調(diào)度時(shí)間");
    rescheduleJob("trigger1","trigger1","0/10 * * * * ?");
  
    Thread.sleep(60 * 1000);
    //暫停調(diào)度
    System.out.println("暫停調(diào)度");
    pauseJob("trigger1","trigger1");
  
    Thread.sleep(60 * 1000);
    System.out.println("恢復(fù)調(diào)度");
    resumeJob("trigger1","trigger1");
  
    Thread.sleep(60 * 1000);
    System.out.println("刪除調(diào)度");
    removeJob("trigger1","trigger1");
    Thread.sleep(60 * 1000);
  
    System.out.println(scheduler);
  }
  
  /**
   * 添加job執(zhí)行
   *
   * @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;


  }
  
  /**
   * 重新設(shè)置job執(zhí)行
   * @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正常配置信息,然后點(diǎn)擊運(yùn)行即可。

本實(shí)例中當(dāng)運(yùn)行的任務(wù)在暫停的情況下,一旦重新恢復(fù),會(huì)將暫停期間的任務(wù)運(yùn)行如圖:

源碼鏈接:  https://github.com/albert-liu435/springquartz

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring中的之啟動(dòng)過程obtainFreshBeanFactory詳解

    Spring中的之啟動(dòng)過程obtainFreshBeanFactory詳解

    這篇文章主要介紹了Spring中的之啟動(dòng)過程obtainFreshBeanFactory詳解,在refresh時(shí),prepareRefresh后,馬上就調(diào)用了obtainFreshBeanFactory創(chuàng)建beanFactory以及掃描bean信息(beanDefinition),并通過BeanDefinitionRegistry注冊(cè)到容器中,需要的朋友可以參考下
    2024-02-02
  • Java日期時(shí)間字符串和毫秒相互轉(zhuǎn)換的方法

    Java日期時(shí)間字符串和毫秒相互轉(zhuǎn)換的方法

    這篇文章主要為大家詳細(xì)介紹了Java日期時(shí)間字符串和毫秒相互轉(zhuǎn)換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 一起來看看springboot集成redis的使用注解

    一起來看看springboot集成redis的使用注解

    這篇文章主要為大家詳細(xì)介紹了springboot集成redis的使用注解,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java實(shí)現(xiàn)視頻時(shí)間維度剪切的工具類

    Java實(shí)現(xiàn)視頻時(shí)間維度剪切的工具類

    這篇文章主要為大家詳細(xì)介紹了將視頻按照時(shí)間維度進(jìn)行剪切的Java工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • Spring詳解四種加載配置項(xiàng)的方法

    Spring詳解四種加載配置項(xiàng)的方法

    這篇文章主要給大家介紹了關(guān)于springboot加載配置項(xiàng)的四種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用springboot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-06-06
  • SpringBoot2.6.3集成quartz的方式

    SpringBoot2.6.3集成quartz的方式

    quartz是java里頭定時(shí)任務(wù)的經(jīng)典開源實(shí)現(xiàn),這里講述一下如何在SpringBoot2.6.3集成quartz,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2022-02-02
  • Spring Cloud Gateway全局異常處理的方法詳解

    Spring Cloud Gateway全局異常處理的方法詳解

    這篇文章主要給大家介紹了關(guān)于Spring Cloud Gateway全局異常處理的相關(guān)資料,需要的朋友可以參考下
    2018-10-10
  • 關(guān)于Java中代碼塊的執(zhí)行順序

    關(guān)于Java中代碼塊的執(zhí)行順序

    這篇文章主要介紹了關(guān)于Java中代碼塊的執(zhí)行順序,構(gòu)造代碼塊是給所有對(duì)象進(jìn)行統(tǒng)一初始化,而構(gòu)造函數(shù)是給對(duì)應(yīng)的對(duì)象初始化,因?yàn)闃?gòu)造函數(shù)是可以多個(gè)的,運(yùn)行哪個(gè)構(gòu)造函數(shù)就會(huì)建立什么樣的對(duì)象,但無論建立哪個(gè)對(duì)象,都會(huì)先執(zhí)行相同的構(gòu)造代碼塊,需要的朋友可以參考下
    2023-08-08
  • Java中的CyclicBarrier循環(huán)柵欄解析

    Java中的CyclicBarrier循環(huán)柵欄解析

    這篇文章主要介紹了Java中的CyclicBarrier循環(huán)柵欄解析,從字面上的意思可以知道,這個(gè)類的中文意思是"循環(huán)柵欄",大概的意思就是一個(gè)可循環(huán)利用的屏障,它的作用就是會(huì)讓所有線程都等待完成后才會(huì)繼續(xù)下一步行動(dòng),需要的朋友可以參考下
    2023-12-12
  • springboot集成activemq的實(shí)例代碼

    springboot集成activemq的實(shí)例代碼

    本篇文章主要介紹了springboot集成activemq的實(shí)例代碼,詳細(xì)的介紹了ActiveMQ和Spring-Boot 集成 ActiveMQ,有興趣的可以了解下。
    2017-05-05

最新評(píng)論