在Google Guava 10版本引入了EventBus, 它主要用来简化我们处理生产/消费者编程模型. 
基本用法 
使用Guava之后, 如果要订阅消息, 就不用再继承指定的接口, 只需要在指定的方法上加上@Subscribe注解即可: 
 
 
  
1. public class
2.    
3. public int lastMessage = 0;  
4.    
5. @Subscribe
6. public void
7.         lastMessage = event.getMessage();  
8.     }  
9.    
10. public int
11. return
12.     }  
13. } 
 
上面的lastMessage用来接收消息. 
下面定义的类用来对消息进行封装: 
 
 
1. public class
2.    
3. private final int
4.    
5. public OurTestEvent(int
6. this.message = message;  
7.     }  
8.    
9. public int
10. return
11.     }  
12. } 
 
通过写一个测试来了解EventBus如何工作:
 
1. @Test
2. public void shouldReceiveEvent() throws
3.    
4. // given
5. new EventBus("test");  
6. new
7.    
8.     eventBus.register(listener);  
9.    
10. // when
11. new OurTestEvent(200));  
12.    
13. // then
14. 200);  
15. } 
 
上面的测试是不是很简单? 
MultiListener的使用 
只需要在要订阅消息的方法上加上@Subscribe注解即可实现对多个消息的订阅: 
 
 
  
1. public class
2.    
3. public
4. public
5.    
6. @Subscribe
7. public void
8.         lastInteger = event;  
9.     }  
10.    
11. @Subscribe
12. public void
13.         lastLong = event;  
14.     }  
15.    
16. public
17. return
18.     }  
19.    
20. public
21. return
22.     }  
23. } 
 
下面是对应的测试:
 
 
@Test
public void shouldReceiveMultipleEvents() throws
   
// given
new EventBus("test");  
new
   
    eventBus.register(multiListener);  
   
// when
new Integer(100));  
new Long(800));  
   
// then
100);  
    assertThat(multiListener.getLastLong()).isEqualTo(800L);  
} 
 
高级用法 
1.Dead Event 
如果EventBus发送的消息都不是订阅者关心的称之为Dead Event. 看下面的例子: 
 
 
1. /**
2.  * Listener waiting for the event that any message was posted but not delivered to anyone
3.  */
4. public class
5.    
6. boolean notDelivered = false;  
7.    
8. @Subscribe
9. public void
10. true;  
11.     }  
12.    
13. public boolean
14. return
15.     }  
16. }
 
下面是测试类:
 
 
1. @Test
2. public void shouldDetectEventWithoutListeners() throws
3.    
4. // given
5. new EventBus("test");  
6.    
7. new
8.     eventBus.register(deadEventListener);  
9.    
10. // when
11. new OurTestEvent(200));  
12.    
13.     assertThat(deadEventListener.isNotDelivered()).isTrue();  
14. } 
 
如果没有消息订阅者监听消息, EventBus将发送DeadEvent消息, 这时我们可以通过log的方式来记录这种状态. 
2.Event的继承 
如果Listener A监听Event A, 而Event A有一个子类Event B, 此时Listener A将同时接收Event A和B消息 
看下面的例子: 
 
 
1. public class
2.    
3. private
4.    
5. @Subscribe
6. public void
7.         lastMessage = integer;  
8.     }  
9.    
10. public
11. return
12.     }  
13. }  
14.   
15.       
16. public class
17.    
18. private
19.    
20. @Subscribe
21. public void
22.         lastMessage = integer;  
23.     }  
24.    
25. public
26. return
27.     }  
28. }
 
对应的测试类: 
 
 
1. @Test
2. public void shouldGetEventsFromSubclass() throws
3.    
4. // given
5. new EventBus("test");  
6. new
7. new
8.     eventBus.register(integerListener);  
9.     eventBus.register(numberListener);  
10.    
11. // when
12. new Integer(100));  
13.    
14. // then
15. 100);  
16. 100);  
17.    
18. //when
19. new
20.    
21. // then
22. // this one should has the old value as it listens only for Integers
23. 100);  
24.     assertThat(numberListener.getLastMessage()).isEqualTo(200L);  
25. }
 










