java - Quartz中,对job和trigger都要定义一个组名字(group),这个组有什么用处?
PHPz
PHPz 2017-04-17 16:03:03
0
1
2360
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 其实就是一个分类,命令空间的意思。

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿