先贴Event抽象类的源码
public abstract class Event implements Serializable {
private static final AtomicLong SEQUENCE = new AtomicLong(0);
private final long sequence = SEQUENCE.getAndIncrement();
public long sequence() {
return sequence;
}
}
- 其实这个类没什么具体的功能,仅仅起到一个标记作用,用来标记其子类是一个事件类,而这个事件的具体属性也都在其子类里面
拿一个具体的实现来瞅瞅
/**
* 操作客户端事件
* Operation client event.
*/
public class ClientOperationEvent extends Event {
private static final long serialVersionUID = -4582413232902517619L;
private final String clientId;
private final Service service;
public ClientOperationEvent(String clientId, Service service) {
this.clientId = clientId;
this.service = service;
}
public String getClientId() {
return clientId;
}
public Service getService() {
return service;
}
/**
* 客户端服务注册事件
* Client register service event.
*/
public static class ClientRegisterServiceEvent extends ClientOperationEvent {
private static final long serialVersionUID = 3412396514440368087L;
public ClientRegisterServiceEvent(Service service, String clientId) {
super(clientId, service);
}
}
/**
* 客户端注销服务事件
* Client deregister service event.
*/
public static class ClientDeregisterServiceEvent extends ClientOperationEvent {
private static final long serialVersionUID = -4518919987813223120L;
public ClientDeregisterServiceEvent(Service service, String clientId) {
super(clientId, service);
}
}
/**
* 客户端订阅服务事件
* Client subscribe service event.
*/
public static class ClientSubscribeServiceEvent extends ClientOperationEvent {
private static final long serialVersionUID = -4518919987813223120L;
public ClientSubscribeServiceEvent(Service service, String clientId) {
super(clientId, service);
}
}
/**
* 客户端取消订阅服务事件
* Client unsubscribe service event.
*/
public static class ClientUnsubscribeServiceEvent extends ClientOperationEvent {
private static final long serialVersionUID = -4518919987813223120L;
public ClientUnsubscribeServiceEvent(Service service, String clientId) {
super(clientId, service);
}
}
}
- 我们可以看到的是,这个
**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就会原子增加,可以看下面这段代码
不过有个小问题,如果达到long的最大值的话(9223372036854775807)会不会直接事件通知就down了?public static void main(String[] args) {
for(int i=0;i<10;i++){
ClientOperationEvent clientOperationEvent = new ClientOperationEvent(null, null);
System.out.print(clientOperationEvent.sequence());
}
}
=================================================
0123456789
当然,既然唠嗑的是一个事件,如果看了之前的几篇文章自然就会想到,订阅者Subscriber,毕竟事件的落地还是订阅者
这里还是贴上上篇订阅者Subscriber里比较熟悉的代码
@Component
public class ClientServiceIndexesManager extends SmartSubscriber {
@Override
public List<Class<? extends Event>> subscribeTypes() {
List<Class<? extends Event>> result = new LinkedList<>();
result.add(ClientOperationEvent.ClientRegisterServiceEvent.class);
result.add(ClientOperationEvent.ClientDeregisterServiceEvent.class);
result.add(ClientOperationEvent.ClientSubscribeServiceEvent.class);
result.add(ClientOperationEvent.ClientUnsubscribeServiceEvent.class);
result.add(ClientEvent.ClientDisconnectEvent.class);
return result;
}
@Override
public void onEvent(Event event) {
if (event instanceof ClientEvent.ClientDisconnectEvent) {
handleClientDisconnect((ClientEvent.ClientDisconnectEvent) event);
} else if (event instanceof ClientOperationEvent) {
handleClientOperation((ClientOperationEvent) event);
}
}
}
- 可以看到,在
**subscribeTypes()**
方法里,这个订阅者订阅了我们的**ClientOperationEvent**
事件以及其子类们(该方法调用处获取的是订阅的类型),当事件发布者调用了订阅者的onEvent方法的时候,假设当前事件是 ClientRegisterServiceEvent 那么就会去执行handleClientOperation 方法来做客户端注册操作
当然,顺便贴一下发布这个注册事件的代码:
NotifyCenter.publishEvent(new ClientOperationEvent.ClientRegisterServiceEvent(singleton, clientId));
至此,Event的细节已经剖析的差不多了