package guavademo.event.bus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
/**
* Created by liuguangxin on 2018/4/9.
*/
public class OrderEvent {
private String message;
public OrderEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
class EventListener {
public EventListener(){
System.out.println();
}
@Subscribe
public void listen(OrderEvent event) {
//TODO @Subscribe注解的处理逻辑在com.google.common.eventbus.EventBus.register()中被处理
System.out.println("receive msg:" + event.getMessage());
}
@Subscribe
public void listen(String event) {
System.out.println("receive msg:" + event);
}
}
class Main {
public static void main(String[] args) {
EventBus eventBus = new EventBus("daxin");
//register all subscriber
eventBus.register(new EventListener());
// 支持多个订阅者
// eventBus.register(new EventListener());
//publish event
eventBus.post(new OrderEvent("hello"));
eventBus.post(new OrderEvent("world"));
eventBus.post("String type Event");
}
}
@Subscribe注解的处理逻辑在com.google.common.eventbus.EventBus.register()中被处理