Giriş
Şu satırı dahil ederiz
import com.google.common.eventbus.AsyncEventBus;import com.google.common.eventbus.EventBus;import com.google.common.eventbus.Subscribe;import java.util.concurrent.Executors;
EventBus ile aynı şeydir. Aboneleri asenkron olarak tetikler.
Bi-Directional EventBus
Bu birazcık uğraşarak mümkün.
Örnek
Elimizde şöyle bir kod olsun
public class AsyncCallDataBean<T> {private T response = null;private CountDownLatch latch = new CountDownLatch(1);public T getReponse(long timeoutMS) {try {latch.await(timeoutMS, TimeUnit.MILLISECONDS);} catch (InterruptedException e) {e.printStackTrace();}return response;}public void setResponse (T response) {this.response = response;latch.countDown();}}public class RequestResponseDataBean extends AsyncCallDataBean<String>{private Object payLoad;public RequestResponseDataBean (Object payLoad) {this.payLoad = payLoad;}public Object getPayLoad() {return payLoad;}}
Kullanmak için şöyle yaparız
public class EventBusService {private static EventBusService instance = new EventBusService();public static EventBusService $() {return instance;}private EventBus eventBus = null;private EventBusService() {eventBus = new AsyncEventBus(Executors.newCachedThreadPool());}public void registerSubscriber(Object subscriber) {eventBus.register(subscriber);}public void unRegisterSubscriber(Object subscriber) {eventBus.unregister(subscriber);}public void postEvent(Object e) {eventBus.post(e);}}EventBusService.$().registerSubscriber(new MyEventBusSubscriber());RequestResponseDataBean rrb = new RequestResponseDataBean("Some event payload");EventBusService.$().postEvent(rrb);long timeOut = 3000; // wait for 3 seconds before timing outString result = rrb.getReponse (timeOut);
// will unblock on time out or call to the rrb.setResponse()
constructor - Executor
Açıklaması şöyle.
Şöyle yaparız.
İmzası şöyle
Açıklaması şöyle.
Executor will be used to dispatch events.
Örnek
Şöyle yaparız.EventBus eventBus = new AsyncEventBus(MoreExecutors.newDirectExecutorService());
Örnek
Şöyle yaparız
EventBus eventBus = new AsyncEventBus(Executors.newCachedThreadPool());
constructor - Executor + SubscriberExceptionHandler
public class PausableAsyncEventBus extends AsyncEventBus {
public PausableAsyncEventBus(Executor executor,
SubscriberExceptionHandler subscriberExceptionHandler) {
super(executor, subscriberExceptionHandler);
}
...
}
post metoduİmzası şöyle
public void post(Object event);
Örnek
Şöyle yaparız.TargetedDialogStateWrapper wrapper = ...;
clientServerEventBus.post(wrapper);
register metodu
Örnek
Şöyle yaparız
//create an asynch bus in new ThreadEventBus eventBus = new AsyncEventBus(Executors.newCachedThreadPool());class Transaction {...}class Receipt {...}//Listenerclass PaymentService {@Subscribeprivate void debitBuyer(Transaction transaction) {...}@Subscribeprivate void creditSeller(Transaction transaction) {...}}//Listenerclass ReceiptSender {@Subscribepublic void sendRecieptToCustomer(Receipt receipt) {...}@Subscribepublic void sendRecieptToSeller(Receipt receipt) {...}}//registering listenerseventBus.register(new PaymentService());eventBus.register(new ReceiptSender());//doing payment proccesseventBus.post(new Transaction("Buy a ticket", 100D));//sending messageeventBus.post(new Receipt("Successful transaction"));
Hiç yorum yok:
Yorum Gönder