简单的quartz 可视化监听管理界面

spring-quartz。

导包、配置,不在此介绍。

简单的quartz管理界面,包括触发器的暂停、恢复、删除、修改(暂无),任务的运行、触发添加、创建,删除。

扩展内容:日志的管理,添加和创建触发器的操作(暂无实现)

本文原创,禁止转载。

扩展内容(暂不实现)

 

前端代码:

<%--
  Created by IntelliJ IDEA.
  User: ttttt
  Date: 2017/5/31
  Time: 15:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>


    Title
    
    


调度详情

<%----%> <%----%> <%----%> <%----%>
任务组名称 定时任务名称 触发器组名称 触发器名称任务类名触发器描述 时间表达式 任务状态 开始时间 上次重启时间 上次执行时间下次运行时间操作
${var.jobGroup} ${var.jobName} ${var.triggerGroupName} ${var.triggerName}${var.jobClass}${var.description} ${var.cronExpr} 未知 正常运行 暂停状态 完成状态 错误状态 锁定状态 <%--pattern="yyyy-MM-dd HH:mm:ss"/> 暂停 恢复 修改 删除
没有相关数据

任务详情

任务组名称 任务名称 任务类名 任务描述 持久性 禁止并发 数据更新 恢复性 关联调度器数量 操作
${var.jobGroup} ${var.jobName} ${var.jobClass} ${var.description} ${var.triggerCount} 运行一次 添加调度 创建调度 删除
没有相关数据

添加/修改 调度

创建调度


Controllere代码:

package com.sky.controller;

import com.sky.bean.JobDetailManage;
import com.sky.bean.JobEntity;
import com.sky.util.QuartzManager;
import com.sky.util.QuartzUtils;
import com.sun.istack.internal.NotNull;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author sky
 */
@Controller
public class JobController {

    private Scheduler quartzScheduler;

    @RequestMapping("/quartz")
    public ModelAndView quartz() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("quartz");
        JobKey jobKey = JobKey.jobKey("hello4", "group1");
        try {
            mv.addObject("jobInfos", getSchedulerJobInfo());
            mv.addObject("jobDetailInfos", getJobDetailInfos());
            mv.addObject("jobDetailInfo", getJobDetailInfo(jobKey));
        } catch (SchedulerException e) {
        }
        return mv;
    }

    //暂停任务
    @PostMapping("quartz/pauseJob")
    @ResponseBody
    public boolean pauseJob(@NotNull String triggerName, @NotNull String triggerGroup) {
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
        try {
            quartzScheduler.pauseTrigger(triggerKey);
        } catch (SchedulerException e) {
            System.out.println(e.getMessage());
        }
        return true;
    }

    //恢复任务
    @PostMapping("quartz/resumeJob")
    @ResponseBody
    public boolean resumeJob(@NotNull String triggerName, @NotNull String triggerGroup) throws Exception {
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
        quartzScheduler.resumeTrigger(triggerKey);
        return true;
    }

    //删除任务
    @PostMapping("quartz/deleteJob")
    @ResponseBody
    public boolean deleteJob(@NotNull String triggerName, @NotNull String triggerGroup) throws Exception {
        JobKey jobKey = JobKey.jobKey(triggerName, triggerGroup);
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
        quartzScheduler.pauseTrigger(triggerKey);
        return quartzScheduler.unscheduleJob(triggerKey);
    }

    //立即运行任务
    @PostMapping("quartz/triggerJob")
    @ResponseBody
    public boolean triggerJob(@NotNull String jobName, @NotNull String jobGroup) throws Exception {
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        quartzScheduler.triggerJob(jobKey);
        return true;
    }

    //删除任务详情
    @PostMapping("quartz/deleteJobDetail")
    @ResponseBody
    public boolean deleteJobDetail(@NotNull String jobName, @NotNull String jobGroup) throws Exception {
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        List triggers = (List) quartzScheduler.getTriggersOfJob(jobKey);
        if (triggers.size() > 0) {
            return false;
        }
        return quartzScheduler.deleteJob(jobKey);
    }

    //    @SuppressWarnings("unchecked")
    private List getSchedulerJobInfo() throws SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        quartzScheduler = schedulerFactory.getScheduler();

        List jobInfos = new ArrayList();
        List triggerGroupNames = quartzScheduler.getTriggerGroupNames();
        for (String triggerGroupName : triggerGroupNames) {
            Set triggerKeySet = quartzScheduler.getTriggerKeys(GroupMatcher.triggerGroupEquals(triggerGroupName));
            for (TriggerKey triggerKey : triggerKeySet) {
                Trigger t = quartzScheduler.getTrigger(triggerKey);
                JobEntity jobInfo = new JobEntity();
                if (t instanceof CronTrigger) {
                    CronTrigger trigger = (CronTrigger) t;
                    jobInfo.setCronExpr(trigger.getCronExpression());
                } else if (t instanceof SimpleTrigger) {
                    SimpleTrigger trigger = (SimpleTrigger) t;
                    jobInfo.setCronExpr(trigger.getRepeatCount() + 1 + "-" + trigger.getTimesTriggered() + "(" + trigger.getRepeatInterval() + ")");

                }
                JobKey jobKey = t.getJobKey();
                JobDetail jd = quartzScheduler.getJobDetail(jobKey);
                jobInfo.setJobName(jobKey.getName());
                jobInfo.setJobGroup(jobKey.getGroup());
                jobInfo.setTriggerName(triggerKey.getName());
                jobInfo.setTriggerGroupName(triggerKey.getGroup());
                jobInfo.setNextFireTime(t.getNextFireTime());
                jobInfo.setPreviousFireTime(t.getPreviousFireTime());
                jobInfo.setStartTime(t.getStartTime());
                jobInfo.setEndTime(t.getEndTime());
                jobInfo.setJobClass(jd.getJobClass().getCanonicalName());
                jobInfo.setDescription(t.getDescription());

                Trigger.TriggerState triggerState = quartzScheduler.getTriggerState(t.getKey());
                jobInfo.setJobStatus(triggerState.toString());// NONE无, NORMAL正常, PAUSED暂停, COMPLETE完全, ERROR错误, BLOCKED阻塞
                JobDataMap map = quartzScheduler.getJobDetail(jobKey).getJobDataMap();
//                jobInfo.setCount(Integer.parseInt((String) map.get("count")));
                if (null != map) {
                    jobInfo.setJobDataMap(map);
                } else {
                    jobInfo.setJobDataMap(new JobDataMap());
                }
                jobInfos.add(jobInfo);
            }
        }
        return jobInfos;
    }

    //    @SuppressWarnings("unchecked")
    private List getJobDetailInfos() throws SchedulerException {
        List jobDetailInfos = new ArrayList();

        for (String groupName : quartzScheduler.getJobGroupNames()) {
            for (JobKey jobKey : quartzScheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
                jobDetailInfos.add(getJobDetailInfo(jobKey));
            }
        }
        return jobDetailInfos;
    }

    //    @SuppressWarnings("unchecked")
    private JobDetailManage getJobDetailInfo(JobKey jobKey) throws SchedulerException {
        JobDetail jobDetail = quartzScheduler.getJobDetail(jobKey);
        List triggers = (List) quartzScheduler.getTriggersOfJob(jobKey);

        if(jobDetail != null){
            JobDetailManage jobDetailManage = new JobDetailManage();
            jobDetailManage.setJobName(jobKey.getName());
            jobDetailManage.setJobGroup(jobKey.getGroup());
            jobDetailManage.setDescription(jobDetail.getDescription());
            jobDetailManage.setDuration(jobDetail.isDurable());
            jobDetailManage.setJobClass(jobDetail.getJobClass().getCanonicalName());
            jobDetailManage.setJobDataMap(jobDetail.getJobDataMap());
            jobDetailManage.setPersistJobDataAfterExecution(jobDetail.isPersistJobDataAfterExecution());
            jobDetailManage.setConcurrentExectionDisallowed(jobDetail.isConcurrentExectionDisallowed());
            jobDetailManage.setRequestsRecovery(jobDetail.requestsRecovery());
            jobDetailManage.setTriggerCount(triggers.size());

            return jobDetailManage;
        }
        return null;
    }
}

  

 JobDetailManage类:

package com.sky.bean;

import org.quartz.JobDataMap;

/**
 * @author sky
 */
public class JobDetailManage {
    private String jobName;
    private String jobGroup;
    private String description;
    private Boolean duration;
    private String jobClass;
    private JobDataMap jobDataMap;
    private Boolean concurrentExectionDisallowed;
    private Boolean persistJobDataAfterExecution;
    private Boolean requestsRecovery;
    private Integer triggerCount;

    //get set
}

  

JobEntity类:

package com.sky.bean;

import org.quartz.JobDataMap;

import java.util.Date;

/**
 * 调度器信息
 *
 * @author sky
 */
public class JobEntity {
    private int jobId;
    private String jobType;
    private String jobGroup;
    private String jobName;
    private String triggerName;
    private String triggerGroupName;
    private String cronExpr;
    private String description;
    private Date previousFireTime;
    private Date nextFireTime;
    private String jobStatus;
    private Date startTime;
    private Date endTime;
    private Date reStartTime;
    private String jobClass;
    private int count;
    private JobDataMap jobDataMap;

    //get set
}

  

转载于:https://www.cnblogs.com/skyLogin/p/6957414.html

你可能感兴趣的:(java,javascript,前端,ViewUI)