Java Code Examples for org.jboss.netty.bootstrap.ClientBootstrap#releaseExternalResources()

The following examples show how to use org.jboss.netty.bootstrap.ClientBootstrap#releaseExternalResources() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleTcpClient.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

  bootstrap.setOption("tcpNoDelay", true);
  bootstrap.setOption("keepAlive", true);

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
 
Example 2
Source File: SimpleTcpClient.java    From big-c with Apache License 2.0 6 votes vote down vote up
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

  bootstrap.setOption("tcpNoDelay", true);
  bootstrap.setOption("keepAlive", true);

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
 
Example 3
Source File: FileClient.java    From netty-file-parent with Apache License 2.0 5 votes vote down vote up
private static void uploadFile(ClientBootstrap bootstrap, String host,
		int port, File file, String fileName, String thumbMark,
		String userName, String pwd) {
	//1.构建uri对象
	URI uri = getUri(host, port);
	//2.连接netty服务端
	ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,
			port));
	//3.异步获取Channel对象
	Channel channel = future.awaitUninterruptibly().getChannel();
	if (!future.isSuccess()) {
		future.getCause().printStackTrace();
		bootstrap.releaseExternalResources();
		return;
	}
	//4.初始化文件上传句柄对象
	WrapFileClientHandler handler = new UploadFileClientHandler(host, uri,
			file, fileName, thumbMark, userName, pwd);
	//5.获取Request对象
	HttpRequest request = handler.getRequest();
	//6.获取Http数据处理工厂
	HttpDataFactory factory = getHttpDataFactory();
	//7.进行数据的包装处理,主要是进行上传文件所需要的参数的设置,此时调用的句柄是具体的UploadFileClientHandler对象
	HttpPostRequestEncoder bodyRequestEncoder = handler
			.wrapRequestData(factory);
	//8.把request写到管道中,传输给服务端
	channel.write(request);
	//9.做一些关闭资源的动作
	if (bodyRequestEncoder.isChunked()) {
		channel.write(bodyRequestEncoder).awaitUninterruptibly();
	}
	bodyRequestEncoder.cleanFiles();
	channel.getCloseFuture().awaitUninterruptibly();

	bootstrap.releaseExternalResources();
	factory.cleanAllHttpDatas();
}
 
Example 4
Source File: FileClient.java    From netty-file-parent with Apache License 2.0 5 votes vote down vote up
private static void deleteFile(ClientBootstrap bootstrap, String host,
		int port, String filePath, String userName, String pwd) {
	URI uri = getUri(host, port);
	ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,
			port));
	Channel channel = future.awaitUninterruptibly().getChannel();
	if (!future.isSuccess()) {
		future.getCause().printStackTrace();
		bootstrap.releaseExternalResources();
		return;
	}

	WrapFileClientHandler handler = new DeleteFileClientHandler(host, uri,
			filePath, userName, pwd);
	HttpRequest request = handler.getRequest();
	HttpDataFactory factory = getHttpDataFactory();
	HttpPostRequestEncoder bodyRequestEncoder = handler
			.wrapRequestData(factory);
	channel.write(request);
	if (bodyRequestEncoder.isChunked()) {
		channel.write(bodyRequestEncoder).awaitUninterruptibly();
	}
	bodyRequestEncoder.cleanFiles();
	channel.getCloseFuture().awaitUninterruptibly();
	bootstrap.releaseExternalResources();
	factory.cleanAllHttpDatas();
}
 
Example 5
Source File: FileClient.java    From netty-file-parent with Apache License 2.0 5 votes vote down vote up
private static void replaceFile(ClientBootstrap bootstrap, String host,
		int port, File file, String filePath, String userName, String pwd) {
	URI uri = getUri(host, port);
	ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,
			port));
	Channel channel = future.awaitUninterruptibly().getChannel();
	if (!future.isSuccess()) {
		future.getCause().printStackTrace();
		bootstrap.releaseExternalResources();
		return;
	}

	WrapFileClientHandler handler = new ReplaceFileClientHandler(host, uri,
			filePath, file, userName, pwd);
	HttpRequest request = handler.getRequest();
	HttpDataFactory factory = getHttpDataFactory();
	HttpPostRequestEncoder bodyRequestEncoder = handler
			.wrapRequestData(factory);
	channel.write(request);
	if (bodyRequestEncoder.isChunked()) {
		channel.write(bodyRequestEncoder).awaitUninterruptibly();
	}
	bodyRequestEncoder.cleanFiles();
	channel.getCloseFuture().awaitUninterruptibly();
	bootstrap.releaseExternalResources();
	factory.cleanAllHttpDatas();
}
 
Example 6
Source File: FileClient.java    From netty-file-parent with Apache License 2.0 5 votes vote down vote up
private static void createThumbPicture(ClientBootstrap bootstrap,
		String host, int port, String filePath, String userName, String pwd) {
	URI uri = getUri(host, port);
	ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,
			port));
	Channel channel = future.awaitUninterruptibly().getChannel();
	if (!future.isSuccess()) {
		future.getCause().printStackTrace();
		bootstrap.releaseExternalResources();
		return;
	}

	WrapFileClientHandler handler = new CreateThumbPictureClientHandler(
			host, uri, userName, pwd, filePath);
	HttpRequest request = handler.getRequest();
	HttpDataFactory factory = getHttpDataFactory();
	HttpPostRequestEncoder bodyRequestEncoder = handler
			.wrapRequestData(factory);
	channel.write(request);
	if (bodyRequestEncoder.isChunked()) {
		channel.write(bodyRequestEncoder).awaitUninterruptibly();
	}
	bodyRequestEncoder.cleanFiles();
	channel.getCloseFuture().awaitUninterruptibly();
	bootstrap.releaseExternalResources();
	factory.cleanAllHttpDatas();
}
 
Example 7
Source File: NettySend.java    From jlogstash-input-plugin with Apache License 2.0 5 votes vote down vote up
public void connect(){

		bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory(
				Executors.newCachedThreadPool(),
				Executors.newCachedThreadPool()));
		bootstrap.setOption("tcpNoDelay", false);
		bootstrap.setOption("keepAlive", true);

		final NettyClientHandler handler = new NettyClientHandler(this, timer);
		
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
			
			public ChannelPipeline getPipeline() throws Exception {
				ChannelPipeline pipeline =  Channels.pipeline();
				pipeline.addLast("handler", handler);
				pipeline.addLast("encoder", new StringEncoder());
				return pipeline;
			}
		});
		
		bootstrap.setOption("remoteAddress", new InetSocketAddress(host, port));
		try {
			ChannelFuture future = bootstrap.connect().sync();
			channel = future.getChannel();
		} catch (Exception e) {
			logger.error(ExceptionUtil.getErrorMessage(e));
			bootstrap.releaseExternalResources();
			System.exit(-1);//第一次连接出现异常直接退出,不走重连
		}
	}
 
Example 8
Source File: TcpProviderPoc.java    From parallec with Apache License 2.0 4 votes vote down vote up
public void request() throws Exception {
    
    // Parse options.
    String host = "localhost";
    int port = 10081;
    int connectTimeoutMillis = 2000;
    // Configure the client.
    ClientBootstrap bootstrap = new ClientBootstrap(
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));

    final TelnetClientHandler handler = new TelnetClientHandler();

    // Configure the pipeline factory.
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024,
                    Delimiters.lineDelimiter()));
            pipeline.addLast("stringDecoder", stringDecoder);
            pipeline.addLast("stringEncoder", stringEncoder);
            pipeline.addLast("handler", handler);
            return pipeline;
        }
    });
    
    
    bootstrap.setOption("connectTimeoutMillis",connectTimeoutMillis);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", true);
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,
            port));

    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.awaitUninterruptibly().getChannel();
    // Read commands from the stdin.
    ChannelFuture lastWriteFuture = null;
    String command = "hadoopMonitorFromClient";

    // Sends the line to server.
    lastWriteFuture = channel.write(command + "\r\n");

    // Wait until all messages are flushed before closing the channel.
    if (lastWriteFuture != null) {
        lastWriteFuture.await();
    }

    // note that we need to wait for the response before

    // wait time before close the channel too early that msg will not be
    // received.

    while (!handler.channelCompleted) {
        Thread.sleep(1l);
    }

    // Close the connection. Make sure the close operation ends because
    // all I/O operations are asynchronous in Netty.
    channel.close().awaitUninterruptibly();
    // Shut down all thread pools to exit.
    bootstrap.releaseExternalResources();
    
}