Skip to content

Commit 6923d3a

Browse files
SeunMattpivovarit
authored andcommitted
Example Code for Exceptions in Netty (eugenp#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
1 parent 740073b commit 6923d3a

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
import java.util.logging.Logger;
7+
8+
public class ChannelHandlerA extends ChannelInboundHandlerAdapter {
9+
10+
private Logger logger = Logger.getLogger(getClass().getName());
11+
12+
@Override
13+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
14+
throw new Exception("Ooops");
15+
}
16+
17+
@Override
18+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
19+
logger.info("Exception Occurred in ChannelHandler A");
20+
ctx.fireExceptionCaught(cause);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
import java.util.logging.Logger;
7+
8+
9+
public class ChannelHandlerB extends ChannelInboundHandlerAdapter {
10+
11+
private Logger logger = Logger.getLogger(getClass().getName());
12+
13+
@Override
14+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
15+
logger.info("Exception Handled in ChannelHandler B");
16+
logger.info(cause.getLocalizedMessage());
17+
//do more exception handling
18+
ctx.close();
19+
}
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.ChannelOption;
7+
import io.netty.channel.EventLoopGroup;
8+
import io.netty.channel.nio.NioEventLoopGroup;
9+
import io.netty.channel.socket.SocketChannel;
10+
import io.netty.channel.socket.nio.NioServerSocketChannel;
11+
12+
public class NettyServerB {
13+
14+
private int port;
15+
16+
private NettyServerB(int port) {
17+
this.port = port;
18+
}
19+
20+
private void run() throws Exception {
21+
22+
EventLoopGroup bossGroup = new NioEventLoopGroup();
23+
EventLoopGroup workerGroup = new NioEventLoopGroup();
24+
25+
try {
26+
ServerBootstrap b = new ServerBootstrap();
27+
b.group(bossGroup, workerGroup)
28+
.channel(NioServerSocketChannel.class)
29+
.childHandler(new ChannelInitializer<SocketChannel>() {
30+
public void initChannel(SocketChannel ch) throws Exception {
31+
ch.pipeline().addLast(new ChannelHandlerA(), new ChannelHandlerB());
32+
}
33+
})
34+
.option(ChannelOption.SO_BACKLOG, 128)
35+
.childOption(ChannelOption.SO_KEEPALIVE, true);
36+
ChannelFuture f = b.bind(port).sync(); // (7)
37+
f.channel().closeFuture().sync();
38+
} finally {
39+
workerGroup.shutdownGracefully();
40+
bossGroup.shutdownGracefully();
41+
}
42+
}
43+
44+
public static void main(String[] args) throws Exception {
45+
new NettyServerB(8080).run();
46+
}
47+
}

0 commit comments

Comments
 (0)