public static void main(String[] args) throws InterruptedException {
    final TradeServer server = new TradeServer();

    // Use a Reactor to dispatch events using the default Dispatcher
    Reactor reactor = new Reactor();

    // Create a single Selector for efficiency
    Selector trade = $("trade.execute");

    // For each Trade event, execute that on the server
    reactor.on(
        trade,
        new Consumer<Event<Trade>>() {
          @Override
          public void accept(Event<Trade> tradeEvent) {
            server.execute(tradeEvent.getData());

            // Since we're async, for this test, use a latch to tell when we're done
            latch.countDown();
          }
        });

    // Start a throughput timer
    startTimer();

    // Publish one event per trade
    for (int i = 0; i < totalTrades; i++) {
      // Pull next randomly-generated Trade from server
      Trade t = server.nextTrade();

      // Notify the Reactor the event is ready to be handled
      reactor.notify(trade, Fn.event(t));
    }

    // Stop throughput timer and output metrics
    endTimer();

    server.stop();
  }
Beispiel #2
0
 /**
  * Assign an error handler to exceptions of the given type.
  *
  * @param exceptionType the type of exceptions to handle
  * @param onError the error handler for each exception
  * @param <E> type of the exception to handle
  * @return {@literal this}
  */
 public <E extends Throwable> Composable<T> when(
     @Nonnull Class<E> exceptionType, @Nonnull Consumer<E> onError) {
   this.events.on(Fn.T(exceptionType), new EventConsumer<E>(onError));
   return this;
 }