use of io.netty.channel.ChannelPipeline in project motan by weibocom.
the class NettyClient method open.
@Override
public synchronized boolean open() {
if (isAvailable()) {
return true;
}
bootstrap = new Bootstrap();
int timeout = getUrl().getIntParameter(URLParamType.connectTimeout.getName(), URLParamType.connectTimeout.getIntValue());
if (timeout <= 0) {
throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
// 最大响应包限制
final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
bootstrap.group(nioEventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new NettyDecoder(codec, NettyClient.this, maxContentLength));
pipeline.addLast("encoder", new NettyEncoder());
pipeline.addLast("handler", new NettyChannelHandler(NettyClient.this, new MessageHandler() {
@Override
public Object handle(Channel channel, Object message) {
Response response = (Response) message;
ResponseFuture responseFuture = NettyClient.this.removeCallback(response.getRequestId());
if (responseFuture == null) {
LoggerUtil.warn("NettyClient has response from server, but responseFuture not exist, requestId={}", response.getRequestId());
return null;
}
if (response.getException() != null) {
responseFuture.onFailure(response);
} else {
responseFuture.onSuccess(response);
}
return null;
}
}));
}
});
// 初始化连接池
initPool();
LoggerUtil.info("NettyClient finish Open: url={}", url);
// 注册统计回调
StatsUtil.registryStatisticCallback(this);
// 设置可用状态
state = ChannelState.ALIVE;
return true;
}
use of io.netty.channel.ChannelPipeline in project neo4j by neo4j.
the class TransportSelectionHandler method switchToWebsocket.
private void switchToWebsocket(ChannelHandlerContext ctx) {
ChannelPipeline p = ctx.pipeline();
memoryTracker.allocateHeap(HTTP_SERVER_CODEC_SHALLOW_SIZE + HTTP_OBJECT_AGGREGATOR_SHALLOW_SIZE + WEB_SOCKET_SERVER_PROTOCOL_HANDLER_SHALLOW_SIZE + WEB_SOCKET_FRAME_AGGREGATOR_SHALLOW_SIZE + WebSocketFrameTranslator.SHALLOW_SIZE + ProtocolHandshaker.SHALLOW_SIZE);
p.addLast(new HttpServerCodec(), new HttpObjectAggregator(MAX_WEBSOCKET_HANDSHAKE_SIZE), new WebSocketServerProtocolHandler("/", null, false, MAX_WEBSOCKET_FRAME_SIZE), new WebSocketFrameAggregator(MAX_WEBSOCKET_FRAME_SIZE), new WebSocketFrameTranslator(), newHandshaker());
p.remove(this);
}
use of io.netty.channel.ChannelPipeline in project neo4j by neo4j.
the class TransportSelectionHandler method enableSsl.
private void enableSsl(ChannelHandlerContext ctx) {
// allocate sufficient space for another transport selection handlers as this instance will be freed upon pipeline removal
memoryTracker.allocateHeap(SSL_HANDLER_SHALLOW_SIZE + SHALLOW_SIZE);
ChannelPipeline p = ctx.pipeline();
p.addLast(sslCtx.newHandler(ctx.alloc()));
p.addLast(new TransportSelectionHandler(boltChannel, null, encryptionRequired, true, logging, boltProtocolFactory, channelProtector, memoryTracker));
p.remove(this);
}
use of io.netty.channel.ChannelPipeline in project neo4j by neo4j.
the class TransportWriteThrottleTest method setup.
@BeforeEach
void setup() {
config = mock(SocketChannelConfig.class);
lockAttribute = mock(Attribute.class);
Attribute durationExceedAttribute = mock(Attribute.class);
when(durationExceedAttribute.get()).thenReturn(null);
channel = mock(SocketChannel.class, Answers.RETURNS_MOCKS);
when(channel.config()).thenReturn(config);
when(channel.isOpen()).thenReturn(true);
when(channel.remoteAddress()).thenReturn(InetSocketAddress.createUnresolved("localhost", 0));
when(channel.attr(TransportWriteThrottle.LOCK_KEY)).thenReturn(lockAttribute);
when(channel.attr(TransportWriteThrottle.MAX_DURATION_EXCEEDED_KEY)).thenReturn(durationExceedAttribute);
ChannelPipeline pipeline = channel.pipeline();
when(channel.pipeline()).thenReturn(pipeline);
context = mock(ChannelHandlerContext.class, Answers.RETURNS_MOCKS);
when(context.channel()).thenReturn(channel);
}
use of io.netty.channel.ChannelPipeline in project neo4j by neo4j.
the class TransportSelectionHandlerTest method channelHandlerContextMockSslAlreadyConfigured.
private static ChannelHandlerContext channelHandlerContextMockSslAlreadyConfigured() {
Channel channel = mock(Channel.class);
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
ChannelPipeline pipeline = mock(ChannelPipeline.class);
SslHandler sslHandler = mock(SslHandler.class);
when(context.channel()).thenReturn(channel);
when(context.pipeline()).thenReturn(pipeline);
when(context.pipeline().get(SslHandler.class)).thenReturn(sslHandler);
return context;
}
Aggregations