17 Ocak 2019 Perşembe

AsyncEventBus Sınıfı

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 out
String result = rrb.getReponse (timeOut);
// will unblock on time out or call to the rrb.setResponse()
constructor - Executor
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 
Şöyle yaparız.
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 Thread
EventBus eventBus = new AsyncEventBus(Executors.newCachedThreadPool());

class Transaction {...}

class Receipt {...}
//Listener
class PaymentService {
  @Subscribe
  private void debitBuyer(Transaction transaction) {
    ...
  }
  @Subscribe
  private void creditSeller(Transaction transaction) {
    ...
  }
}
//Listener
class ReceiptSender {
  @Subscribe
  public void sendRecieptToCustomer(Receipt receipt) {
    ...
  }
  @Subscribe
  public void sendRecieptToSeller(Receipt receipt) {
    ...
  }
}

//registering listeners
eventBus.register(new PaymentService());
eventBus.register(new ReceiptSender());

//doing payment proccess
eventBus.post(new Transaction("Buy a ticket", 100D));
//sending message
eventBus.post(new Receipt("Successful transaction"));

Hiç yorum yok:

Yorum Gönder