首页  >  问答  >  正文

java - Quartz中,对job和trigger都要定义一个组名字(group),这个组有什么用处?

JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

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

如上代码

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

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

PHPzPHPz2717 天前2326

全部回复(1)我来回复

  • PHP中文网

    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 其实就是一个分类,命令空间的意思。

    回复
    0
  • 取消回复