Esempio n. 1
0
  /** terminate this context */
  public synchronized void term() {
    clientScheduleService.stop();

    for (IConnection conn : connections.values()) {
      conn.close();
    }

    connections = null;

    // we need to release resources associated with client channel factory
    clientChannelFactory.releaseExternalResources();
  }
Esempio n. 2
0
  private void internalConnect(
      final Handler<ClientConnection> connectHandler, final long contextID) {

    if (bootstrap == null) {
      channelFactory =
          new NioClientSocketChannelFactory(
              VertxInternal.instance.getAcceptorPool(), VertxInternal.instance.getWorkerPool());
      bootstrap = new ClientBootstrap(channelFactory);

      checkSSL();

      bootstrap.setPipelineFactory(
          new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
              ChannelPipeline pipeline = Channels.pipeline();
              if (ssl) {
                SSLEngine engine = context.createSSLEngine();
                engine.setUseClientMode(true); // We are on the client side of the connection
                pipeline.addLast("ssl", new SslHandler(engine));
              }
              pipeline.addLast("encoder", new HttpRequestEncoder());
              pipeline.addLast("decoder", new HttpResponseDecoder());
              pipeline.addLast("handler", new ClientHandler());
              return pipeline;
            }
          });
    }

    // Client connections share context with caller
    channelFactory.setWorker(VertxInternal.instance.getWorkerForContextID(contextID));
    bootstrap.setOptions(connectionOptions);
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    future.addListener(
        new ChannelFutureListener() {
          public void operationComplete(ChannelFuture channelFuture) throws Exception {
            if (channelFuture.isSuccess()) {

              final NioSocketChannel ch = (NioSocketChannel) channelFuture.getChannel();

              runOnCorrectThread(
                  ch,
                  new Runnable() {
                    public void run() {
                      final ClientConnection conn =
                          new ClientConnection(
                              HttpClient.this,
                              ch,
                              host + ":" + port,
                              ssl,
                              keepAlive,
                              contextID,
                              Thread.currentThread());
                      conn.closedHandler(
                          new SimpleHandler() {
                            public void handle() {
                              pool.connectionClosed();
                            }
                          });
                      connectionMap.put(ch, conn);
                      VertxInternal.instance.setContextID(contextID);
                      connectHandler.handle(conn);
                    }
                  });
            } else {
              Throwable t = channelFuture.getCause();
              if (t instanceof Exception && exceptionHandler != null) {
                exceptionHandler.handle((Exception) t);
              } else {
                log.error("Unhandled exception", t);
              }
            }
          }
        });
  }
 @Override
 public void shutdown() {
   socketChannelFactory.releaseExternalResources();
 }