use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project netty by netty.
the class FixedChannelPoolTest method testAcquireTimeout.
@Test(expected = TimeoutException.class)
public void testAcquireTimeout() throws Exception {
EventLoopGroup group = new LocalEventLoopGroup();
LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
Bootstrap cb = new Bootstrap();
cb.remoteAddress(addr);
cb.group(group).channel(LocalChannel.class);
ServerBootstrap sb = new ServerBootstrap();
sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
}
});
// Start server
Channel sc = sb.bind(addr).syncUninterruptibly().channel();
ChannelPoolHandler handler = new TestChannelPoolHandler();
ChannelPool pool = new FixedChannelPool(cb, handler, ChannelHealthChecker.ACTIVE, AcquireTimeoutAction.FAIL, 500, 1, Integer.MAX_VALUE);
Channel channel = pool.acquire().syncUninterruptibly().getNow();
Future<Channel> future = pool.acquire();
try {
future.syncUninterruptibly();
} finally {
sc.close().syncUninterruptibly();
channel.close().syncUninterruptibly();
group.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project cradle by BingLau7.
the class EchoClient method start.
void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
// 指定 EventLoopGroup 以处理客户端时间;需要适用于 NIO 的实现
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host, port)).handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
// 连接远程节点,阻塞等待直到连接完成
ChannelFuture f = b.connect().sync();
// 阻塞,直到 Channel 关闭
f.channel().closeFuture().sync();
} finally {
// 关闭线程池并释放所有的资源
group.shutdownGracefully().sync();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project intellij-community by JetBrains.
the class BuildManager method startListening.
private int startListening() throws Exception {
EventLoopGroup group;
BuiltInServer mainServer = StartupUtil.getServer();
boolean isOwnEventLoopGroup = !Registry.is("compiler.shared.event.group", true) || mainServer == null || mainServer.getEventLoopGroup() instanceof OioEventLoopGroup;
if (isOwnEventLoopGroup) {
group = new NioEventLoopGroup(1, ConcurrencyUtil.newNamedThreadFactory("External compiler"));
} else {
group = mainServer.getEventLoopGroup();
}
final ServerBootstrap bootstrap = serverBootstrap(group);
bootstrap.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(@NotNull Channel channel) throws Exception {
channel.pipeline().addLast(myChannelRegistrar, new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(CmdlineRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), myMessageDispatcher);
}
});
Channel serverChannel = bootstrap.bind(InetAddress.getLoopbackAddress(), 0).syncUninterruptibly().channel();
myChannelRegistrar.setServerChannel(serverChannel, isOwnEventLoopGroup);
return ((InetSocketAddress) serverChannel.localAddress()).getPort();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project alluxio by Alluxio.
the class NettyDataServer method createBootstrapOfType.
/**
* Creates a default {@link io.netty.bootstrap.ServerBootstrap} where the channel and groups are
* preset.
*
* @param type the channel type; current channel types supported are nio and epoll
* @return an instance of {@code ServerBootstrap}
*/
private ServerBootstrap createBootstrapOfType(final ChannelType type) {
final ServerBootstrap boot = new ServerBootstrap();
final int bossThreadCount = Configuration.getInt(PropertyKey.WORKER_NETWORK_NETTY_BOSS_THREADS);
// If number of worker threads is 0, Netty creates (#processors * 2) threads by default.
final int workerThreadCount = Configuration.getInt(PropertyKey.WORKER_NETWORK_NETTY_WORKER_THREADS);
final EventLoopGroup bossGroup = NettyUtils.createEventLoop(type, bossThreadCount, "data-server-boss-%d", false);
final EventLoopGroup workerGroup = NettyUtils.createEventLoop(type, workerThreadCount, "data-server-worker-%d", false);
final Class<? extends ServerChannel> socketChannelClass = NettyUtils.getServerChannelClass(type);
boot.group(bossGroup, workerGroup).channel(socketChannelClass);
if (type == ChannelType.EPOLL) {
boot.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
}
return boot;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project pulsar by yahoo.
the class MockBrokerService method startMockBrokerService.
public void startMockBrokerService() throws Exception {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("mock-pulsar-%s").build();
final int numThreads = 2;
final int MaxMessageSize = 5 * 1024 * 1024;
EventLoopGroup eventLoopGroup;
try {
if (SystemUtils.IS_OS_LINUX) {
try {
eventLoopGroup = new EpollEventLoopGroup(numThreads, threadFactory);
} catch (UnsatisfiedLinkError e) {
eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
}
} else {
eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
}
workerGroup = eventLoopGroup;
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(workerGroup, workerGroup);
if (workerGroup instanceof EpollEventLoopGroup) {
bootstrap.channel(EpollServerSocketChannel.class);
} else {
bootstrap.channel(NioServerSocketChannel.class);
}
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MaxMessageSize, 0, 4, 0, 4));
ch.pipeline().addLast("handler", new MockServerCnx());
}
});
// Bind and start to accept incoming connections.
bootstrap.bind(brokerServicePort).sync();
} catch (Exception e) {
throw e;
}
}
Aggregations