All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.n3r.quartz.glass.web.controller.IndexController Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
package org.n3r.quartz.glass.web.controller;

import org.joda.time.DateTime;
import org.n3r.quartz.glass.configuration.Configuration;
import org.quartz.*;
import org.quartz.Trigger.TriggerState;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * The home page !
 */
@Controller
public class IndexController {

    @Autowired
    protected Scheduler quartzScheduler;

    @Autowired
    protected Configuration configuration;

    @RequestMapping({"/", "/index"})
    public String dashboard(Model model) throws SchedulerException {
        List runningJobs = quartzScheduler.getCurrentlyExecutingJobs();

        List pausedTriggers = new ArrayList();
        List hangedTriggers = new ArrayList();

        List groups = quartzScheduler.getJobGroupNames();

        Collections.sort(groups);

        for (String group : groups) {
            Set jobKeys = quartzScheduler.getJobKeys(GroupMatcher.groupEquals(group));

            for (JobKey jobKey : jobKeys) {
                List triggers = quartzScheduler.getTriggersOfJob(jobKey);

                for (Trigger trigger : triggers) {
                    if (isPaused(trigger)) {
                        pausedTriggers.add(trigger);
                    } else if (isHanged(trigger, runningJobs)) {
                        hangedTriggers.add(trigger);
                    }
                }
            }
        }

        model.addAttribute("runningJobs", runningJobs);
        model.addAttribute("pausedTriggers", pausedTriggers);
        model.addAttribute("hangedTriggers", hangedTriggers);

        return "dashboard";
    }

    @RequestMapping("/start")
    public String start() throws SchedulerException {
        quartzScheduler.start();

        return "redirect:/glass/";
    }

    @RequestMapping("/standby")
    public String standby() throws SchedulerException {
        quartzScheduler.standby();

        return "redirect:/glass/";
    }

    @RequestMapping("/restartTrigger")
    public String restartTrigger(String group, String name) throws SchedulerException {
        Trigger trigger = quartzScheduler.getTrigger(new TriggerKey(name, group));

        if (trigger == null) return "redirect:/glass/";

        trigger = trigger.getTriggerBuilder().startAt(new Date()).build();

        quartzScheduler.rescheduleJob(trigger.getKey(), trigger);

        return "redirect:/glass/";
    }

    @RequestMapping("/interrupt")
    public String interrupt(String group, String name) throws SchedulerException {
        JobDetail job = quartzScheduler.getJobDetail(new JobKey(name, group));

        if (job == null) return "redirect:/glass/";

        quartzScheduler.interrupt(job.getKey());

        return "redirect:/glass/";
    }

    private boolean isPaused(Trigger trigger) throws SchedulerException {
        return quartzScheduler.getTriggerState(trigger.getKey()) == TriggerState.PAUSED;
    }

    private boolean isHanged(Trigger trigger, List runningJobs) throws SchedulerException {
        Date nextFireTime = trigger.getNextFireTime();

        if (nextFireTime == null) return false;

        if (isRunning(trigger, runningJobs)) return false;

        Date oneMinuteAgo = new DateTime().minusMinutes(1).toDate();

        return nextFireTime.before(oneMinuteAgo);
    }

    private boolean isRunning(Trigger trigger, List runningJobs) {
        for (JobExecutionContext runningJob : runningJobs) {
            if (runningJob.getTrigger().getKey().equals(trigger.getKey())) {
                return true;
            }
        }

        return false;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy