-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
尹吉欢
committed
Mar 25, 2018
1 parent
db71628
commit 6668561
Showing
7 changed files
with
219 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...lastic-job-starter/src/main/java/com/cxytiandi/elasticjob/dynamic/bean/JobProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.cxytiandi.elasticjob.dynamic.bean; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import lombok.Getter; | ||
@Getter | ||
public class JobProperties { | ||
|
||
/** | ||
* 自定义异常处理类 | ||
* @return | ||
*/ | ||
@JsonProperty("job_exception_handler") | ||
private String jobExceptionHandler = "com.dangdang.ddframe.job.executor.handler.impl.DefaultJobExceptionHandler"; | ||
|
||
/** | ||
* 自定义业务处理线程池 | ||
* @return | ||
*/ | ||
@JsonProperty("executor_service_handler") | ||
private String executorServiceHandler = "com.dangdang.ddframe.job.executor.handler.impl.DefaultExecutorServiceHandler"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...ot-elastic-job-starter/src/main/java/com/cxytiandi/elasticjob/dynamic/util/JsonUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.cxytiandi.elasticjob.dynamic.util; | ||
|
||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
/** | ||
* Json 工具类 | ||
* @author yinjihuan | ||
* | ||
*/ | ||
public class JsonUtils { | ||
private static ObjectMapper mapper = new ObjectMapper(); | ||
|
||
public static String toString(Object obj){ | ||
return toJson(obj); | ||
} | ||
|
||
public static String toJson(Object obj){ | ||
try{ | ||
StringWriter writer = new StringWriter(); | ||
mapper.writeValue(writer, obj); | ||
return writer.toString(); | ||
}catch(Exception e){ | ||
throw new RuntimeException("序列化对象【"+obj+"】时出错", e); | ||
} | ||
} | ||
|
||
public static <T> T toBean(Class<T> entityClass, String jsonString){ | ||
try { | ||
return mapper.readValue(jsonString, entityClass); | ||
} catch (Exception e) { | ||
throw new RuntimeException("JSON【"+jsonString+"】转对象时出错", e); | ||
} | ||
} | ||
|
||
/** | ||
* 用于对象通过其他工具已转为JSON的字符形式,这里不需要再加上引号 | ||
* @param obj | ||
* @param isObject | ||
*/ | ||
public static String getJsonSuccess(String obj, boolean isObject){ | ||
String jsonString = null; | ||
if(obj == null){ | ||
jsonString = "{\"success\":true}"; | ||
}else{ | ||
jsonString = "{\"success\":true,\"data\":"+obj+"}"; | ||
} | ||
return jsonString; | ||
} | ||
|
||
public static String getJsonSuccess(Object obj){ | ||
return getJsonSuccess(obj, null); | ||
} | ||
|
||
public static String getJsonSuccess(Object obj, String message) { | ||
if(obj == null){ | ||
return "{\"success\":true,\"message\":\""+message+"\"}"; | ||
}else{ | ||
try{ | ||
Map<String, Object> map = new HashMap<String, Object>(); | ||
map.put("success", true); | ||
return "{\"success\":true,"+toString(obj)+",\"message\":\""+message+"\"}"; | ||
}catch(Exception e){ | ||
throw new RuntimeException("序列化对象【"+obj+"】时出错", e); | ||
} | ||
} | ||
} | ||
|
||
public static String getJsonError(Object obj){ | ||
return getJsonError(obj, null); | ||
} | ||
|
||
public static String getJsonError(Object obj, String message) { | ||
if(obj == null){ | ||
return "{\"success\":false,\"message\":\""+message+"\"}"; | ||
}else{ | ||
try{ | ||
obj = parseIfException(obj); | ||
return "{\"success\":false,\"data\":"+toString(obj)+",\"message\":\""+message+"\"}"; | ||
}catch(Exception e){ | ||
throw new RuntimeException("序列化对象【"+obj+"】时出错", e); | ||
} | ||
} | ||
} | ||
|
||
public static Object parseIfException(Object obj){ | ||
if(obj instanceof Exception){ | ||
return getErrorMessage((Exception) obj, null); | ||
} | ||
return obj; | ||
} | ||
|
||
public static String getErrorMessage(Exception e, String defaultMessage){ | ||
return defaultMessage != null ? defaultMessage : null; | ||
} | ||
|
||
public static ObjectMapper getMapper() { | ||
return mapper; | ||
} | ||
} |
Oops, something went wrong.