11package net .cryptic_game .microservice ;
22
33import io .netty .bootstrap .Bootstrap ;
4- import io .netty .channel .Channel ;
5- import io .netty .channel .ChannelHandler ;
6- import io .netty .channel .ChannelHandlerContext ;
7- import io .netty .channel .EventLoopGroup ;
8- import io .netty .channel .SimpleChannelInboundHandler ;
4+ import io .netty .channel .*;
95import io .netty .channel .epoll .Epoll ;
106import io .netty .channel .epoll .EpollEventLoopGroup ;
117import io .netty .channel .epoll .EpollSocketChannel ;
3834import javax .persistence .Entity ;
3935import java .lang .reflect .InvocationTargetException ;
4036import java .lang .reflect .Method ;
41- import java .util .Arrays ;
42- import java .util .Date ;
43- import java .util .HashMap ;
44- import java .util .List ;
45- import java .util .Map ;
46- import java .util .Set ;
47- import java .util .UUID ;
48-
49- import static net .cryptic_game .microservice .error .ServerError .INTERNAL_ERROR ;
50- import static net .cryptic_game .microservice .error .ServerError .MISSING_PARAMETERS ;
51- import static net .cryptic_game .microservice .error .ServerError .UNKNOWN_SERVICE ;
52- import static net .cryptic_game .microservice .error .ServerError .UNSUPPORTED_FORMAT ;
37+ import java .util .*;
38+
39+ import static net .cryptic_game .microservice .error .ServerError .*;
5340import static net .cryptic_game .microservice .utils .JSONUtils .checkData ;
5441import static net .cryptic_game .microservice .utils .SocketUtils .send ;
5542import static net .cryptic_game .microservice .utils .SocketUtils .sendError ;
@@ -59,6 +46,7 @@ public abstract class MicroService extends SimpleChannelInboundHandler<String> {
5946
6047 private static final boolean E_POLL = Epoll .isAvailable ();
6148 private static final Logger LOG = LoggerFactory .getLogger (MicroService .class );
49+ private static final EventLoopGroup EVENT_LOOP_GROUP = E_POLL ? new EpollEventLoopGroup () : new NioEventLoopGroup ();
6250
6351 private static MicroService instance ;
6452
@@ -126,15 +114,15 @@ private void init() {
126114 }
127115
128116 private void start () {
129- EventLoopGroup group = E_POLL ? new EpollEventLoopGroup () : new NioEventLoopGroup ();
117+ final JSONObject registration = JSONBuilder .anJSON ()
118+ .add ("action" , "register" )
119+ .add ("name" , name )
120+ .build ();
121+ Channel channel = null ;
130122
131123 try {
132- JSONObject registration = JSONBuilder .anJSON ()
133- .add ("action" , "register" )
134- .add ("name" , name )
135- .build ();
136-
137- Channel channel = new Bootstrap ().group (group )
124+ channel = new Bootstrap ()
125+ .group (EVENT_LOOP_GROUP )
138126 .channel (E_POLL ? EpollSocketChannel .class : NioSocketChannel .class )
139127 .handler (new MicroServiceInitializer (this ))
140128 .connect (Config .get (DefaultConfig .MSSOCKET_HOST ), Config .getInteger (DefaultConfig .MSSOCKET_PORT ))
@@ -143,14 +131,23 @@ private void start() {
143131 this .channel = channel ;
144132
145133 channel .writeAndFlush (registration .toString ());
146-
147134 channel .closeFuture ().syncUninterruptibly ();
135+
148136 } catch (Exception e ) {
149137 LOG .warn (e .toString (), e );
138+ } finally {
139+ if (channel != null ) {
140+ try {
141+ channel .close ().syncUninterruptibly ();
142+ } catch (Exception e ) {
143+ LOG .warn ("Unable to close channel: " + e .toString (), e );
144+ }
145+ }
146+
147+ LOG .info ("Reconnection in 10 seconds..." );
150148
151- group .shutdownGracefully ().syncUninterruptibly ();
152149 try {
153- Thread .sleep (10000L );
150+ Thread .sleep (10000L ); // 10 seconds
154151 } catch (InterruptedException ignored ) {
155152 }
156153
0 commit comments