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

一、handlerA必需要在handlerB前执行 java

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


二、handlerA必须在handlerB前执行 spa

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


三、举例:注册handler1,handler2,而且配置handlerWithBarrier在handler1,handler2执行完成后执行 对象

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


四、举例:注册handler1,handler2,而且配置handlerWithBarrier在handler1,handler2执行完成后执行。handler1和handler2能够经过and注册到同一个group hash

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


五、举例:调用after配置执行顺序时,须要先经过handleEventsWith注册到Disruptor io

@Test(expected = IllegalArgumentException.class) table

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

}


六、举例: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)来确认的。

相关文章
相关标签/搜索