@@ -36,10 +36,8 @@ public class EmbedServer {
3636 public void start (final String address , final int port , final String appname , final String accessToken ) {
3737 executorBiz = new ExecutorBizImpl ();
3838 thread = new Thread (new Runnable () {
39-
4039 @ Override
4140 public void run () {
42-
4341 // param
4442 EventLoopGroup bossGroup = new NioEventLoopGroup ();
4543 EventLoopGroup workerGroup = new NioEventLoopGroup ();
@@ -61,8 +59,6 @@ public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
6159 throw new RuntimeException ("xxl-job, EmbedServer bizThreadPool is EXHAUSTED!" );
6260 }
6361 });
64-
65-
6662 try {
6763 // start server
6864 ServerBootstrap bootstrap = new ServerBootstrap ();
@@ -92,11 +88,9 @@ public void initChannel(SocketChannel channel) throws Exception {
9288 future .channel ().closeFuture ().sync ();
9389
9490 } catch (InterruptedException e ) {
95- if (e instanceof InterruptedException ) {
96- logger .info (">>>>>>>>>>> xxl-job remoting server stop." );
97- } else {
98- logger .error (">>>>>>>>>>> xxl-job remoting server error." , e );
99- }
91+ logger .info (">>>>>>>>>>> xxl-job remoting server stop." );
92+ } catch (Exception e ) {
93+ logger .error (">>>>>>>>>>> xxl-job remoting server error." , e );
10094 } finally {
10195 // stop
10296 try {
@@ -106,17 +100,15 @@ public void initChannel(SocketChannel channel) throws Exception {
106100 logger .error (e .getMessage (), e );
107101 }
108102 }
109-
110103 }
111-
112104 });
113- thread .setDaemon (true ); // daemon, service jvm, user thread leave >>> daemon leave >>> jvm leave
105+ thread .setDaemon (true ); // daemon, service jvm, user thread leave >>> daemon leave >>> jvm leave
114106 thread .start ();
115107 }
116108
117109 public void stop () throws Exception {
118110 // destroy server thread
119- if (thread != null && thread .isAlive ()) {
111+ if (thread != null && thread .isAlive ()) {
120112 thread .interrupt ();
121113 }
122114
@@ -130,7 +122,7 @@ public void stop() throws Exception {
130122
131123 /**
132124 * netty_http
133- *
125+ * <p>
134126 * Copy from : https://github.com/xuxueli/xxl-rpc
135127 *
136128 * @author xuxueli 2015-11-24 22:25:15
@@ -141,6 +133,7 @@ public static class EmbedHttpServerHandler extends SimpleChannelInboundHandler<F
141133 private ExecutorBiz executorBiz ;
142134 private String accessToken ;
143135 private ThreadPoolExecutor bizThreadPool ;
136+
144137 public EmbedHttpServerHandler (ExecutorBiz executorBiz , String accessToken , ThreadPoolExecutor bizThreadPool ) {
145138 this .executorBiz = executorBiz ;
146139 this .accessToken = accessToken ;
@@ -149,7 +142,6 @@ public EmbedHttpServerHandler(ExecutorBiz executorBiz, String accessToken, Threa
149142
150143 @ Override
151144 protected void channelRead0 (final ChannelHandlerContext ctx , FullHttpRequest msg ) throws Exception {
152-
153145 // request parse
154146 //final byte[] requestBytes = ByteBufUtil.getBytes(msg.content()); // byteBuf.toString(io.netty.util.CharsetUtil.UTF_8);
155147 String requestData = msg .content ().toString (CharsetUtil .UTF_8 );
@@ -175,38 +167,38 @@ public void run() {
175167 }
176168
177169 private Object process (HttpMethod httpMethod , String uri , String requestData , String accessTokenReq ) {
178-
179170 // valid
180171 if (HttpMethod .POST != httpMethod ) {
181172 return new ReturnT <String >(ReturnT .FAIL_CODE , "invalid request, HttpMethod not support." );
182173 }
183- if (uri == null || uri .trim ().length ()== 0 ) {
174+ if (uri == null || uri .trim ().length () == 0 ) {
184175 return new ReturnT <String >(ReturnT .FAIL_CODE , "invalid request, uri-mapping empty." );
185176 }
186- if (accessToken != null
187- && accessToken .trim ().length ()> 0
177+ if (accessToken != null
178+ && accessToken .trim ().length () > 0
188179 && !accessToken .equals (accessTokenReq )) {
189180 return new ReturnT <String >(ReturnT .FAIL_CODE , "The access token is wrong." );
190181 }
191182
192183 // services mapping
193184 try {
194- if ("/beat" .equals (uri )) {
195- return executorBiz .beat ();
196- } else if ("/idleBeat" .equals (uri )) {
197- IdleBeatParam idleBeatParam = GsonTool .fromJson (requestData , IdleBeatParam .class );
198- return executorBiz .idleBeat (idleBeatParam );
199- } else if ("/run" .equals (uri )) {
200- TriggerParam triggerParam = GsonTool .fromJson (requestData , TriggerParam .class );
201- return executorBiz .run (triggerParam );
202- } else if ("/kill" .equals (uri )) {
203- KillParam killParam = GsonTool .fromJson (requestData , KillParam .class );
204- return executorBiz .kill (killParam );
205- } else if ("/log" .equals (uri )) {
206- LogParam logParam = GsonTool .fromJson (requestData , LogParam .class );
207- return executorBiz .log (logParam );
208- } else {
209- return new ReturnT <String >(ReturnT .FAIL_CODE , "invalid request, uri-mapping(" + uri +") not found." );
185+ switch (uri ) {
186+ case "/beat" :
187+ return executorBiz .beat ();
188+ case "/idleBeat" :
189+ IdleBeatParam idleBeatParam = GsonTool .fromJson (requestData , IdleBeatParam .class );
190+ return executorBiz .idleBeat (idleBeatParam );
191+ case "/run" :
192+ TriggerParam triggerParam = GsonTool .fromJson (requestData , TriggerParam .class );
193+ return executorBiz .run (triggerParam );
194+ case "/kill" :
195+ KillParam killParam = GsonTool .fromJson (requestData , KillParam .class );
196+ return executorBiz .kill (killParam );
197+ case "/log" :
198+ LogParam logParam = GsonTool .fromJson (requestData , LogParam .class );
199+ return executorBiz .log (logParam );
200+ default :
201+ return new ReturnT <String >(ReturnT .FAIL_CODE , "invalid request, uri-mapping(" + uri + ") not found." );
210202 }
211203 } catch (Exception e ) {
212204 logger .error (e .getMessage (), e );
@@ -261,6 +253,4 @@ public void stopRegistry() {
261253 // stop registry
262254 ExecutorRegistryThread .getInstance ().toStop ();
263255 }
264-
265-
266256}
0 commit comments