use of io.reactivex.netty.channel.SingleNioLoopProvider in project WSPerfLab by Netflix-Skunkworks.
the class StartServer method main.
public static void main(String[] args) {
Clock.disableSystemTimeCalls();
int eventLoops = Runtime.getRuntime().availableProcessors();
int port = 8888;
String backendHost = "127.0.0.1";
int backendPort = 8989;
if (args.length == 0) {
// use defaults
} else if (args.length == 4) {
eventLoops = Integer.parseInt(args[0]);
port = Integer.parseInt(args[1]);
backendHost = args[2];
backendPort = Integer.parseInt(args[3]);
} else {
System.err.println("Execute with either no argument (for defaults) or 4 arguments: EVENTLOOPS, PORT, BACKEND_HOST, BACKEND_PORT");
System.exit(-1);
}
System.out.println(String.format("Using eventloops: %d port: %d backend host: %s backend port: %d", eventLoops, port, backendHost, backendPort));
route = new TestRouteBasic(backendHost, backendPort);
routeHello = new TestRouteHello();
SingleNioLoopProvider provider = new SingleNioLoopProvider(eventLoops);
RxNetty.useEventLoopProvider(provider);
System.out.println("Starting service on port " + port + " with backend at " + backendHost + ':' + backendPort + " ...");
startMonitoring();
RxNetty.<ByteBuf, ByteBuf>newHttpServerBuilder(port, (request, response) -> {
try {
if (request.getUri().startsWith("/hello")) {
return routeHello.handle(request, response);
}
long startTime = System.currentTimeMillis();
counter.increment(CounterEvent.REQUESTS);
return route.handle(request, response).doOnCompleted(() -> {
counter.increment(CounterEvent.SUCCESS);
latency.addValue((int) (System.currentTimeMillis() - startTime));
}).onErrorResumeNext(t -> {
if (t instanceof PoolExhaustedException) {
counter.increment(CounterEvent.CLIENT_POOL_EXHAUSTION);
} else if (t instanceof SocketException) {
counter.increment(CounterEvent.SOCKET_EXCEPTION);
} else if (t instanceof IOException) {
counter.increment(CounterEvent.IO_EXCEPTION);
} else if (t instanceof CancellationException) {
counter.increment(CounterEvent.CANCELLATION_EXCEPTION);
} else if (t instanceof JsonParseException) {
counter.increment(CounterEvent.PARSING_EXCEPTION);
} else {
counter.increment(CounterEvent.NETTY_ERROR);
}
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
response.writeString("");
return response.close(false);
});
} catch (Throwable e) {
System.err.println("Server => Error [" + request.getPath() + "] => " + e);
counter.increment(CounterEvent.NETTY_ERROR);
response.setStatus(HttpResponseStatus.BAD_REQUEST);
return response.writeStringAndFlush("Error 400: Bad Request\n" + e.getMessage() + '\n');
}
}).eventLoops(new NioEventLoopGroup(1), provider.globalServerEventLoop()).build().withErrorHandler(throwable -> Observable.empty()).withErrorResponseGenerator((response, error) -> System.err.println("Error: " + error.getMessage())).startAndWait();
}
Aggregations