我希望提供一个用户界面来动态定义Quartz JOBs。用户可以定义JOB的属性,如JOB名称、cron表达式或时间间隔、任务的特定java类等。
是否有任何开源项目可提供此功能?或者,如果我想创建自己的动态Quartz Scheduler模块,应该怎么做?
是否有任何开源项目可提供此功能?或者,如果我想创建自己的动态Quartz Scheduler模块,应该怎么做?
动态Quartz JOB调度的示例代码:
Maven依赖项
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
应用程序上下文文件 (dynamic-jobs.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
<!-- The Task Bean-->
<bean id="myTask" class="com.xxx.example.dynamicjob.MyTask" />
<!-- The quartz scheduler configuration -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"/>
</beans>
任务的Java类
package com.xxx.example.dynamicjob;
public class MyTask
{
public void performAction() {
System.out.println("Hey, you reached me...:)");
}
}
创建动态作业的Java类
package com.xxx.example.dynamicjob;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
public class DynamicJobExample {
/**
* @param args
*/
public static void main(String[] args) {
ClassPathResource res = new ClassPathResource("dynamic-jobs.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
//get the quartzFactory bean
Scheduler scheduler = (Scheduler) factory.getBean("scheduler");
//get the task bean
MyTask myTask = (MyTask) factory.getBean("myTask");
try {
// create JOB
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
jobDetail.setTargetObject(myTask);
jobDetail.setTargetMethod("performAction");
jobDetail.setName("MyJobDetail");
jobDetail.setConcurrent(false);
jobDetail.afterPropertiesSet();
/* SimpleTriggerBean trigger = new SimpleTriggerBean();
trigger.setBeanName("MyTrigger");
trigger.setJobDetail((JobDetail) jobDetail.getObject());
trigger.setRepeatInterval(5000);
trigger.afterPropertiesSet();
*/
// create CRON Trigger
CronTriggerBean cronTrigger = new CronTriggerBean();
cronTrigger.setBeanName("CRON0001");
// Execute after each 5 second
String expression = "5 * * * * ?";
cronTrigger.setCronExpression(expression);
cronTrigger.afterPropertiesSet();
//scheduler.scheduleJob(jobDetail, cronTrigger);
scheduler.scheduleJob((JobDetail) jobDetail.getObject(), cronTrigger);
// Start Scheduler
scheduler.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用Spring 4而不会遇到任何问题(Quartz 2.0) - spring-dispatcher-servlet.xml
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:scheduler id="myScheduler" pool-size="10"/>
<task:executor id="myExecutor" pool-size="1"/>
将上述创建的bean注入Spring bean或Service类中
@Service
public class DynamicJobExample {
@Autowired
@Qualifier("myScheduler")
private TaskScheduler scheduler;
public void schedule() throws ParseException {
scheduler.schedule(new MyTask(), new CronTrigger("5 * * * * ?"));
}
}
我的任务应该是一个可运行的类
public class MyTask implements Runnable{
@Override
public void run() {
System.out.println("Hey, you reached me...:)");
}
}
<spring.version>4.1.0.RELEASE</spring.version>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
JobDetail jobDetail;
String myJobClass = "com.MyJobClass";
String myJobName = "myJobName";
String myJobGroup ="myJobGroup";
Class myJobClassType= Class.forName(myJobClass);
jobDetail =JobBuilder.newJob().ofType(myJobClassType)
.withIdentity(myJobName, myJobGroup).build();
String myGroup = "myGroup";
String name = "TriggerName";
String expresion = "5 * * * * ?";
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(name, myGroup)
.withSchedule(
CronScheduleBuilder.cronSchedule(expresion))
.build();
package com.myproject;
import java.text.ParseException;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.stereotype.Component;
@Component
public class QuartzSchedulerExample {
public QuartzSchedulerExample() {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler scheduler;
try {
scheduler = schedFact.getScheduler();
JobDetail details = JobBuilder.newJob(MyMailBean.class)
.withDescription("something")
.withIdentity("mejob","myGroup")
.storeDurably(true).build();
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setName("T1");
try {
trigger.setCronExpression("0 0 0 * * ? *");
} catch (ParseException e) {
e.printStackTrace();
}
trigger.setDescription("desc");
scheduler.scheduleJob(details,trigger);
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
该任务将在每天的00:00:00(小时:分钟:秒)运行
我有一个类似的问题,我想在quartz(2.3.2版本)的JOB_DETAILS表中更新cron表达式。 情况是这样的,我已经有一个预定的作业正在运行,我想更新它并以不同的频率运行它。 其他的事情我都处理好了,但是表格上的更新不会发生。所以在阅读文档后,我发现可以使用add方法更新作业的详细信息。
// store, and set overwrite flag to 'true'
scheduler.addJob(jobDetail, true);
我所做的事情只涉及Java。