Disruptor技术调研之配置EventHandler执行顺序

1、handlerA必须要在handlerB前执行

dw.handleEventsWith(A).then(B);


2、handlerA必须在handlerB前执行

dw.after(A).handleEventsWith(B);


3、举例:注册handler1,handler2,并且配置handlerWithBarrier在handler1,handler2执行完成后执行

disruptor.handleEventsWith(handler1, handler2);
disruptor.after(handler1, handler2).handleEventsWith(handlerWithBarrier);


4、举例:注册handler1,handler2,并且配置handlerWithBarrier在handler1,handler2执行完成后执行。handler1和handler2可以通过and注册到同一个group

disruptor.handleEventsWith(handler1);
final EventHandlerGroup<TestEvent> handler2Group = disruptor.handleEventsWith(handler2);
disruptor.after(handler1).and(handler2Group).handleEventsWith(handlerWithBarrier);


5、举例:调用after配置执行顺序时,需要先通过handleEventsWith注册到Disruptor

@Test(expected = IllegalArgumentException.class)

public void shouldThrowExceptionIfHandlerIsNotAlreadyConsuming()
    throws Exception
{
    disruptor.after(createDelayedEventHandler()).handleEventsWith(createDelayedEventHandler());

}


6、举例:EvilEqualsEventHandler重写了equals和hashCode方法,下述的handler1和handler2具有相同的equals和hashCode返回值。但是Disruptor内部的handlers的注册时通过IdentityHashMap存储的,也就是根据对象的引用是否相等来判断是否已注册,而非对象是否相等。

@Test(expected = IllegalArgumentException.class)
public void shouldTrackEventHandlersByIdentityNotEquality()
    throws Exception
{
    EvilEqualsEventHandler handler1 = new EvilEqualsEventHandler();
    EvilEqualsEventHandler handler2 = new EvilEqualsEventHandler();

    disruptor.handleEventsWith(handler1);
    disruptor.after(handler2);

}

--->>

java.util.IdentityHashMap<K, V>

This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).) 

大致的意思是该类也是实现了Map接口,但是比较键值时使用reference-equality替代了object-equality比较的方法。只有当k1==k2才被认为是相同键值。普通的实现了Map接口的键值比较相同时是通过k1==null ? k2==null : k1.equals(k2)来确认的。

转载于:https://my.oschina.net/u/914290/blog/668590

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值