java - Quartz中,对job和trigger都要定义一个组名字(group),这个组有什么用处?
PHPz
PHPz 2017-04-17 16:03:03
0
1
2322
JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();

如上代码

  1. 对于JobDetail,"group1"有什么用处?在什么情况下需要用到它?

  2. 对于Trigger,"group1"有什么用处?在什么情况下需要用到它?

PHPz
PHPz

学习是最好的投资!

全部回覆(1)
洪涛

在 org.quartz 套件中的 Schedule 介面的註解說明了:

 * <p>
 * <code>Job</code> s and <code>Trigger</code> s have a name and group
 * associated with them, which should uniquely identify them within a single
 * <code>{@link Scheduler}</code>. The 'group' feature may be useful for
 * creating logical groupings or categorizations of <code>Jobs</code> s and
 * <code>Triggers</code>s. If you don't have need for assigning a group to a
 * given <code>Jobs</code> of <code>Triggers</code>, then you can use the
 * <code>DEFAULT_GROUP</code> constant defined on this interface.
 * </p>

可見, group 是用於分類的,相當於一個命名空間。

另外,從 equals 分析 group 有什麼用。比如說,你是判斷兩個 trigger 或 job 是一樣的呢?例如 trigger,在 SimpleTriggerImpl 類別中

@Override
    public boolean equals(Object o) {
        if(!(o instanceof Trigger))
            return false;
        
        Trigger other = (Trigger)o;

        return !(other.getKey() == null || getKey() == null) && getKey().equals(other.getKey());

    }

那麼,這個 equals方法就是 在超類別 Key 中的equals 方法,這裡就用到了 group:

@Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        @SuppressWarnings("unchecked")
        Key<T> other = (Key<T>) obj;
        if (group == null) {
            if (other.group != null)
                return false;
        } else if (!group.equals(other.group))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

所以說,group 其實就是一個分類,命令空間的意思。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!