java - Quartz中,对job和trigger都要定义一个组名字(group),这个组有什么用处?
PHPz
PHPz 2017-04-17 16:03:03
0
1
2298
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

学习是最好的投资!

Antworte allen (1)
洪涛

在 org.quartz 包中的 Schedule 接口的注释说明了:

* 

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

可见, 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 other = (Key) 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 其实就是一个分类,命令空间的意思。

    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage
    Über uns Haftungsausschluss Sitemap
    Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!