use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project motan by weibocom.
the class NettyServer method open.
@Override
public boolean open() {
if (isAvailable()) {
LoggerUtil.warn("NettyServer ServerChannel already Open: url=" + url);
return state.isAliveState();
}
if (bossGroup == null) {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
}
LoggerUtil.info("NettyServer ServerChannel start Open: url=" + url);
boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
int maxServerConnection = url.getIntParameter(URLParamType.maxServerConnection.getName(), URLParamType.maxServerConnection.getIntValue());
int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), URLParamType.workerQueueSize.getIntValue());
int minWorkerThread, maxWorkerThread;
if (shareChannel) {
minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER);
maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER);
} else {
minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER);
maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER);
}
standardThreadExecutor = (standardThreadExecutor != null && !standardThreadExecutor.isShutdown()) ? standardThreadExecutor : new StandardThreadExecutor(minWorkerThread, maxWorkerThread, workerQueueSize, new DefaultThreadFactory("NettyServer-" + url.getServerPortStr(), true));
standardThreadExecutor.prestartAllCoreThreads();
channelManage = new NettyServerChannelManage(maxServerConnection);
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("channel_manage", channelManage);
pipeline.addLast("decoder", new NettyDecoder(codec, NettyServer.this, maxContentLength));
pipeline.addLast("encoder", new NettyEncoder());
NettyChannelHandler handler = new NettyChannelHandler(NettyServer.this, messageHandler, standardThreadExecutor);
pipeline.addLast("handler", handler);
}
});
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(url.getPort()));
channelFuture.syncUninterruptibly();
serverChannel = channelFuture.channel();
setLocalAddress((InetSocketAddress) serverChannel.localAddress());
if (url.getPort() == 0) {
url.setPort(getLocalAddress().getPort());
}
state = ChannelState.ALIVE;
StatsUtil.registryStatisticCallback(this);
LoggerUtil.info("NettyServer ServerChannel finish Open: url=" + url);
return state.isAliveState();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project flink by apache.
the class NettyClient method connect.
// ------------------------------------------------------------------------
// Client connections
// ------------------------------------------------------------------------
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;
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project pinpoint by naver.
the class NettyIT method client.
public Bootstrap client(EventLoopGroup workerGroup) {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HttpClientCodec());
ch.pipeline().addLast(new HttpObjectAggregator(65535));
}
});
return bootstrap;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project netty by netty.
the class FileRegionThrottleTest method testGlobalWriteThrottle.
@Disabled("This test is flaky, need more investigation")
@Test
public void testGlobalWriteThrottle() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final GlobalTrafficShapingHandler gtsh = new GlobalTrafficShapingHandler(group, WRITE_LIMIT, 0);
ServerBootstrap bs = new ServerBootstrap();
bs.group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new LineBasedFrameDecoder(Integer.MAX_VALUE));
ch.pipeline().addLast(new MessageDecoder());
ch.pipeline().addLast(gtsh);
}
});
Channel sc = bs.bind(0).sync().channel();
Channel cc = clientConnect(sc.localAddress(), new ReadHandler(latch)).channel();
long start = TrafficCounter.milliSecondFromNano();
cc.writeAndFlush(Unpooled.copiedBuffer("send-file\n", CharsetUtil.US_ASCII)).sync();
latch.await();
long timeTaken = TrafficCounter.milliSecondFromNano() - start;
assertTrue(timeTaken > 3000, "Data streamed faster than expected");
sc.close().sync();
cc.close().sync();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project netty by netty.
the class FileRegionThrottleTest method clientConnect.
private ChannelFuture clientConnect(final SocketAddress server, final ReadHandler readHandler) throws Exception {
Bootstrap bc = new Bootstrap();
bc.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(readHandler);
}
});
return bc.connect(server).sync();
}
Aggregations