JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
如上代码
对于JobDetail,"group1"有什么用处?在什么情况下需要用到它?
对于Trigger,"group1"有什么用处?在什么情况下需要用到它?
PHP中文网2017-04-17 16:05:03
在 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 其實就是一個分類,命令空間的意思。