/** * The last call in the building of a RestExpress server, bind() causes Netty to bind to the * listening address and process incoming messages. * * @return Channel */ public Channel bind(int port) { setPort(port); // Configure the server. if (getIoThreadCount() == 0) { bootstrap = Bootstraps.createServerNioBootstrap(); } else { bootstrap = Bootstraps.createServerNioBootstrap(getIoThreadCount()); } // Set up the event pipeline factory. DefaultRequestHandler requestHandler = new DefaultRequestHandler(createRouteResolver(), createResponseProcessorResolver()); // Add MessageObservers to the request handler here, if desired... requestHandler.addMessageObserver(messageObservers.toArray(new MessageObserver[0])); requestHandler.setExceptionMap(exceptionMap); // Add pre/post processors to the request handler here... addPreprocessors(requestHandler); addPostprocessors(requestHandler); addFinallyProcessors(requestHandler); PipelineBuilder pf = new PipelineBuilder() .addRequestHandler(requestHandler) .setMaxContentLength(serverSettings.getMaxContentSize()); if (getExecutorThreadCount() > 0) { ExecutionHandler executionHandler = new ExecutionHandler( new OrderedMemoryAwareThreadPoolExecutor(getExecutorThreadCount(), 0, 0)); pf.setExecutionHandler(executionHandler); } bootstrap.setPipelineFactory(pf); setBootstrapOptions(); // Bind and start to accept incoming connections. if (shouldUseSystemOut()) { System.out.println("Starting " + getName() + " Server on port " + port); } Channel channel = bootstrap.bind(new InetSocketAddress(port)); allChannels.add(channel); bindPlugins(); return channel; }
/** @param requestHandler */ private void addFinallyProcessors(DefaultRequestHandler requestHandler) { for (Postprocessor processor : getFinallyProcessors()) { requestHandler.addFinallyProcessor(processor); } }
/** @param requestHandler */ private void addPreprocessors(DefaultRequestHandler requestHandler) { for (Preprocessor processor : getPreprocessors()) { requestHandler.addPreprocessor(processor); } }