Example Code for Exceptions in Netty (#3406)
* added updated example codes * updated example code StringToCharStream * deleted StringToCharStream.java locally * removed redundant file * added code for apache commons collection SetUtils * refactored example code * added example code for bytebuddy * added example code for PCollections * update pom * refactored tests for PCollections * spring security xml config * spring security xml config * remove redundant comment * example code for apache-shiro * updated example code for Vavr Collections * updated Vavr's Collection example * updated Vavr Collection file * updated example code for Apache Shiro * updated Vavr Collections example * added example code for N1QL * update example code for N1QL * added integration test for N1QL * update N1QL Example code * update the N1QL example Code * rename module to couchbase * rename module to couchbase * change module name in parent module and pom * added cas-server module * added cas secured app for Spring SSO with CAS * refactor cas modules into cas folder * updated files * removed redundant files * refactor the config for cas-server * added sql file to generate tables and database * added source code for VRaptor * update source code for VRaptor article * update pom and reformat code * add example code for BAEL-1439 * updated code for netty exceptions
This commit is contained in:
parent
740073b6ad
commit
6923d3accd
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.netty;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class ChannelHandlerA extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private Logger logger = Logger.getLogger(getClass().getName());
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
throw new Exception("Ooops");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
logger.info("Exception Occurred in ChannelHandler A");
|
||||
ctx.fireExceptionCaught(cause);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.netty;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
public class ChannelHandlerB extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private Logger logger = Logger.getLogger(getClass().getName());
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
logger.info("Exception Handled in ChannelHandler B");
|
||||
logger.info(cause.getLocalizedMessage());
|
||||
//do more exception handling
|
||||
ctx.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.netty;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
|
||||
public class NettyServerB {
|
||||
|
||||
private int port;
|
||||
|
||||
private NettyServerB(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
private void run() throws Exception {
|
||||
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup();
|
||||
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
|
||||
try {
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ch.pipeline().addLast(new ChannelHandlerA(), new ChannelHandlerB());
|
||||
}
|
||||
})
|
||||
.option(ChannelOption.SO_BACKLOG, 128)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true);
|
||||
ChannelFuture f = b.bind(port).sync(); // (7)
|
||||
f.channel().closeFuture().sync();
|
||||
} finally {
|
||||
workerGroup.shutdownGracefully();
|
||||
bossGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new NettyServerB(8080).run();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue