org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel Java Examples

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel. 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: HttpTestClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a client instance for the server at the target host and port.
 *
 * @param host Host of the HTTP server
 * @param port Port of the HTTP server
 */
public HttpTestClient(String host, int port) {
	this.host = host;
	this.port = port;

	this.group = new NioEventLoopGroup();

	this.bootstrap = new Bootstrap();
	this.bootstrap.group(group)
			.channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {

				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ChannelPipeline p = ch.pipeline();
					p.addLast(new HttpClientCodec());
					p.addLast(new HttpContentDecompressor());
					p.addLast(new ClientHandler(responses));
				}
			});
}
 
Example #2
Source File: ClientTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private Channel createServerChannel(final ChannelHandler... handlers) throws UnknownHostException, InterruptedException {
	ServerBootstrap bootstrap = new ServerBootstrap()
			// Bind address and port
			.localAddress(InetAddress.getLocalHost(), 0)
			// NIO server channels
			.group(nioGroup)
			.channel(NioServerSocketChannel.class)
			// See initializer for pipeline details
			.childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline()
							.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
							.addLast(handlers);
				}
			});

	return bootstrap.bind().sync().channel();
}
 
Example #3
Source File: ClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private Channel createServerChannel(final ChannelHandler... handlers) throws UnknownHostException, InterruptedException {
	ServerBootstrap bootstrap = new ServerBootstrap()
			// Bind address and port
			.localAddress(InetAddress.getLocalHost(), 0)
			// NIO server channels
			.group(nioGroup)
			.channel(NioServerSocketChannel.class)
			// See initializer for pipeline details
			.childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline()
							.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
							.addLast(handlers);
				}
			});

	return bootstrap.bind().sync().channel();
}
 
Example #4
Source File: HttpTestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a client instance for the server at the target host and port.
 *
 * @param host Host of the HTTP server
 * @param port Port of the HTTP server
 */
public HttpTestClient(String host, int port) {
	this.host = host;
	this.port = port;

	this.group = new NioEventLoopGroup();

	this.bootstrap = new Bootstrap();
	this.bootstrap.group(group)
			.channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {

				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ChannelPipeline p = ch.pipeline();
					p.addLast(new HttpClientCodec());
					p.addLast(new HttpContentDecompressor());
					p.addLast(new ClientHandler(responses));
				}
			});
}
 
Example #5
Source File: HttpTestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a client instance for the server at the target host and port.
 *
 * @param host Host of the HTTP server
 * @param port Port of the HTTP server
 */
public HttpTestClient(String host, int port) {
	this.host = host;
	this.port = port;

	this.group = new NioEventLoopGroup();

	this.bootstrap = new Bootstrap();
	this.bootstrap.group(group)
			.channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {

				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ChannelPipeline p = ch.pipeline();
					p.addLast(new HttpClientCodec());
					p.addLast(new HttpContentDecompressor());
					p.addLast(new ClientHandler(responses));
				}
			});
}
 
Example #6
Source File: ClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private Channel createServerChannel(final ChannelHandler... handlers) throws UnknownHostException, InterruptedException {
	ServerBootstrap bootstrap = new ServerBootstrap()
			// Bind address and port
			.localAddress(InetAddress.getLocalHost(), 0)
			// NIO server channels
			.group(nioGroup)
			.channel(NioServerSocketChannel.class)
			// See initializer for pipeline details
			.childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline()
							.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
							.addLast(handlers);
				}
			});

	return bootstrap.bind().sync().channel();
}
 
Example #7
Source File: AbstractServerBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void initChannel(SocketChannel channel) throws Exception {
	channel.pipeline()
			.addLast(new ChunkedWriteHandler())
			.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
			.addLast(sharedRequestHandler);
}
 
Example #8
Source File: KvStateServerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a client bootstrap.
 */
private Bootstrap createBootstrap(final ChannelHandler... handlers) {
	return new Bootstrap().group(NIO_GROUP).channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(handlers);
				}
			});
}
 
Example #9
Source File: NettyClientServerSslTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initChannel(SocketChannel channel) throws Exception {
	super.initChannel(channel);

	SslHandler sslHandler = (SslHandler) channel.pipeline().get("ssl");
	assertNotNull(sslHandler);
	serverHandler[0] = sslHandler;

	latch.trigger();
}
 
Example #10
Source File: NettyServer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initChannel(SocketChannel channel) throws Exception {
	if (sslHandlerFactory != null) {
		channel.pipeline().addLast("ssl",
			sslHandlerFactory.createNettySSLHandler(channel.alloc()));
	}

	channel.pipeline().addLast(protocol.getServerChannelHandlers());
}
 
Example #11
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
public RestClient(RestClientConfiguration configuration, Executor executor) {
	Preconditions.checkNotNull(configuration);
	this.executor = Preconditions.checkNotNull(executor);
	this.terminationFuture = new CompletableFuture<>();

	final SSLHandlerFactory sslHandlerFactory = configuration.getSslHandlerFactory();
	ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {
		@Override
		protected void initChannel(SocketChannel socketChannel) {
			try {
				// SSL should be the first handler in the pipeline
				if (sslHandlerFactory != null) {
					socketChannel.pipeline().addLast("ssl", sslHandlerFactory.createNettySSLHandler(socketChannel.alloc()));
				}

				socketChannel.pipeline()
					.addLast(new HttpClientCodec())
					.addLast(new HttpObjectAggregator(configuration.getMaxContentLength()))
					.addLast(new ChunkedWriteHandler()) // required for multipart-requests
					.addLast(new IdleStateHandler(configuration.getIdlenessTimeout(), configuration.getIdlenessTimeout(), configuration.getIdlenessTimeout(), TimeUnit.MILLISECONDS))
					.addLast(new ClientHandler());
			} catch (Throwable t) {
				t.printStackTrace();
				ExceptionUtils.rethrow(t);
			}
		}
	};
	NioEventLoopGroup group = new NioEventLoopGroup(1, new ExecutorThreadFactory("flink-rest-client-netty"));

	bootstrap = new Bootstrap();
	bootstrap
		.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(configuration.getConnectionTimeout()))
		.group(group)
		.channel(NioSocketChannel.class)
		.handler(initializer);

	LOG.debug("Rest client endpoint started.");
}
 
Example #12
Source File: AbstractServerBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void initChannel(SocketChannel channel) throws Exception {
	channel.pipeline()
			.addLast(new ChunkedWriteHandler())
			.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
			.addLast(sharedRequestHandler);
}
 
Example #13
Source File: KvStateServerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a client bootstrap.
 */
private Bootstrap createBootstrap(final ChannelHandler... handlers) {
	return new Bootstrap().group(NIO_GROUP).channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(handlers);
				}
			});
}
 
Example #14
Source File: NettyClientServerSslTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initChannel(SocketChannel channel) throws Exception {
	super.initChannel(channel);

	SslHandler sslHandler = (SslHandler) channel.pipeline().get("ssl");
	assertNotNull(sslHandler);
	serverHandler[0] = sslHandler;

	latch.trigger();
}
 
Example #15
Source File: NettyServer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void initChannel(SocketChannel channel) throws Exception {
	if (sslHandlerFactory != null) {
		channel.pipeline().addLast("ssl",
			sslHandlerFactory.createNettySSLHandler(channel.alloc()));
	}

	channel.pipeline().addLast(protocol.getServerChannelHandlers());
}
 
Example #16
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
public RestClient(RestClientConfiguration configuration, Executor executor) {
	Preconditions.checkNotNull(configuration);
	this.executor = Preconditions.checkNotNull(executor);
	this.terminationFuture = new CompletableFuture<>();

	final SSLHandlerFactory sslHandlerFactory = configuration.getSslHandlerFactory();
	ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {
		@Override
		protected void initChannel(SocketChannel socketChannel) {
			try {
				// SSL should be the first handler in the pipeline
				if (sslHandlerFactory != null) {
					socketChannel.pipeline().addLast("ssl", sslHandlerFactory.createNettySSLHandler(socketChannel.alloc()));
				}

				socketChannel.pipeline()
					.addLast(new HttpClientCodec())
					.addLast(new HttpObjectAggregator(configuration.getMaxContentLength()))
					.addLast(new ChunkedWriteHandler()) // required for multipart-requests
					.addLast(new IdleStateHandler(configuration.getIdlenessTimeout(), configuration.getIdlenessTimeout(), configuration.getIdlenessTimeout(), TimeUnit.MILLISECONDS))
					.addLast(new ClientHandler());
			} catch (Throwable t) {
				t.printStackTrace();
				ExceptionUtils.rethrow(t);
			}
		}
	};
	NioEventLoopGroup group = new NioEventLoopGroup(1, new ExecutorThreadFactory("flink-rest-client-netty"));

	bootstrap = new Bootstrap();
	bootstrap
		.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(configuration.getConnectionTimeout()))
		.group(group)
		.channel(NioSocketChannel.class)
		.handler(initializer);

	LOG.info("Rest client endpoint started.");
}
 
Example #17
Source File: AbstractServerBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void initChannel(SocketChannel channel) throws Exception {
	channel.pipeline()
			.addLast(new ChunkedWriteHandler())
			.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
			.addLast(sharedRequestHandler);
}
 
Example #18
Source File: KvStateServerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a client bootstrap.
 */
private Bootstrap createBootstrap(final ChannelHandler... handlers) {
	return new Bootstrap().group(NIO_GROUP).channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(handlers);
				}
			});
}
 
Example #19
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public RestClient(RestClientConfiguration configuration, Executor executor) {
	Preconditions.checkNotNull(configuration);
	this.executor = Preconditions.checkNotNull(executor);
	this.terminationFuture = new CompletableFuture<>();

	final SSLHandlerFactory sslHandlerFactory = configuration.getSslHandlerFactory();
	ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {
		@Override
		protected void initChannel(SocketChannel socketChannel) {
			try {
				// SSL should be the first handler in the pipeline
				if (sslHandlerFactory != null) {
					socketChannel.pipeline().addLast("ssl", sslHandlerFactory.createNettySSLHandler());
				}

				socketChannel.pipeline()
					.addLast(new HttpClientCodec())
					.addLast(new HttpObjectAggregator(configuration.getMaxContentLength()))
					.addLast(new ChunkedWriteHandler()) // required for multipart-requests
					.addLast(new IdleStateHandler(configuration.getIdlenessTimeout(), configuration.getIdlenessTimeout(), configuration.getIdlenessTimeout(), TimeUnit.MILLISECONDS))
					.addLast(new ClientHandler());
			} catch (Throwable t) {
				t.printStackTrace();
				ExceptionUtils.rethrow(t);
			}
		}
	};
	NioEventLoopGroup group = new NioEventLoopGroup(1, new ExecutorThreadFactory("flink-rest-client-netty"));

	bootstrap = new Bootstrap();
	bootstrap
		.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(configuration.getConnectionTimeout()))
		.group(group)
		.channel(NioSocketChannel.class)
		.handler(initializer);

	LOG.info("Rest client endpoint started.");
}
 
Example #20
Source File: NettyClient.java    From flink with Apache License 2.0 4 votes vote down vote up
ChannelFuture connect(final InetSocketAddress serverSocketAddress) {
	checkState(bootstrap != null, "Client has not been initialized yet.");

	// --------------------------------------------------------------------
	// Child channel pipeline for accepted connections
	// --------------------------------------------------------------------

	bootstrap.handler(new ChannelInitializer<SocketChannel>() {
		@Override
		public void initChannel(SocketChannel channel) throws Exception {

			// SSL handler should be added first in the pipeline
			if (clientSSLFactory != null) {
				SslHandler sslHandler = clientSSLFactory.createNettySSLHandler(
						channel.alloc(),
						serverSocketAddress.getAddress().getCanonicalHostName(),
						serverSocketAddress.getPort());
				channel.pipeline().addLast("ssl", sslHandler);
			}
			channel.pipeline().addLast(protocol.getClientChannelHandlers());
		}
	});

	try {
		return bootstrap.connect(serverSocketAddress);
	}
	catch (ChannelException e) {
		if ((e.getCause() instanceof java.net.SocketException &&
				e.getCause().getMessage().equals("Too many open files")) ||
			(e.getCause() instanceof ChannelException &&
					e.getCause().getCause() instanceof java.net.SocketException &&
					e.getCause().getCause().getMessage().equals("Too many open files")))
		{
			throw new ChannelException(
					"The operating system does not offer enough file handles to open the network connection. " +
							"Please increase the number of available file handles.", e.getCause());
		}
		else {
			throw e;
		}
	}
}
 
Example #21
Source File: NettyClient.java    From flink with Apache License 2.0 4 votes vote down vote up
ChannelFuture connect(final InetSocketAddress serverSocketAddress) {
	checkState(bootstrap != null, "Client has not been initialized yet.");

	// --------------------------------------------------------------------
	// Child channel pipeline for accepted connections
	// --------------------------------------------------------------------

	bootstrap.handler(new ChannelInitializer<SocketChannel>() {
		@Override
		public void initChannel(SocketChannel channel) throws Exception {

			// SSL handler should be added first in the pipeline
			if (clientSSLFactory != null) {
				SslHandler sslHandler = clientSSLFactory.createNettySSLHandler(
						channel.alloc(),
						serverSocketAddress.getAddress().getCanonicalHostName(),
						serverSocketAddress.getPort());
				channel.pipeline().addLast("ssl", sslHandler);
			}
			channel.pipeline().addLast(protocol.getClientChannelHandlers());
		}
	});

	try {
		return bootstrap.connect(serverSocketAddress);
	}
	catch (ChannelException e) {
		if ((e.getCause() instanceof java.net.SocketException &&
				e.getCause().getMessage().equals("Too many open files")) ||
			(e.getCause() instanceof ChannelException &&
					e.getCause().getCause() instanceof java.net.SocketException &&
					e.getCause().getCause().getMessage().equals("Too many open files")))
		{
			throw new ChannelException(
					"The operating system does not offer enough file handles to open the network connection. " +
							"Please increase the number of available file handles.", e.getCause());
		}
		else {
			throw e;
		}
	}
}
 
Example #22
Source File: NettyClient.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
ChannelFuture connect(final InetSocketAddress serverSocketAddress) {
	checkState(bootstrap != null, "Client has not been initialized yet.");

	// --------------------------------------------------------------------
	// Child channel pipeline for accepted connections
	// --------------------------------------------------------------------

	bootstrap.handler(new ChannelInitializer<SocketChannel>() {
		@Override
		public void initChannel(SocketChannel channel) throws Exception {

			// SSL handler should be added first in the pipeline
			if (clientSSLFactory != null) {
				SslHandler sslHandler = clientSSLFactory.createNettySSLHandler(
						serverSocketAddress.getAddress().getCanonicalHostName(),
						serverSocketAddress.getPort());
				channel.pipeline().addLast("ssl", sslHandler);
			}
			channel.pipeline().addLast(protocol.getClientChannelHandlers());
		}
	});

	try {
		return bootstrap.connect(serverSocketAddress);
	}
	catch (ChannelException e) {
		if ((e.getCause() instanceof java.net.SocketException &&
				e.getCause().getMessage().equals("Too many open files")) ||
			(e.getCause() instanceof ChannelException &&
					e.getCause().getCause() instanceof java.net.SocketException &&
					e.getCause().getCause().getMessage().equals("Too many open files")))
		{
			throw new ChannelException(
					"The operating system does not offer enough file handles to open the network connection. " +
							"Please increase the number of available file handles.", e.getCause());
		}
		else {
			throw e;
		}
	}
}