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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel. These examples are extracted from open source projects. 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 Project: Flink-CEPplus   Source File: HttpTestClient.java    License: 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 Project: Flink-CEPplus   Source File: ClientTest.java    License: 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 Project: flink   Source File: HttpTestClient.java    License: 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 4
Source Project: flink   Source File: ClientTest.java    License: 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 5
Source Project: flink   Source File: HttpTestClient.java    License: 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 Project: flink   Source File: ClientTest.java    License: 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 Project: Flink-CEPplus   Source File: RestClient.java    License: 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 8
Source Project: Flink-CEPplus   Source File: KvStateServerTest.java    License: 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 Project: Flink-CEPplus   Source File: AbstractServerBase.java    License: 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 10
Source Project: flink   Source File: RestClient.java    License: 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 11
Source Project: flink   Source File: NettyServer.java    License: 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 12
Source Project: flink   Source File: NettyClientServerSslTest.java    License: 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 13
Source Project: flink   Source File: KvStateServerTest.java    License: 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 Project: flink   Source File: AbstractServerBase.java    License: 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 15
Source Project: flink   Source File: RestClient.java    License: 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 16
Source Project: flink   Source File: NettyServer.java    License: 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 17
Source Project: flink   Source File: NettyClientServerSslTest.java    License: 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 18
Source Project: flink   Source File: KvStateServerTest.java    License: 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 Project: flink   Source File: AbstractServerBase.java    License: 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 20
Source Project: Flink-CEPplus   Source File: NettyClient.java    License: 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;
		}
	}
}
 
Example 21
Source Project: flink   Source File: NettyClient.java    License: 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 Project: flink   Source File: NettyClient.java    License: 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;
		}
	}
}