提交 1970f7f3 编写于 作者: 许雪里's avatar 许雪里

Clean Code

上级 3595d9c0
package com.xxl.job.admin.controller.resolver; 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.biz.model.ReturnT;
import com.xxl.job.core.util.JacksonUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
......
...@@ -21,7 +21,7 @@ package com.xxl.job.admin.core.jobbean.impl; ...@@ -21,7 +21,7 @@ package com.xxl.job.admin.core.jobbean.impl;
// try { // try {
// TimeUnit.SECONDS.sleep(10); // TimeUnit.SECONDS.sleep(10);
// } catch (InterruptedException e) { // } catch (InterruptedException e) {
// e.printStackTrace(); // logger.error(e.getMessage(), e);
// } // }
// //
// return false; // return false;
......
...@@ -23,7 +23,7 @@ package com.xxl.job.admin.core.jobbean.impl; ...@@ -23,7 +23,7 @@ package com.xxl.job.admin.core.jobbean.impl;
// try { // try {
// TimeUnit.SECONDS.sleep(10); // TimeUnit.SECONDS.sleep(10);
// } catch (InterruptedException e) { // } catch (InterruptedException e) {
// e.printStackTrace(); // logger.error(e.getMessage(), e);
// } // }
// //
// return false; // return false;
......
...@@ -143,7 +143,7 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware { ...@@ -143,7 +143,7 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware {
} }
} catch (SchedulerException e) { } catch (SchedulerException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
} }
...@@ -377,7 +377,7 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware { ...@@ -377,7 +377,7 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware {
} }
} catch (SchedulerException e) { } catch (SchedulerException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
return null; return null;
} }
return jobList; return jobList;
......
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> T readValue(String jsonStr, Class<T> 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> T readValueRefer(String jsonStr, Class<T> clazz) {
try {
return getInstance().readValue(jsonStr, new TypeReference<T>() { });
} 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<String, String> map = new HashMap<String, String>();
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();
}
}
}
...@@ -147,7 +147,7 @@ public class MailUtil { ...@@ -147,7 +147,7 @@ public class MailUtil {
mailSender.send(mimeMessage); mailSender.send(mimeMessage);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
return false; return false;
} }
......
...@@ -226,7 +226,7 @@ public class XxlJobServiceImpl implements XxlJobService { ...@@ -226,7 +226,7 @@ public class XxlJobServiceImpl implements XxlJobService {
xxlJobLogGlueDao.deleteByJobId(id); xxlJobLogGlueDao.deleteByJobId(id);
return ReturnT.SUCCESS; return ReturnT.SUCCESS;
} catch (SchedulerException e) { } catch (SchedulerException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
return ReturnT.FAIL; return ReturnT.FAIL;
} }
...@@ -241,7 +241,7 @@ public class XxlJobServiceImpl implements XxlJobService { ...@@ -241,7 +241,7 @@ public class XxlJobServiceImpl implements XxlJobService {
boolean ret = XxlJobDynamicScheduler.pauseJob(name, group); // jobStatus do not store boolean ret = XxlJobDynamicScheduler.pauseJob(name, group); // jobStatus do not store
return ret?ReturnT.SUCCESS:ReturnT.FAIL; return ret?ReturnT.SUCCESS:ReturnT.FAIL;
} catch (SchedulerException e) { } catch (SchedulerException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
return ReturnT.FAIL; return ReturnT.FAIL;
} }
} }
...@@ -256,7 +256,7 @@ public class XxlJobServiceImpl implements XxlJobService { ...@@ -256,7 +256,7 @@ public class XxlJobServiceImpl implements XxlJobService {
boolean ret = XxlJobDynamicScheduler.resumeJob(name, group); boolean ret = XxlJobDynamicScheduler.resumeJob(name, group);
return ret?ReturnT.SUCCESS:ReturnT.FAIL; return ret?ReturnT.SUCCESS:ReturnT.FAIL;
} catch (SchedulerException e) { } catch (SchedulerException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
return ReturnT.FAIL; return ReturnT.FAIL;
} }
} }
...@@ -271,7 +271,7 @@ public class XxlJobServiceImpl implements XxlJobService { ...@@ -271,7 +271,7 @@ public class XxlJobServiceImpl implements XxlJobService {
XxlJobDynamicScheduler.triggerJob(name, group); XxlJobDynamicScheduler.triggerJob(name, group);
return ReturnT.SUCCESS; return ReturnT.SUCCESS;
} catch (SchedulerException e) { } catch (SchedulerException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
return ReturnT.FAIL; return ReturnT.FAIL;
} }
} }
......
...@@ -43,7 +43,7 @@ public class AdminBizTest { ...@@ -43,7 +43,7 @@ public class AdminBizTest {
AdminBiz adminBiz = (AdminBiz) new NetComClientProxy(AdminBiz.class, addressUrl, accessToken).getObject(); AdminBiz adminBiz = (AdminBiz) new NetComClientProxy(AdminBiz.class, addressUrl, accessToken).getObject();
int jobId = 1; int jobId = 1;
ReturnT<String> returnT = adminBiz.triggerJob(1); ReturnT<String> returnT = adminBiz.triggerJob(jobId);
Assert.assertTrue(returnT.getCode() == ReturnT.SUCCESS_CODE); Assert.assertTrue(returnT.getCode() == ReturnT.SUCCESS_CODE);
} }
......
...@@ -75,9 +75,9 @@ public class GlueFactory { ...@@ -75,9 +75,9 @@ public class GlueFactory {
try { try {
field.set(instance, fieldBean); field.set(instance, fieldBean);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
} }
} }
......
...@@ -174,13 +174,13 @@ public class XxlJobFileAppender { ...@@ -174,13 +174,13 @@ public class XxlJobFileAppender {
return sb.toString(); return sb.toString();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} finally { } finally {
if (reader != null) { if (reader != null) {
try { try {
reader.close(); reader.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
} }
} }
......
...@@ -71,7 +71,7 @@ public class HttpClientUtil { ...@@ -71,7 +71,7 @@ public class HttpClientUtil {
try { try {
httpClient.close(); httpClient.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
} }
return responseBytes; return responseBytes;
...@@ -101,7 +101,7 @@ public class HttpClientUtil { ...@@ -101,7 +101,7 @@ public class HttpClientUtil {
} }
return message; return message;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
throw e; throw e;
} }
} }
......
...@@ -6,6 +6,8 @@ import org.codehaus.jackson.JsonParseException; ...@@ -6,6 +6,8 @@ import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference; import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
...@@ -20,6 +22,8 @@ import java.util.Map; ...@@ -20,6 +22,8 @@ import java.util.Map;
* @author xuxueli 2015-9-25 18:02:56 * @author xuxueli 2015-9-25 18:02:56
*/ */
public class JacksonUtil { public class JacksonUtil {
private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class);
private final static ObjectMapper objectMapper = new ObjectMapper(); private final static ObjectMapper objectMapper = new ObjectMapper();
public static ObjectMapper getInstance() { public static ObjectMapper getInstance() {
return objectMapper; return objectMapper;
...@@ -36,11 +40,11 @@ public class JacksonUtil { ...@@ -36,11 +40,11 @@ public class JacksonUtil {
try { try {
return getInstance().writeValueAsString(obj); return getInstance().writeValueAsString(obj);
} catch (JsonGenerationException e) { } catch (JsonGenerationException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} catch (JsonMappingException e) { } catch (JsonMappingException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
return null; return null;
} }
...@@ -57,11 +61,11 @@ public class JacksonUtil { ...@@ -57,11 +61,11 @@ public class JacksonUtil {
try { try {
return getInstance().readValue(jsonStr, clazz); return getInstance().readValue(jsonStr, clazz);
} catch (JsonParseException e) { } catch (JsonParseException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} catch (JsonMappingException e) { } catch (JsonMappingException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
return null; return null;
} }
...@@ -69,11 +73,11 @@ public class JacksonUtil { ...@@ -69,11 +73,11 @@ public class JacksonUtil {
try { try {
return getInstance().readValue(jsonStr, new TypeReference<T>() { }); return getInstance().readValue(jsonStr, new TypeReference<T>() { });
} catch (JsonParseException e) { } catch (JsonParseException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} catch (JsonMappingException e) { } catch (JsonMappingException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
return null; return null;
} }
...@@ -87,7 +91,7 @@ public class JacksonUtil { ...@@ -87,7 +91,7 @@ public class JacksonUtil {
System.out.println(json); System.out.println(json);
System.out.println(readValue(json, Map.class)); System.out.println(readValue(json, Map.class));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error(e.getMessage(), e);
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册