From 1970f7f3b851f19b751c16e3b18fcf4d0c1bad4a Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Wed, 30 Aug 2017 22:43:43 +0800 Subject: [PATCH] Clean Code --- .../resolver/WebExceptionResolver.java | 2 +- .../jobbean/impl/DemoConcurrentJobBean.java | 2 +- .../core/jobbean/impl/DemoNomalJobBean.java | 2 +- .../core/schedule/XxlJobDynamicScheduler.java | 4 +- .../xxl/job/admin/core/util/JacksonUtil.java | 93 ------------------- .../com/xxl/job/admin/core/util/MailUtil.java | 2 +- .../admin/service/impl/XxlJobServiceImpl.java | 8 +- .../com/xxl/job/dao/impl/AdminBizTest.java | 2 +- .../com/xxl/job/core/glue/GlueFactory.java | 4 +- .../xxl/job/core/log/XxlJobFileAppender.java | 4 +- .../com/xxl/job/core/util/HttpClientUtil.java | 4 +- .../com/xxl/job/core/util/JacksonUtil.java | 24 +++-- 12 files changed, 31 insertions(+), 120 deletions(-) delete mode 100644 xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/resolver/WebExceptionResolver.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/resolver/WebExceptionResolver.java index 5a15245f..e8928f5f 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/resolver/WebExceptionResolver.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/resolver/WebExceptionResolver.java @@ -1,7 +1,7 @@ package com.xxl.job.admin.controller.resolver; -import com.xxl.job.admin.core.util.JacksonUtil; import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.util.JacksonUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ResponseBody; diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/jobbean/impl/DemoConcurrentJobBean.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/jobbean/impl/DemoConcurrentJobBean.java index 22a77f54..a64ef5b7 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/jobbean/impl/DemoConcurrentJobBean.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/jobbean/impl/DemoConcurrentJobBean.java @@ -21,7 +21,7 @@ package com.xxl.job.admin.core.jobbean.impl; // try { // TimeUnit.SECONDS.sleep(10); // } catch (InterruptedException e) { -// e.printStackTrace(); +// logger.error(e.getMessage(), e); // } // // return false; diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/jobbean/impl/DemoNomalJobBean.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/jobbean/impl/DemoNomalJobBean.java index 7d5bf667..ccb7c41d 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/jobbean/impl/DemoNomalJobBean.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/jobbean/impl/DemoNomalJobBean.java @@ -23,7 +23,7 @@ package com.xxl.job.admin.core.jobbean.impl; // try { // TimeUnit.SECONDS.sleep(10); // } catch (InterruptedException e) { -// e.printStackTrace(); +// logger.error(e.getMessage(), e); // } // // return false; diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/schedule/XxlJobDynamicScheduler.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/schedule/XxlJobDynamicScheduler.java index f30d6144..2a6e5661 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/schedule/XxlJobDynamicScheduler.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/schedule/XxlJobDynamicScheduler.java @@ -143,7 +143,7 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware { } } catch (SchedulerException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } } @@ -377,7 +377,7 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware { } } catch (SchedulerException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); return null; } return jobList; diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java deleted file mode 100644 index 16ca016e..00000000 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.xxl.job.admin.core.util; - - -import org.codehaus.jackson.JsonGenerationException; -import org.codehaus.jackson.JsonParseException; -import org.codehaus.jackson.map.JsonMappingException; -import org.codehaus.jackson.map.ObjectMapper; -import org.codehaus.jackson.type.TypeReference; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -/** - * Jackson util - * - * 1、obj need private and set/get; - * 2、do not support inner class; - * - * @author xuxueli 2015-9-25 18:02:56 - */ -public class JacksonUtil { - private final static ObjectMapper objectMapper = new ObjectMapper(); - public static ObjectMapper getInstance() { - return objectMapper; - } - - /** - * bean、array、List、Map --> json - * - * @param obj - * @return json string - * @throws Exception - */ - public static String writeValueAsString(Object obj) { - try { - return getInstance().writeValueAsString(obj); - } catch (JsonGenerationException e) { - e.printStackTrace(); - } catch (JsonMappingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - /** - * string --> bean、Map、List(array) - * - * @param jsonStr - * @param clazz - * @return obj - * @throws Exception - */ - public static T readValue(String jsonStr, Class clazz) { - try { - return getInstance().readValue(jsonStr, clazz); - } catch (JsonParseException e) { - e.printStackTrace(); - } catch (JsonMappingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - public static T readValueRefer(String jsonStr, Class clazz) { - try { - return getInstance().readValue(jsonStr, new TypeReference() { }); - } catch (JsonParseException e) { - e.printStackTrace(); - } catch (JsonMappingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - public static void main(String[] args) { - try { - Map map = new HashMap(); - map.put("aaa", "111"); - map.put("bbb", "222"); - String json = writeValueAsString(map); - System.out.println(json); - System.out.println(readValue(json, Map.class)); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/MailUtil.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/MailUtil.java index e3bec8ef..07e5cc21 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/MailUtil.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/MailUtil.java @@ -147,7 +147,7 @@ public class MailUtil { mailSender.send(mimeMessage); return true; } catch (Exception e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } return false; } diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java index 60490fb6..d144eff2 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java @@ -226,7 +226,7 @@ public class XxlJobServiceImpl implements XxlJobService { xxlJobLogGlueDao.deleteByJobId(id); return ReturnT.SUCCESS; } catch (SchedulerException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } return ReturnT.FAIL; } @@ -241,7 +241,7 @@ public class XxlJobServiceImpl implements XxlJobService { boolean ret = XxlJobDynamicScheduler.pauseJob(name, group); // jobStatus do not store return ret?ReturnT.SUCCESS:ReturnT.FAIL; } catch (SchedulerException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); return ReturnT.FAIL; } } @@ -256,7 +256,7 @@ public class XxlJobServiceImpl implements XxlJobService { boolean ret = XxlJobDynamicScheduler.resumeJob(name, group); return ret?ReturnT.SUCCESS:ReturnT.FAIL; } catch (SchedulerException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); return ReturnT.FAIL; } } @@ -271,7 +271,7 @@ public class XxlJobServiceImpl implements XxlJobService { XxlJobDynamicScheduler.triggerJob(name, group); return ReturnT.SUCCESS; } catch (SchedulerException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); return ReturnT.FAIL; } } diff --git a/xxl-job-admin/src/test/java/com/xxl/job/dao/impl/AdminBizTest.java b/xxl-job-admin/src/test/java/com/xxl/job/dao/impl/AdminBizTest.java index b65cfb53..6402b8a0 100644 --- a/xxl-job-admin/src/test/java/com/xxl/job/dao/impl/AdminBizTest.java +++ b/xxl-job-admin/src/test/java/com/xxl/job/dao/impl/AdminBizTest.java @@ -43,7 +43,7 @@ public class AdminBizTest { AdminBiz adminBiz = (AdminBiz) new NetComClientProxy(AdminBiz.class, addressUrl, accessToken).getObject(); int jobId = 1; - ReturnT returnT = adminBiz.triggerJob(1); + ReturnT returnT = adminBiz.triggerJob(jobId); Assert.assertTrue(returnT.getCode() == ReturnT.SUCCESS_CODE); } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java b/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java index ba2ba3f1..400cfc09 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java @@ -75,9 +75,9 @@ public class GlueFactory { try { field.set(instance, fieldBean); } catch (IllegalArgumentException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } catch (IllegalAccessException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } } } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java b/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java index 27b942be..1a502344 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java @@ -174,13 +174,13 @@ public class XxlJobFileAppender { return sb.toString(); } } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } } } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/util/HttpClientUtil.java b/xxl-job-core/src/main/java/com/xxl/job/core/util/HttpClientUtil.java index 00b646ba..417a80ff 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/util/HttpClientUtil.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/util/HttpClientUtil.java @@ -71,7 +71,7 @@ public class HttpClientUtil { try { httpClient.close(); } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } } return responseBytes; @@ -101,7 +101,7 @@ public class HttpClientUtil { } return message; } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); throw e; } } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/util/JacksonUtil.java b/xxl-job-core/src/main/java/com/xxl/job/core/util/JacksonUtil.java index 5fde5c02..086c0ce1 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/util/JacksonUtil.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/util/JacksonUtil.java @@ -6,6 +6,8 @@ import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.HashMap; @@ -20,6 +22,8 @@ import java.util.Map; * @author xuxueli 2015-9-25 18:02:56 */ public class JacksonUtil { + private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class); + private final static ObjectMapper objectMapper = new ObjectMapper(); public static ObjectMapper getInstance() { return objectMapper; @@ -36,11 +40,11 @@ public class JacksonUtil { try { return getInstance().writeValueAsString(obj); } catch (JsonGenerationException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } catch (JsonMappingException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } return null; } @@ -57,11 +61,11 @@ public class JacksonUtil { try { return getInstance().readValue(jsonStr, clazz); } catch (JsonParseException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } catch (JsonMappingException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } return null; } @@ -69,11 +73,11 @@ public class JacksonUtil { try { return getInstance().readValue(jsonStr, new TypeReference() { }); } catch (JsonParseException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } catch (JsonMappingException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } return null; } @@ -87,7 +91,7 @@ public class JacksonUtil { System.out.println(json); System.out.println(readValue(json, Map.class)); } catch (Exception e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } } }