本文整理汇总了Java中io.netty.channel.ChannelInboundHandler类的典型用法代码示例。如果您正苦于以下问题:Java ChannelInboundHandler类的具体用法?Java ChannelInboundHandler怎么用?Java ChannelInboundHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelInboundHandler类属于io.netty.channel包,在下文中一共展示了ChannelInboundHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initChannel_adds_HttpRequestDecoder_as_the_first_inbound_handler_after_sslCtx
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
@Test
public void initChannel_adds_HttpRequestDecoder_as_the_first_inbound_handler_after_sslCtx() {
// given
HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
// when
hci.initChannel(socketChannelMock);
// then
ArgumentCaptor<ChannelHandler> channelHandlerArgumentCaptor = ArgumentCaptor.forClass(ChannelHandler.class);
verify(channelPipelineMock, atLeastOnce()).addLast(anyString(), channelHandlerArgumentCaptor.capture());
List<ChannelHandler> handlers = channelHandlerArgumentCaptor.getAllValues();
Pair<Integer, ChannelInboundHandler> firstInboundHandler = findChannelHandler(handlers, ChannelInboundHandler.class);
Pair<Integer, HttpRequestDecoder> foundHandler = findChannelHandler(handlers, HttpRequestDecoder.class);
assertThat(firstInboundHandler, notNullValue());
assertThat(foundHandler, notNullValue());
// No SSL Context was passed, so HttpRequestDecoder should be the first inbound handler.
assertThat(foundHandler.getLeft(), is(firstInboundHandler.getLeft()));
assertThat(foundHandler.getRight(), is(firstInboundHandler.getRight()));
}
示例2: channelRegistered
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
/**
* When this channel is registered, we will add all the ChannelHandlers passed into our
* constructor to the pipeline.
*/
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
/**
* This check is necessary as a channel may be registered with different event loops during it
* lifetime and we only want to configure it once.
*/
if (handlers != null) {
for (ChannelHandler handler : handlers) {
ctx.pipeline().addBefore(ctx.name(), null, handler);
}
ChannelHandler handler0 = handlers[0];
ChannelHandlerContext handler0Ctx = ctx.pipeline().context(handlers[0]);
handlers = null;
if (handler0Ctx != null) { // The handler may have removed itself immediately
if (handler0 instanceof ChannelInboundHandler) {
((ChannelInboundHandler) handler0).channelRegistered(handler0Ctx);
} else {
handler0Ctx.fireChannelRegistered();
}
}
} else {
super.channelRegistered(ctx);
}
}
示例3: writeToChannel
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
/**
* Write to channel.
*
* @param channel the channel
* @param magicBytes the magic bytes
* @param pathOrCommand the path or command
* @param attachment the attachment
*/
private void writeToChannel(Channel channel, String[] magicBytes, Object pathOrCommand, Object attachment) {
long firstAttempt = System.currentTimeMillis();
long timeOut = RemotingConstants.TEN * RemotingConstants.THOUSAND;
while (!channel.isOpen() || !channel.isActive()) {
if (System.currentTimeMillis() - firstAttempt >= timeOut) {
try {
throw new TimeoutException();
} catch (TimeoutException e) {
logger.error("Waited for 10 sec for connection reattempt to JumbuneAgent, but failed to connect", e);
}
break;
}
}
if (channel.isActive()) {
logger.debug("channel #" + channel.hashCode() + " connected");
} else {
logger.warn("channel #" + channel.hashCode() + " still disconnected, about to write on disconnected Channel");
}
if (attachment != null && attachment instanceof CyclicBarrier) {
channel.attr(RemotingConstants.barrierKey).set((CyclicBarrier)attachment);
}else if (attachment != null) {
channel.attr(RemotingConstants.handlerKey).set((ChannelInboundHandler)attachment);
}
channel.write(Unpooled.wrappedBuffer(magicBytes[0].getBytes(), magicBytes[1].getBytes(), magicBytes[2].getBytes()));
channel.write(pathOrCommand);
channel.flush();
}
示例4: newInstance
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
@Override
public ChannelInboundHandler newInstance() {
NetHandler handler = new NetHandler();
handler.setChannelHandlerFactory(this);
handler.setCache(cache);
return handler;
}
示例5: OlapPipelineFactory
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
public OlapPipelineFactory(ChannelInboundHandler submitHandler, ChannelInboundHandler cancelHandler, ChannelInboundHandler statusHandler){
this.submitHandler=submitHandler;
this.cancelHandler=cancelHandler;
this.statusHandler=statusHandler;
this.decoder = new ProtobufDecoder(OlapMessage.Command.getDefaultInstance(),buildExtensionRegistry());
}
示例6: startServer
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
public void startServer(SConfiguration config) throws IOException {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(15, new ThreadFactoryBuilder().setNameFormat("OlapServer-%d").setDaemon(true).build());
SpliceLogUtils.warn(LOG, "Olap Server starting (binding to port %s)...", port);
ServerBootstrap bootstrap = new ServerBootstrap();
// Instantiate handler once and share it
OlapJobRegistry registry = new MappedJobRegistry(config.getOlapClientTickTime(),
config.getOlapServerTickLimit(),
TimeUnit.MILLISECONDS);
ChannelInboundHandler submitHandler = new OlapRequestHandler(config,
registry,clock,config.getOlapClientTickTime());
ChannelInboundHandler statusHandler = new OlapStatusHandler(registry);
ChannelInboundHandler cancelHandler = new OlapCancelHandler(registry);
bossGroup = new NioEventLoopGroup(2, new ThreadFactoryBuilder().setNameFormat("OlapServer-boss-%d").setDaemon(true).build());
workerGroup = new NioEventLoopGroup(15, new ThreadFactoryBuilder().setNameFormat("OlapServer-%d").setDaemon(true).build());
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new OlapPipelineFactory(submitHandler,cancelHandler,statusHandler));
bootstrap.option(ChannelOption.TCP_NODELAY, false);
bootstrap.childOption(ChannelOption.TCP_NODELAY, false);
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
try {
this.channel = bootstrap.bind(new InetSocketAddress(getPortNumber())).sync().channel();
} catch (InterruptedException e) {
throw new IOException(e);
}
port = ((InetSocketAddress)channel.localAddress()).getPort();
SpliceLogUtils.warn(LOG, "Olap Server started at port " + port);
}
示例7: decoder
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
public ChannelInboundHandler decoder() {
return decoder;
}
示例8: itCompletesCloseFutureExceptionallyWhenTheConnectionIsClosed
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
@Test
public void itCompletesCloseFutureExceptionallyWhenTheConnectionIsClosed() throws Exception {
ChannelInboundHandler errorHandler = getErrorHandler();
Exception testException = new Exception();
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
errorHandler.exceptionCaught(context, testException);
verify(context).close();
errorHandler.channelInactive(context);
assertThat(session.getCloseFuture().isCompletedExceptionally()).isTrue();
assertThatThrownBy(() -> session.getCloseFuture().get()).hasCause(testException);
}
示例9: getErrorHandler
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
private ChannelInboundHandler getErrorHandler() {
ArgumentCaptor<ChannelHandler> captor = ArgumentCaptor.forClass(ChannelHandler.class);
verify(pipeline).addLast(captor.capture());
return (ChannelInboundHandler) captor.getValue();
}
示例10: SocketThread
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
public SocketThread(String strHost, int nPort, ChannelInboundHandler handler) {
this.strHost = strHost;
this.nPort = nPort;
init(handler);
}
示例11: createHttpRequestHandlerForHttp
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
@Override
protected ChannelInboundHandler createHttpRequestHandlerForHttp() {
return new SpdyServerHandler();
}
示例12: testBindDeadLock
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
@Test(timeout = 10000)
public void testBindDeadLock() throws Exception {
EventLoopGroup groupA = new LocalEventLoopGroup(1);
EventLoopGroup groupB = new LocalEventLoopGroup(1);
try {
ChannelInboundHandler dummyHandler = new DummyHandler();
final Bootstrap bootstrapA = new Bootstrap();
bootstrapA.group(groupA);
bootstrapA.channel(LocalChannel.class);
bootstrapA.handler(dummyHandler);
final Bootstrap bootstrapB = new Bootstrap();
bootstrapB.group(groupB);
bootstrapB.channel(LocalChannel.class);
bootstrapB.handler(dummyHandler);
List<Future<?>> bindFutures = new ArrayList<Future<?>>();
// Try to bind from each other.
for (int i = 0; i < 1024; i ++) {
bindFutures.add(groupA.next().submit(new Runnable() {
@Override
public void run() {
bootstrapB.bind(LocalAddress.ANY);
}
}));
bindFutures.add(groupB.next().submit(new Runnable() {
@Override
public void run() {
bootstrapA.bind(LocalAddress.ANY);
}
}));
}
for (Future<?> f: bindFutures) {
f.sync();
}
} finally {
groupA.shutdownGracefully();
groupB.shutdownGracefully();
groupA.terminationFuture().sync();
groupB.terminationFuture().sync();
}
}
示例13: testConnectDeadLock
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
@Test(timeout = 10000)
public void testConnectDeadLock() throws Exception {
EventLoopGroup groupA = new LocalEventLoopGroup(1);
EventLoopGroup groupB = new LocalEventLoopGroup(1);
try {
ChannelInboundHandler dummyHandler = new DummyHandler();
final Bootstrap bootstrapA = new Bootstrap();
bootstrapA.group(groupA);
bootstrapA.channel(LocalChannel.class);
bootstrapA.handler(dummyHandler);
final Bootstrap bootstrapB = new Bootstrap();
bootstrapB.group(groupB);
bootstrapB.channel(LocalChannel.class);
bootstrapB.handler(dummyHandler);
List<Future<?>> bindFutures = new ArrayList<Future<?>>();
// Try to connect from each other.
for (int i = 0; i < 1024; i ++) {
bindFutures.add(groupA.next().submit(new Runnable() {
@Override
public void run() {
bootstrapB.connect(LocalAddress.ANY);
}
}));
bindFutures.add(groupB.next().submit(new Runnable() {
@Override
public void run() {
bootstrapA.connect(LocalAddress.ANY);
}
}));
}
for (Future<?> f: bindFutures) {
f.sync();
}
} finally {
groupA.shutdownGracefully();
groupB.shutdownGracefully();
groupA.terminationFuture().sync();
groupB.terminationFuture().sync();
}
}
示例14: connect
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
public static CompletionStage<RawMemcacheClient> connect(
final HostAndPort address,
final int outstandingRequestLimit,
final boolean binary,
final Executor executor,
final long timeoutMillis,
final Charset charset,
final Metrics metrics,
final int maxSetLength) {
final ChannelInboundHandler decoder;
if (binary) {
decoder = new BinaryMemcacheDecoder();
} else {
decoder = new AsciiMemcacheDecoder(charset);
}
final ChannelHandler initializer = new ChannelInitializer<Channel>() {
@Override
protected void initChannel(final Channel ch) throws Exception {
ch.pipeline().addLast(
new TcpTuningHandler(),
decoder,
// Downstream
new MemcacheEncoder()
);
}
};
final CompletableFuture<RawMemcacheClient> clientFuture = new CompletableFuture<>();
final Bootstrap bootstrap = new Bootstrap()
.group(EVENT_LOOP_GROUP)
.handler(initializer)
.channel(NioSocketChannel.class)
.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, SimpleSizeEstimator.INSTANCE);
final ChannelFuture connectFuture = bootstrap.connect(
new InetSocketAddress(address.getHostText(), address.getPort()));
connectFuture.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
// Create client
final RawMemcacheClient client = new DefaultRawMemcacheClient(
address,
future.channel(),
outstandingRequestLimit,
executor,
timeoutMillis,
metrics,
maxSetLength);
clientFuture.complete(client);
} else {
future.channel().close();
clientFuture.completeExceptionally(future.cause());
}
});
return onExecutor(clientFuture, executor);
}
示例15: setDelegate
import io.netty.channel.ChannelInboundHandler; //导入依赖的package包/类
public void setDelegate(ChannelInboundHandler delegate) {
this.delegate = delegate;
}