先贴Event抽象类的源码

  1. public abstract class Event implements Serializable {
  2. private static final AtomicLong SEQUENCE = new AtomicLong(0);
  3. private final long sequence = SEQUENCE.getAndIncrement();
  4. public long sequence() {
  5. return sequence;
  6. }
  7. }
  • 其实这个类没什么具体的功能,仅仅起到一个标记作用,用来标记其子类是一个事件类,而这个事件的具体属性也都在其子类里面

拿一个具体的实现来瞅瞅

  1. /**
  2. * 操作客户端事件
  3. * Operation client event.
  4. */
  5. public class ClientOperationEvent extends Event {
  6. private static final long serialVersionUID = -4582413232902517619L;
  7. private final String clientId;
  8. private final Service service;
  9. public ClientOperationEvent(String clientId, Service service) {
  10. this.clientId = clientId;
  11. this.service = service;
  12. }
  13. public String getClientId() {
  14. return clientId;
  15. }
  16. public Service getService() {
  17. return service;
  18. }
  19. /**
  20. * 客户端服务注册事件
  21. * Client register service event.
  22. */
  23. public static class ClientRegisterServiceEvent extends ClientOperationEvent {
  24. private static final long serialVersionUID = 3412396514440368087L;
  25. public ClientRegisterServiceEvent(Service service, String clientId) {
  26. super(clientId, service);
  27. }
  28. }
  29. /**
  30. * 客户端注销服务事件
  31. * Client deregister service event.
  32. */
  33. public static class ClientDeregisterServiceEvent extends ClientOperationEvent {
  34. private static final long serialVersionUID = -4518919987813223120L;
  35. public ClientDeregisterServiceEvent(Service service, String clientId) {
  36. super(clientId, service);
  37. }
  38. }
  39. /**
  40. * 客户端订阅服务事件
  41. * Client subscribe service event.
  42. */
  43. public static class ClientSubscribeServiceEvent extends ClientOperationEvent {
  44. private static final long serialVersionUID = -4518919987813223120L;
  45. public ClientSubscribeServiceEvent(Service service, String clientId) {
  46. super(clientId, service);
  47. }
  48. }
  49. /**
  50. * 客户端取消订阅服务事件
  51. * Client unsubscribe service event.
  52. */
  53. public static class ClientUnsubscribeServiceEvent extends ClientOperationEvent {
  54. private static final long serialVersionUID = -4518919987813223120L;
  55. public ClientUnsubscribeServiceEvent(Service service, String clientId) {
  56. super(clientId, service);
  57. }
  58. }
  59. }
  • 我们可以看到的是,这个**ClientOperationEvent**客户端操作事件里面具体定义了一些其他的事件,如客户端服务注册事件,客户端注销服务事件,客户端订阅服务事件,客户端取消订阅服务事件
  • 均继承自**ClientOperationEvent****Event**,所以这里的每一个事件均含有sequence,clientId,service这些属性
  • 既然这里每个事件类都携带有本身这个事件所带有的含义(属性),如客户端注册,自然就应该有客户端id以及客户端所包含的信息,如namespace,group等,所以不同的订阅者只需要按需来对这个事件进行相应的处理
  • 当然每次新生成一个事件,sequence都会原子自增1次,因为**private static final AtomicLong SEQUENCE = new AtomicLong(0)**这段代码,SEQUENCE static定义的,并且每次new一个Enent事件出来的时候sequence = _SEQUENCE_.getAndIncrement_()_;即模板SEQUENCE就会原子增加,可以看下面这段代码
    1. public static void main(String[] args) {
    2. for(int i=0;i<10;i++){
    3. ClientOperationEvent clientOperationEvent = new ClientOperationEvent(null, null);
    4. System.out.print(clientOperationEvent.sequence());
    5. }
    6. }
    7. =================================================
    8. 0123456789
    不过有个小问题,如果达到long的最大值的话(9223372036854775807)会不会直接事件通知就down了?

当然,既然唠嗑的是一个事件,如果看了之前的几篇文章自然就会想到,订阅者Subscriber,毕竟事件的落地还是订阅者
这里还是贴上上篇订阅者Subscriber里比较熟悉的代码

  1. @Component
  2. public class ClientServiceIndexesManager extends SmartSubscriber {
  3. @Override
  4. public List<Class<? extends Event>> subscribeTypes() {
  5. List<Class<? extends Event>> result = new LinkedList<>();
  6. result.add(ClientOperationEvent.ClientRegisterServiceEvent.class);
  7. result.add(ClientOperationEvent.ClientDeregisterServiceEvent.class);
  8. result.add(ClientOperationEvent.ClientSubscribeServiceEvent.class);
  9. result.add(ClientOperationEvent.ClientUnsubscribeServiceEvent.class);
  10. result.add(ClientEvent.ClientDisconnectEvent.class);
  11. return result;
  12. }
  13. @Override
  14. public void onEvent(Event event) {
  15. if (event instanceof ClientEvent.ClientDisconnectEvent) {
  16. handleClientDisconnect((ClientEvent.ClientDisconnectEvent) event);
  17. } else if (event instanceof ClientOperationEvent) {
  18. handleClientOperation((ClientOperationEvent) event);
  19. }
  20. }
  21. }
  • 可以看到,在**subscribeTypes()**方法里,这个订阅者订阅了我们的**ClientOperationEvent**事件以及其子类们(该方法调用处获取的是订阅的类型),当事件发布者调用了订阅者的onEvent方法的时候,假设当前事件是 ClientRegisterServiceEvent 那么就会去执行handleClientOperation 方法来做客户端注册操作

当然,顺便贴一下发布这个注册事件的代码:

  1. NotifyCenter.publishEvent(new ClientOperationEvent.ClientRegisterServiceEvent(singleton, clientId));

至此,Event的细节已经剖析的差不多了