예제 #1
0
  public MicroservicesRunner() {

    TransportsConfiguration trpConfig = TransportConfigurationBuilder.build();
    Set<ListenerConfiguration> listenerConfigurations = trpConfig.getListenerConfigurations();
    for (ListenerConfiguration listenerConfiguration : listenerConfigurations) {
      NettyListener listener = new NettyListener(listenerConfiguration);
      transportManager.registerTransport(listener);
    }

    NettyTransportDataHolder nettyTransportDataHolder = NettyTransportDataHolder.getInstance();
    nettyTransportDataHolder.addNettyChannelInitializer(
        ListenerConfiguration.DEFAULT_KEY, new MSSNettyServerInitializer());
  }
  /** {@inheritDoc} */
  @Override
  public void ignite(
      String host,
      int port,
      SslStores sslStores,
      CountDownLatch latch,
      int maxThreads,
      int minThreads,
      int threadIdleTimeoutMillis,
      Map<String, Class<?>> webSocketHandlers,
      Optional<Integer> webSocketIdleTimeoutMillis) {

    //        if (port == 0) {
    //            try (ServerSocket s = new ServerSocket(0)) {
    //                port = s.getLocalPort();
    //            } catch (IOException e) {
    //                logger.error("Could not get first available port (port set to 0), using
    // default: {}", SPARK_DEFAULT_PORT);
    //                port = SPARK_DEFAULT_PORT;
    //            }
    //        }
    //
    //        server = JettyServerFactory.createServer(maxThreads, minThreads,
    // threadIdleTimeoutMillis);
    //
    //        ServerConnector connector;
    //
    //        if (sslStores == null) {
    //            connector = SocketConnectorFactory.createSocketConnector(server, host, port);
    //        } else {
    //            connector = SocketConnectorFactory.createSecureSocketConnector(server, host, port,
    // sslStores);
    //        }
    //
    //        server = connector.getServer();
    //        server.setConnectors(new Connector[] {connector});
    //
    //        ServletContextHandler webSocketServletContextHandler =
    //                WebSocketServletContextHandlerFactory.create(webSocketHandlers,
    // webSocketIdleTimeoutMillis);
    //
    //        // Handle web socket routes
    //        if (webSocketServletContextHandler == null) {
    //            server.setHandler(handler);
    //        } else {
    //            List<Handler> handlersInList = new ArrayList<>();
    //            handlersInList.add(handler);
    //
    //            // WebSocket handler must be the last one
    //            if (webSocketServletContextHandler != null) {
    //                handlersInList.add(webSocketServletContextHandler);
    //            }
    //
    //            HandlerList handlers = new HandlerList();
    //            handlers.setHandlers(handlersInList.toArray(new Handler[handlersInList.size()]));
    //            server.setHandler(handlers);
    //        }
    //
    //        try {
    //            logger.info("== {} has ignited ...", NAME);
    //            logger.info(">> Listening on {}:{}", host, port);
    //
    //            server.start();
    //            latch.countDown();
    //            server.join();
    //        } catch (Exception e) {
    //            logger.error("ignite failed", e);
    //            System.exit(100); // NOSONAR
    //        }

    NettyTransportDataHolder nettyTransportDataHolder = NettyTransportDataHolder.getInstance();
    ListenerConfiguration listenerConfiguration =
        new ListenerConfiguration("netty-" + port, "0.0.0.0", port);
    NettyListener listener = new NettyListener(listenerConfiguration);
    transportManager.registerTransport(listener);
    nettyTransportDataHolder.addNettyChannelInitializer(
        listenerConfiguration.getId(),
        new CarbonNettyServerInitializer() {
          ServletBridgeChannelPipelineFactory servletBridgeChannelPipelineFactory;

          @Override
          public void setup(Map<String, String> map) {
            servletBridgeChannelPipelineFactory =
                new ServletBridgeChannelPipelineFactory(
                    new WebappConfiguration()
                        .addHttpServlet(
                            new HttpServlet() {
                              @Override
                              protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                                  throws ServletException, IOException {
                                handler.doHandle(null, req, resp);
                              }
                            }));
          }

          @Override
          public void initChannel(SocketChannel socketChannel) {
            try {
              servletBridgeChannelPipelineFactory.initChannel(socketChannel);
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });
    transportManager.startTransports();
  }