⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ramjobstore.java

📁 Quartz is a full-featured, open source job scheduling system that can be integrated with, or used al
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                    Trigger[] trigs = getTriggersForJob(ctxt, tw.trigger
                            .getJobName(), tw.trigger.getJobGroup());
                    if ((trigs == null || trigs.length == 0) && !jw.jobDetail.isDurable()) {
                        removeJob(ctxt, tw.trigger.getJobName(), tw.trigger
                                .getJobGroup());
                    }
                }
            }
        }

        return found;
    }


    /**
     * @see org.quartz.spi.JobStore#replaceTrigger(org.quartz.core.SchedulingContext, java.lang.String, java.lang.String, org.quartz.Trigger)
     */
    public boolean replaceTrigger(SchedulingContext ctxt, String triggerName, String groupName, Trigger newTrigger) throws JobPersistenceException {
        String key = TriggerWrapper.getTriggerNameKey(triggerName, groupName);

        boolean found = false;

        synchronized (triggerLock) {
            // remove from triggers by FQN map
            TriggerWrapper tw = (TriggerWrapper) triggersByFQN.remove(key);
            found = ( tw == null) ? false : true;

            if (found) {

                if (!tw.getTrigger().getJobName().equals(newTrigger.getJobName()) ||
                    !tw.getTrigger().getJobGroup().equals(newTrigger.getJobGroup())) {
                    throw new JobPersistenceException("New trigger is not related to the same job as the old trigger.");
                }

                tw = null;
                // remove from triggers by group
                HashMap grpMap = (HashMap) triggersByGroup.get(groupName);
                if (grpMap != null) {
                    grpMap.remove(triggerName);
                    if (grpMap.size() == 0) {
                        triggersByGroup.remove(groupName);
                    }
                }
                // remove from triggers array
                Iterator tgs = triggers.iterator();
                while (tgs.hasNext()) {
                    tw = (TriggerWrapper) tgs.next();
                    if (key.equals(tw.key)) {
                        tgs.remove();
                        break;
                    }
                }
                timeTriggers.remove(tw);

                try {
                    storeTrigger(ctxt, newTrigger, false);
                } catch(JobPersistenceException jpe) {
                    storeTrigger(ctxt, tw.getTrigger(), false); // put previous trigger back...
                    throw jpe;
                }
            }
        }

        return found;
    }

    /**
     * <p>
     * Retrieve the <code>{@link org.quartz.JobDetail}</code> for the given
     * <code>{@link org.quartz.Job}</code>.
     * </p>
     *
     * @param jobName
     *          The name of the <code>Job</code> to be retrieved.
     * @param groupName
     *          The group name of the <code>Job</code> to be retrieved.
     * @return The desired <code>Job</code>, or null if there is no match.
     */
    public JobDetail retrieveJob(SchedulingContext ctxt, String jobName,
            String groupName) {
        JobWrapper jw = (JobWrapper) jobsByFQN.get(JobWrapper.getJobNameKey(
                jobName, groupName));

        return (jw != null) ? (JobDetail)jw.jobDetail.clone() : null;
    }

    /**
     * <p>
     * Retrieve the given <code>{@link org.quartz.Trigger}</code>.
     * </p>
     *
     * @param triggerName
     *          The name of the <code>Trigger</code> to be retrieved.
     * @param groupName
     *          The group name of the <code>Trigger</code> to be retrieved.
     * @return The desired <code>Trigger</code>, or null if there is no
     *         match.
     */
    public Trigger retrieveTrigger(SchedulingContext ctxt, String triggerName,
            String groupName) {
        TriggerWrapper tw = (TriggerWrapper) triggersByFQN.get(TriggerWrapper
                .getTriggerNameKey(triggerName, groupName));

        return (tw != null) ? (Trigger)tw.getTrigger().clone() : null;
    }

    /**
     * <p>
     * Get the current state of the identified <code>{@link Trigger}</code>.
     * </p>
     *
     * @see Trigger#STATE_NORMAL
     * @see Trigger#STATE_PAUSED
     * @see Trigger#STATE_COMPLETE
     * @see Trigger#STATE_ERROR
     * @see Trigger#STATE_BLOCKED
     * @see Trigger#STATE_NONE
     */
    public int getTriggerState(SchedulingContext ctxt, String triggerName,
            String groupName) throws JobPersistenceException {
        TriggerWrapper tw = (TriggerWrapper) triggersByFQN.get(TriggerWrapper
                .getTriggerNameKey(triggerName, groupName));
        if (tw == null) {
            return Trigger.STATE_NONE;
        }

        if (tw.state == TriggerWrapper.STATE_COMPLETE) {
            return Trigger.STATE_COMPLETE;
        }

        if (tw.state == TriggerWrapper.STATE_PAUSED) {
            return Trigger.STATE_PAUSED;
        }

        if (tw.state == TriggerWrapper.STATE_PAUSED_BLOCKED) {
            return Trigger.STATE_PAUSED;
        }

        if (tw.state == TriggerWrapper.STATE_BLOCKED) {
            return Trigger.STATE_BLOCKED;
        }

        if (tw.state == TriggerWrapper.STATE_ERROR) {
            return Trigger.STATE_ERROR;
        }

        return Trigger.STATE_NORMAL;
    }

    /**
     * <p>
     * Store the given <code>{@link org.quartz.Calendar}</code>.
     * </p>
     *
     * @param calendar
     *          The <code>Calendar</code> to be stored.
     * @param replaceExisting
     *          If <code>true</code>, any <code>Calendar</code> existing
     *          in the <code>JobStore</code> with the same name & group
     *          should be over-written.
     * @param updateTriggers
     *          If <code>true</code>, any <code>Trigger</code>s existing
     *          in the <code>JobStore</code> that reference an existing
     *          Calendar with the same name with have their next fire time
     *          re-computed with the new <code>Calendar</code>.
     * @throws ObjectAlreadyExistsException
     *           if a <code>Calendar</code> with the same name already
     *           exists, and replaceExisting is set to false.
     */
    public void storeCalendar(SchedulingContext ctxt, String name,
            Calendar calendar, boolean replaceExisting, boolean updateTriggers)
        throws ObjectAlreadyExistsException {
        Object obj = calendarsByName.get(name);

        if (obj != null && replaceExisting == false) {
            throw new ObjectAlreadyExistsException(
                "Calendar with name '" + name + "' already exists.");
        } else if (obj != null) {
            calendarsByName.remove(name);
        }

        calendarsByName.put(name, calendar);

        if(obj != null && updateTriggers) {
            synchronized (triggerLock) {
                Iterator trigs = getTriggerWrappersForCalendar(name).iterator();
                while (trigs.hasNext()) {
                    TriggerWrapper tw = (TriggerWrapper) trigs.next();
                    Trigger trig = tw.getTrigger();
                    boolean removed = timeTriggers.remove(tw);

                    trig.updateWithNewCalendar(calendar, getMisfireThreshold());

                    if(removed) {
                        timeTriggers.add(tw);
                    }
                }
            }
        }
    }

    /**
     * <p>
     * Remove (delete) the <code>{@link org.quartz.Calendar}</code> with the
     * given name.
     * </p>
     *
     * <p>
     * If removal of the <code>Calendar</code> would result in
     * <code.Trigger</code>s pointing to non-existent calendars, then a
     * <code>JobPersistenceException</code> will be thrown.</p>
     *       *
     * @param calName The name of the <code>Calendar</code> to be removed.
     * @return <code>true</code> if a <code>Calendar</code> with the given name
     * was found and removed from the store.
     */
    public boolean removeCalendar(SchedulingContext ctxt, String calName)
        throws JobPersistenceException {
        int numRefs = 0;

        synchronized (triggerLock) {
            Iterator itr = triggers.iterator();
            while (itr.hasNext()) {
                Trigger trigg = ((TriggerWrapper) itr.next()).trigger;
                if (trigg.getCalendarName() != null
                        && trigg.getCalendarName().equals(calName)) {
                    numRefs++;
                }
            }
        }

        if (numRefs > 0) {
            throw new JobPersistenceException(
                    "Calender cannot be removed if it referenced by a Trigger!");
        }

        return (calendarsByName.remove(calName) != null);
    }

    /**
     * <p>
     * Retrieve the given <code>{@link org.quartz.Trigger}</code>.
     * </p>
     *
     * @param calName
     *          The name of the <code>Calendar</code> to be retrieved.
     * @return The desired <code>Calendar</code>, or null if there is no
     *         match.
     */
    public Calendar retrieveCalendar(SchedulingContext ctxt, String calName) {
        return (Calendar) calendarsByName.get(calName);
    }

    /**
     * <p>
     * Get the number of <code>{@link org.quartz.JobDetail}</code> s that are
     * stored in the <code>JobsStore</code>.
     * </p>
     */
    public int getNumberOfJobs(SchedulingContext ctxt) {
        return jobsByFQN.size();
    }

    /**
     * <p>
     * Get the number of <code>{@link org.quartz.Trigger}</code> s that are
     * stored in the <code>JobsStore</code>.
     * </p>
     */
    public int getNumberOfTriggers(SchedulingContext ctxt) {
        return triggers.size();
    }

    /**
     * <p>
     * Get the number of <code>{@link org.quartz.Calendar}</code> s that are
     * stored in the <code>JobsStore</code>.
     * </p>
     */
    public int getNumberOfCalendars(SchedulingContext ctxt) {
        return calendarsByName.size();
    }

    /**
     * <p>
     * Get the names of all of the <code>{@link org.quartz.Job}</code> s that
     * have the given group name.
     * </p>
     */
    public String[] getJobNames(SchedulingContext ctxt, String groupName) {
        String[] outList = null;
        HashMap grpMap = (HashMap) jobsByGroup.get(groupName);
        if (grpMap != null) {
            synchronized (triggerLock) {
                outList = new String[grpMap.size()];
                int outListPos = 0;

                for (Iterator valueIter = grpMap.values().iterator(); valueIter.hasNext();) {
                    JobWrapper jw = (JobWrapper)valueIter.next();

                    if (jw != null) {
                        outList[outListPos++] = jw.jobDetail.getName();
                    }
                }
            }
        } else {
            outList = new String[0];
        }

        return outList;
    }

    /**
     * <p>
     * Get the names of all of the <code>{@link org.quartz.Calendar}</code> s
     * in the <code>JobStore</code>.
     * </p>
     *
     * <p>
     * If there are no Calendars in the given group name, the result should be
     * a zero-length array (not <code>null</code>).
     * </p>
     */
    public String[] getCalendarNames(SchedulingContext ctxt) {
        Set names = calendarsByName.keySet();
        return (String[]) names.toArray(new String[names.size()]);
    }

    /**
     * <p>
     * Get the names of all of the <code>{@link org.quartz.Trigger}</code> s
     * that have the given group name.
     * </p>
     */
    public String[] getTriggerNames(SchedulingContext ctxt, String groupName) {
        String[] outList = null;
        HashMap grpMap = (HashMap) triggersByGroup.get(groupName);
        if (grpMap != null) {
            synchronized (triggerLock) {
                outList = new String[grpMap.size()];
                int outListPos = 0;

                for (Iterator valueIter = grpMap.values().iterator(); valueIter.hasNext();) {
                    TriggerWrapper tw = (TriggerWrapper) valueIter.next();

                    if (tw != null) {
                        outList[outListPos++] = tw.trigger.getName();
                    }
                }
            }
        } else {
            outList = new String[0];
        }

        return outList;
    }

    /**
     * <p>
     * Get the names of all of the <code>{@link org.quartz.Job}</code>
     * groups.
     * </p>
     */
    public String[] getJobGroupNames(SchedulingContext ctxt) {
        String[] outList = null;

        synchronized (triggerLock) {
            outList = new String[jobsByGroup.size()];
            int outListPos = 0;
            Iterator keys = jobsByGroup.keySet().iterator();
            while (keys.hasNext()) {
                outList[outListPos++] = (String) keys.next();
            }
        }

        return outList;
    }

    /**
     * <p>
     * Get the names of all of the <code>{@link org.quartz.Trigger}</code>
     * groups.
     * </p>
     */
    public String[] getTriggerGroupNames(SchedulingContext ctxt) {
        String[] outList = null;

        synchronized (triggerLock) {
            outList = new String[triggersByGroup.size()];
            int outListPos = 0;
            Iterator keys = triggersByGroup.keySet().iterator();
            while (keys.hasNext()) {
                outList[outListPos++] = (String) keys.next();
            }
        }

        return outList;
    }

    /**
     * <p>
     * Get all of the Triggers that are associated to the given Job.
     * </p>
     *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -