package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.JobAndTrigger;
import com.example.demo.job.BaseJob;
import com.example.demo.service.IJobAndTriggerService;
import com.github.pagehelper.PageInfo;
@RestController
@RequestMapping(value="/job")
public class JobController
{
@Autowired
private IJobAndTriggerService iJobAndTriggerService;
private static Logger log = LoggerFactory.getLogger(JobController.class);
@PostMapping(value="/addjob")
public void addjob(@RequestParam(value="jobClassName")String jobClassName,
@RequestParam(value="jobGroupName")String jobGroupName,
@RequestParam(value="cronExpression")String cronExpression) throws Exception
{
addJob(jobClassName, jobGroupName, cronExpression);
}
public static void addJob(String jobClassName, String jobGroupName, String cronExpression)throws Exception{
// 通过SchedulerFactory获取一个调度器实例
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
// 启动调度器
sched.start();
//构建job信息
JobDetail jobDetail = JobBuilder.newJob(getClass(jobClassName).getClass()).withIdentity(jobClassName, jobGroupName).build();
//表达式调度构建器(即任务执行的时间)
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
//按新的cronExpression表达式构建一个新的trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName)
.withSchedule(scheduleBuilder).build();
try {
sched.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
System.out.println("创建定时任务失败"+e);
throw new Exception("创建定时任务失败");
}
}
@PostMapping(value="/pausejob")
public void pausejob(@RequestParam(value="jobClassName")String jobClassName, @RequestParam(value="jobGroupName")String jobGroupName) throws Exception
{
jobPause(jobClassName, jobGroupName);
}
public static void jobPause(String jobClassName, String jobGroupName) throws Exception
{
// 通过SchedulerFactory获取一个调度器实例
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
sched.pauseJob(JobKey.jobKey(jobClassName, jobGroupName));
}
@PostMapping(value="/resumejob")
public void resumejob(@RequestParam(value="jobClassName")String jobClassName, @RequestParam(value="jobGroupName")String jobGroupName) throws Exception
{
jobresume(jobClassName, jobGroupName);
}
public static void jobresume(String jobClassName, String jobGroupName) throws Exception
{
// 通过SchedulerFactory获取一个调度器实例
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
sched.resumeJob(JobKey.jobKey(jobClassName, jobGroupName));
}
@PostMapping(value="/reschedulejob")
public void rescheduleJob(@RequestParam(value="jobClassName")String jobClassName,
@RequestParam(value="jobGroupName")String jobGroupName,
@RequestParam(value="cronExpression")String cronExpression) throws Exception
{
jobreschedule(jobClassName, jobGroupName, cronExpression);
}
public static void jobreschedule(String jobClassName, String jobGroupName, String cronExpression) throws Exception
{
try {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobClassName, jobGroupName);
// 表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
// 按新的cronExpression表达式重新构建trigger
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
// 按新的trigger重新设置job执行
scheduler.rescheduleJob(triggerKey, trigger);
} catch (SchedulerException e) {
System.out.println("更新定时任务失败"+e);
throw new Exception("更新定时任务失败");
}
}
@PostMapping(value="/deletejob")
public void deletejob(@RequestParam(value="jobClassName")String jobClassName, @RequestParam(value="jobGroupName")String jobGroupName) throws Exception
{
jobdelete(jobClassName, jobGroupName);
}
public static void jobdelete(String jobClassName, String jobGroupName) throws Exception
{
// 通过SchedulerFactory获取一个调度器实例
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
sched.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
sched.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
sched.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
}
@GetMapping(value="/queryjob")
public Map<String, Object> queryjob(@RequestParam(value="pageNum")Integer pageNum, @RequestParam(value="pageSize")Integer pageSize)
{
PageInfo<JobAndTrigger> jobAndTrigger = iJobAndTriggerService.getJobAndTriggerDetails(pageNum, pageSize);
Map<String, Object> map = new HashMap<String, Object>();
map.put("JobAndTrigger", jobAndTrigger);
map.put("number", jobAndTrigger.getTotal());
return map;
}
public static BaseJob getClass(String classname) throws Exception
{
Class<?> class1 = Class.forName("com.example.demo.job.HelloJob");
BaseJob baseJob = (BaseJob) class1.newInstance();
return baseJob;
//return (BaseJob)class1.newInstance();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
spring boot+vue+quartz2.2.3整合定时调度系统
共48个文件
java:11个
class:11个
xml:4个
5星 · 超过95%的资源 需积分: 50 142 下载量 80 浏览量
2017-09-13
14:51:56
上传
评论 7
收藏 76KB RAR 举报
温馨提示
spring boot+vue+quartz2.2.3整合定时调度系统持久化道mysql,sql脚本在项目里面。sql文件spring boot+vue+quartz2.2.3整合定时调度系统持久化道mysql
资源推荐
资源详情
资源评论
收起资源包目录
demo.rar (48个子文件)
demo
pom.xml 3KB
target
test-classes
com
example
demo
DemoApplicationTests.class 622B
maven-status
maven-compiler-plugin
compile
default-compile
inputFiles.lst 1KB
createdFiles.lst 0B
classes
quartz.properties 1KB
META-INF
maven
com.example
demo
pom.xml 3KB
pom.properties 257B
MANIFEST.MF 334B
application.yml 302B
com
example
demo
dao
JobAndTriggerMapper.class 277B
job
HelloJob.class 1KB
BaseJob.class 255B
NewJob.class 1KB
mapper
JobMapper.xml 861B
controller
JobController.class 7KB
DemoApplication.class 879B
service
IJobAndTriggerService.class 321B
impl
JobAndTriggerImpl.class 1KB
entity
JobAndTrigger.class 2KB
SchedulerConfig.class 2KB
static
JobManager.html 9KB
generated-sources
annotations
mvnw 6KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.core.resources.prefs 209B
org.eclipse.jdt.core.prefs 243B
src
test
java
com
example
demo
DemoApplicationTests.java 331B
main
resources
quartz.properties 1KB
application.yml 302B
static
JobManager.html 9KB
java
com
example
demo
dao
JobAndTriggerMapper.java 198B
job
BaseJob.java 256B
HelloJob.java 567B
NewJob.java 557B
mapper
JobMapper.xml 861B
controller
JobController.java 6KB
service
IJobAndTriggerService.java 249B
impl
JobAndTriggerImpl.java 838B
DemoApplication.java 485B
entity
JobAndTrigger.java 2KB
SchedulerConfig.java 1KB
.project 556B
.classpath 1KB
.gitignore 249B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
mvnw.cmd 5KB
.gitattributes 378B
qrtz_blob_triggers.sql 10KB
共 48 条
- 1
资源评论
- 棋圣20082019-01-02下载了 需要强制评论才能再次下载
- Ginoy2017-10-25下载了 需要强制评论才能再次下载
- 忍冬草王硕2017-11-03还行吧,都是那种简单的Demo,不是我想要的源码,谢谢
- qq_393167572017-11-07虽然写的简单,但是分享出来了还是不错
- 2019-11-27可以呀,学习了
丶附耳聆听
- 粉丝: 4
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功