| 1 | package com.hammurapi.eventbus.tests; |
| 2 | |
| 3 | import java.util.concurrent.atomic.AtomicInteger; |
| 4 | |
| 5 | import com.hammurapi.eventbus.Handler; |
| 6 | import com.hammurapi.eventbus.local.LocalEventDispatchContext; |
| 7 | |
| 8 | public class PriorityHandler { |
| 9 | |
| 10 | private AtomicInteger hpCounter = new AtomicInteger(); |
| 11 | private AtomicInteger ahpCounter = new AtomicInteger(); |
| 12 | private AtomicInteger lpCounter = new AtomicInteger(); |
| 13 | |
| 14 | @Handler(priority=10) |
| 15 | public void highPriority(LocalEventDispatchContext<Object, Integer, Object> ctx, String event) throws InterruptedException { |
| 16 | hpCounter.incrementAndGet(); |
| 17 | Thread.sleep(500); |
| 18 | if ("Hello".equals(event)) { |
| 19 | ctx.consume(event); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | @Handler(priority=11) |
| 24 | public void anotherHighPriority(String event) { |
| 25 | ahpCounter.incrementAndGet(); |
| 26 | } |
| 27 | |
| 28 | @Handler |
| 29 | public void lowPriority(String event) { |
| 30 | lpCounter.incrementAndGet(); |
| 31 | } |
| 32 | |
| 33 | public int getHpCounter() { |
| 34 | return hpCounter.get(); |
| 35 | } |
| 36 | |
| 37 | public int getAhpCounter() { |
| 38 | return ahpCounter.get(); |
| 39 | } |
| 40 | |
| 41 | public int getLpCounter() { |
| 42 | return lpCounter.get(); |
| 43 | } |
| 44 | } |