1717package org .apache .dubbo .remoting .transport .netty ;
1818
1919import org .apache .dubbo .common .URL ;
20- import org .apache .dubbo .common .logger .Logger ;
20+ import org .apache .dubbo .common .logger .ErrorTypeAwareLogger ;
2121import org .apache .dubbo .common .logger .LoggerFactory ;
2222import org .apache .dubbo .common .utils .NetUtils ;
2323import org .apache .dubbo .remoting .Channel ;
3434import org .jboss .netty .channel .MessageEvent ;
3535import org .jboss .netty .channel .SimpleChannelHandler ;
3636
37+ import static org .apache .dubbo .common .constants .LoggerCodeConstants .TRANSPORT_UNEXPECTED_EXCEPTION ;
38+
3739/**
3840 * NettyHandler
3941 */
4042@ Sharable
4143public class NettyHandler extends SimpleChannelHandler {
4244
43- private static final Logger logger = LoggerFactory .getLogger (NettyHandler .class );
45+ private static final ErrorTypeAwareLogger logger = LoggerFactory .getErrorTypeAwareLogger (NettyHandler .class );
4446
4547 private final Map <String , Channel > channels = new ConcurrentHashMap <>(); // <ip:port, channel>
4648
@@ -65,39 +67,43 @@ public Map<String, Channel> getChannels() {
6567
6668 @ Override
6769 public void channelConnected (ChannelHandlerContext ctx , ChannelStateEvent e ) throws Exception {
68- NettyChannel channel = NettyChannel .getOrAddChannel (ctx .getChannel (), url , handler );
70+ org .jboss .netty .channel .Channel ch = ctx .getChannel ();
71+ NettyChannel channel = NettyChannel .getOrAddChannel (ch , url , handler );
6972 try {
7073 if (channel != null ) {
71- channels .put (
72- NetUtils .toAddressString (
73- (InetSocketAddress ) ctx .getChannel ().getRemoteAddress ()),
74- channel );
74+ channels .put (NetUtils .toAddressString ((InetSocketAddress ) ch .getRemoteAddress ()), channel );
7575 }
7676 handler .connected (channel );
7777 } finally {
78- NettyChannel .removeChannelIfDisconnected (ctx . getChannel () );
78+ NettyChannel .removeChannelIfDisconnected (ch );
7979 }
8080
8181 if (logger .isInfoEnabled () && channel != null ) {
82- logger .info ("The connection between " + channel .getRemoteAddress () + " and " + channel .getLocalAddress ()
83- + " is established" );
82+ logger .info (
83+ "The connection {} between {} and {} is established." ,
84+ ch ,
85+ channel .getRemoteAddress (),
86+ channel .getLocalAddress ());
8487 }
8588 }
8689
8790 @ Override
8891 public void channelDisconnected (ChannelHandlerContext ctx , ChannelStateEvent e ) throws Exception {
89- NettyChannel channel = NettyChannel .getOrAddChannel (ctx .getChannel (), url , handler );
92+ org .jboss .netty .channel .Channel ch = ctx .getChannel ();
93+ NettyChannel channel = NettyChannel .getOrAddChannel (ch , url , handler );
9094 try {
91- channels .remove (NetUtils .toAddressString (
92- (InetSocketAddress ) ctx .getChannel ().getRemoteAddress ()));
95+ channels .remove (NetUtils .toAddressString ((InetSocketAddress ) ch .getRemoteAddress ()));
9396 handler .disconnected (channel );
9497 } finally {
95- NettyChannel .removeChannelIfDisconnected (ctx . getChannel () );
98+ NettyChannel .removeChannelIfDisconnected (ch );
9699 }
97100
98101 if (logger .isInfoEnabled ()) {
99- logger .info ("The connection between " + channel .getRemoteAddress () + " and " + channel .getLocalAddress ()
100- + " is disconnected" );
102+ logger .info (
103+ "The connection {} between {} and {} is disconnected." ,
104+ ch ,
105+ channel .getRemoteAddress (),
106+ channel .getLocalAddress ());
101107 }
102108 }
103109
@@ -124,11 +130,25 @@ public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exc
124130
125131 @ Override
126132 public void exceptionCaught (ChannelHandlerContext ctx , ExceptionEvent e ) throws Exception {
127- NettyChannel channel = NettyChannel .getOrAddChannel (ctx .getChannel (), url , handler );
133+ org .jboss .netty .channel .Channel ch = ctx .getChannel ();
134+ NettyChannel channel = NettyChannel .getOrAddChannel (ch , url , handler );
128135 try {
129136 handler .caught (channel , e .getCause ());
130137 } finally {
131- NettyChannel .removeChannelIfDisconnected (ctx .getChannel ());
138+ NettyChannel .removeChannelIfDisconnected (ch );
139+ }
140+
141+ if (logger .isWarnEnabled ()) {
142+ logger .warn (
143+ TRANSPORT_UNEXPECTED_EXCEPTION ,
144+ "" ,
145+ "" ,
146+ channel == null
147+ ? String .format ("The connection %s has exception." , ch )
148+ : String .format (
149+ "The connection %s between %s and %s has exception." ,
150+ ch , channel .getRemoteAddress (), channel .getLocalAddress ()),
151+ e .getCause ());
132152 }
133153 }
134154}
0 commit comments