温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring怎么整合Quartz开发

发布时间:2020-07-31 09:28:18 来源:亿速云 阅读:139 作者:小猪 栏目:编程语言

这篇文章主要为大家展示了Spring怎么整合Quartz开发,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

xml文件配置如下:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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&#63;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 * * * * &#63;", SimpleJob.class, new HashMap<>());
    Thread.sleep(60 * 1000);
    //重新设置调度时间
    System.out.println("重新设置调度时间");
    rescheduleJob("trigger1","trigger1","0/10 * * * * &#63;");
  
    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<&#63; 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正常配置信息,然后点击运行即可。

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

Spring怎么整合Quartz开发

以上就是关于Spring怎么整合Quartz开发的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI