Skip to content

Commit a466a6c

Browse files
authored
Merge pull request #2833 from cszxyang/polish_20220419
异常处理、日志处理及部分代码简单美化
2 parents fbc02e5 + d55068a commit a466a6c

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public void start() throws Exception {
8383
// init executor-server
8484
initEmbedServer(address, ip, port, appname, accessToken);
8585
}
86+
8687
public void destroy(){
8788
// destroy executor-server
8889
stopEmbedServer();
@@ -131,6 +132,7 @@ private void initAdminBizList(String adminAddresses, String accessToken) throws
131132
}
132133
}
133134
}
135+
134136
public static List<AdminBiz> getAdminBizList(){
135137
return adminBizList;
136138
}
@@ -251,6 +253,7 @@ public static JobThread registJobThread(int jobId, IJobHandler handler, String r
251253

252254
return newJobThread;
253255
}
256+
254257
public static JobThread removeJobThread(int jobId, String removeOldReason){
255258
JobThread oldJobThread = jobThreadRepository.remove(jobId);
256259
if (oldJobThread != null) {
@@ -261,9 +264,8 @@ public static JobThread removeJobThread(int jobId, String removeOldReason){
261264
}
262265
return null;
263266
}
267+
264268
public static JobThread loadJobThread(int jobId){
265-
JobThread jobThread = jobThreadRepository.get(jobId);
266-
return jobThread;
269+
return jobThreadRepository.get(jobId);
267270
}
268-
269271
}

xxl-job-core/src/main/java/com/xxl/job/core/server/EmbedServer.java

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)