parent
39a0205e89
commit
8b71e2cec4
@ -0,0 +1,99 @@
|
||||
package com.xxl.job.core.router;
|
||||
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.router.action.BeatAction;
|
||||
import com.xxl.job.core.router.action.KillAction;
|
||||
import com.xxl.job.core.router.action.LogAction;
|
||||
import com.xxl.job.core.router.action.RunAction;
|
||||
import com.xxl.job.core.router.model.RequestModel;
|
||||
import com.xxl.job.core.router.model.ResponseModel;
|
||||
import com.xxl.job.core.router.thread.JobThread;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* handler repository
|
||||
* @author xuxueli 2015-12-19 19:28:44
|
||||
*/
|
||||
public class HandlerRouter {
|
||||
private static Logger logger = LoggerFactory.getLogger(HandlerRouter.class);
|
||||
|
||||
/**
|
||||
* job handler repository
|
||||
*/
|
||||
private static ConcurrentHashMap<String, IJobHandler> jobHandlerRepository = new ConcurrentHashMap<String, IJobHandler>();
|
||||
public static IJobHandler registJobHandler(String name, IJobHandler jobHandler){
|
||||
logger.info("xxl-job register jobhandler success, name:{}, jobHandler:{}", name, jobHandler);
|
||||
return HandlerRouter.jobHandlerRepository.put(name, jobHandler);
|
||||
}
|
||||
public static IJobHandler loadJobHandler(String name){
|
||||
return HandlerRouter.jobHandlerRepository.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* job thread repository
|
||||
*/
|
||||
private static ConcurrentHashMap<String, JobThread> JobThreadRepository = new ConcurrentHashMap<String, JobThread>();
|
||||
public static JobThread registJobThread(String jobkey, IJobHandler handler){
|
||||
JobThread handlerThread = new JobThread(handler);
|
||||
handlerThread.start();
|
||||
logger.info(">>>>>>>>>>> xxl-job regist handler success, jobkey:{}, handler:{}", new Object[]{jobkey, handler});
|
||||
return HandlerRouter.JobThreadRepository.put(jobkey, handlerThread); // putIfAbsent
|
||||
}
|
||||
public static JobThread loadJobThread(String jobKey){
|
||||
return HandlerRouter.JobThreadRepository.get(jobKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* route action repository
|
||||
*/
|
||||
public enum ActionRepository {
|
||||
RUN(new RunAction()),
|
||||
KILL(new KillAction()),
|
||||
LOG(new LogAction()),
|
||||
BEAT(new BeatAction());
|
||||
|
||||
private IAction action;
|
||||
private ActionRepository(IAction action){
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
/**
|
||||
* match Action by enum name
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static IAction matchAction(String name){
|
||||
if (name!=null && name.trim().length()>0) {
|
||||
for (ActionRepository item : ActionRepository.values()) {
|
||||
if (item.name().equals(name)) {
|
||||
return item.action;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// handler push to queue
|
||||
public static ResponseModel route(RequestModel requestModel) {
|
||||
logger.debug(">>>>>>>>>>> xxl-job route, RequestModel:{}", new Object[]{requestModel.toString()});
|
||||
|
||||
// timestamp check
|
||||
if (System.currentTimeMillis() - requestModel.getTimestamp() > 60000) {
|
||||
return new ResponseModel(ResponseModel.SUCCESS, "Timestamp Timeout.");
|
||||
}
|
||||
|
||||
// match action
|
||||
IAction action = ActionRepository.matchAction(requestModel.getAction());
|
||||
if (action == null) {
|
||||
return new ResponseModel(ResponseModel.SUCCESS, "Action match fail.");
|
||||
}
|
||||
|
||||
return action.execute(requestModel);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.xxl.job.core.router;
|
||||
|
||||
import com.xxl.job.core.router.model.RequestModel;
|
||||
import com.xxl.job.core.router.model.ResponseModel;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/7/22.
|
||||
*/
|
||||
public abstract class IAction {
|
||||
|
||||
public abstract ResponseModel execute(RequestModel requestModel);
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.xxl.job.core.router.action;
|
||||
|
||||
import com.xxl.job.core.router.IAction;
|
||||
import com.xxl.job.core.router.model.RequestModel;
|
||||
import com.xxl.job.core.router.model.ResponseModel;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/7/22.
|
||||
*/
|
||||
public class BeatAction extends IAction {
|
||||
|
||||
@Override
|
||||
public ResponseModel execute(RequestModel requestModel) {
|
||||
return new ResponseModel(ResponseModel.SUCCESS, "i am alive.");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.xxl.job.core.router.action;
|
||||
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.router.HandlerRouter;
|
||||
import com.xxl.job.core.router.IAction;
|
||||
import com.xxl.job.core.router.model.RequestModel;
|
||||
import com.xxl.job.core.router.model.ResponseModel;
|
||||
import com.xxl.job.core.router.thread.JobThread;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/7/22.
|
||||
*/
|
||||
public class KillAction extends IAction {
|
||||
|
||||
@Override
|
||||
public ResponseModel execute(RequestModel requestModel) {
|
||||
|
||||
// generate jobKey
|
||||
String jobKey = requestModel.getJobGroup().concat("_").concat(requestModel.getJobName());
|
||||
|
||||
// kill handlerThread, and create new one
|
||||
JobThread jobThread = HandlerRouter.loadJobThread(jobKey);
|
||||
|
||||
if (jobThread != null) {
|
||||
IJobHandler handler = jobThread.getHandler();
|
||||
jobThread.toStop("人工手动终止");
|
||||
jobThread.interrupt();
|
||||
HandlerRouter.registJobThread(jobKey, handler);
|
||||
return new ResponseModel(ResponseModel.SUCCESS, "job thread kull success.");
|
||||
}
|
||||
|
||||
return new ResponseModel(ResponseModel.FAIL, "job thread not found.");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.xxl.job.core.router.action;
|
||||
|
||||
import com.xxl.job.core.log.XxlJobFileAppender;
|
||||
import com.xxl.job.core.router.IAction;
|
||||
import com.xxl.job.core.router.model.RequestModel;
|
||||
import com.xxl.job.core.router.model.ResponseModel;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/7/22.
|
||||
*/
|
||||
public class LogAction extends IAction {
|
||||
|
||||
@Override
|
||||
public ResponseModel execute(RequestModel requestModel) {
|
||||
String logConteng = XxlJobFileAppender.readLog(new Date(requestModel.getLogDateTim()), requestModel.getLogId());
|
||||
return new ResponseModel(ResponseModel.SUCCESS, logConteng);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.xxl.job.core.router.action;
|
||||
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.handler.impl.GlueJobHandler;
|
||||
import com.xxl.job.core.router.HandlerRouter;
|
||||
import com.xxl.job.core.router.IAction;
|
||||
import com.xxl.job.core.router.model.RequestModel;
|
||||
import com.xxl.job.core.router.model.ResponseModel;
|
||||
import com.xxl.job.core.router.thread.JobThread;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/7/22.
|
||||
*/
|
||||
public class RunAction extends IAction {
|
||||
|
||||
@Override
|
||||
public ResponseModel execute(RequestModel requestModel) {
|
||||
|
||||
// generate jobKey
|
||||
String jobKey = requestModel.getJobGroup().concat("_").concat(requestModel.getJobName());
|
||||
|
||||
// load old thread
|
||||
JobThread jobThread = HandlerRouter.loadJobThread(jobKey);
|
||||
|
||||
if (!requestModel.isGlueSwitch()) {
|
||||
// bean model
|
||||
|
||||
// handler instance
|
||||
IJobHandler jobHandler = HandlerRouter.loadJobHandler(requestModel.getExecutorHandler());
|
||||
|
||||
if (jobThread == null) {
|
||||
// jobhandler match
|
||||
if (jobHandler==null) {
|
||||
return new ResponseModel(ResponseModel.FAIL, "job handler for jobKey=[" + jobKey + "] not found.");
|
||||
}
|
||||
jobThread = HandlerRouter.registJobThread(jobKey, jobHandler);
|
||||
} else {
|
||||
|
||||
// job handler update, kill old job thread
|
||||
if (jobThread.getHandler() != jobHandler) {
|
||||
|
||||
// kill old job thread
|
||||
jobThread.toStop("人工手动终止");
|
||||
jobThread.interrupt();
|
||||
|
||||
// new thread, with new job handler
|
||||
jobThread = HandlerRouter.registJobThread(jobKey, jobHandler);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// glue model
|
||||
|
||||
if (jobThread == null) {
|
||||
jobThread = HandlerRouter.registJobThread(jobKey, new GlueJobHandler(requestModel.getJobGroup(), requestModel.getJobName()));
|
||||
}
|
||||
}
|
||||
|
||||
// sometime, cmap.get can not return given value, i do not know why
|
||||
jobThread = HandlerRouter.loadJobThread(jobKey);
|
||||
|
||||
// push data to queue
|
||||
jobThread.pushTriggerQueue(requestModel);
|
||||
return new ResponseModel(ResponseModel.SUCCESS, null);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
package com.xxl.job.core.router.model;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/7/22.
|
||||
*/
|
||||
public class RequestModel {
|
||||
|
||||
private long timestamp;
|
||||
private String action;
|
||||
|
||||
private String jobGroup;
|
||||
private String jobName;
|
||||
|
||||
private String executorHandler;
|
||||
private String executorParams;
|
||||
|
||||
private boolean glueSwitch;
|
||||
|
||||
private String logAddress;
|
||||
private int logId;
|
||||
private long logDateTim;
|
||||
|
||||
private String status;
|
||||
private String msg;
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getJobGroup() {
|
||||
return jobGroup;
|
||||
}
|
||||
|
||||
public void setJobGroup(String jobGroup) {
|
||||
this.jobGroup = jobGroup;
|
||||
}
|
||||
|
||||
public String getJobName() {
|
||||
return jobName;
|
||||
}
|
||||
|
||||
public void setJobName(String jobName) {
|
||||
this.jobName = jobName;
|
||||
}
|
||||
|
||||
public String getExecutorHandler() {
|
||||
return executorHandler;
|
||||
}
|
||||
|
||||
public void setExecutorHandler(String executorHandler) {
|
||||
this.executorHandler = executorHandler;
|
||||
}
|
||||
|
||||
public String getExecutorParams() {
|
||||
return executorParams;
|
||||
}
|
||||
|
||||
public void setExecutorParams(String executorParams) {
|
||||
this.executorParams = executorParams;
|
||||
}
|
||||
|
||||
public boolean isGlueSwitch() {
|
||||
return glueSwitch;
|
||||
}
|
||||
|
||||
public void setGlueSwitch(boolean glueSwitch) {
|
||||
this.glueSwitch = glueSwitch;
|
||||
}
|
||||
|
||||
public String getLogAddress() {
|
||||
return logAddress;
|
||||
}
|
||||
|
||||
public void setLogAddress(String logAddress) {
|
||||
this.logAddress = logAddress;
|
||||
}
|
||||
|
||||
public int getLogId() {
|
||||
return logId;
|
||||
}
|
||||
|
||||
public void setLogId(int logId) {
|
||||
this.logId = logId;
|
||||
}
|
||||
|
||||
public long getLogDateTim() {
|
||||
return logDateTim;
|
||||
}
|
||||
|
||||
public void setLogDateTim(long logDateTim) {
|
||||
this.logDateTim = logDateTim;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RequestModel{" +
|
||||
"timestamp=" + timestamp +
|
||||
", action='" + action + '\'' +
|
||||
", jobGroup='" + jobGroup + '\'' +
|
||||
", jobName='" + jobName + '\'' +
|
||||
", executorHandler='" + executorHandler + '\'' +
|
||||
", executorParams='" + executorParams + '\'' +
|
||||
", glueSwitch=" + glueSwitch +
|
||||
", logAddress='" + logAddress + '\'' +
|
||||
", logId=" + logId +
|
||||
", logDateTim=" + logDateTim +
|
||||
", status='" + status + '\'' +
|
||||
", msg='" + msg + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.xxl.job.core.router.model;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/7/22.
|
||||
*/
|
||||
public class ResponseModel {
|
||||
public static final String SUCCESS = "SUCCESS";
|
||||
public static final String FAIL = "FAIL";
|
||||
|
||||
private String status;
|
||||
private String msg;
|
||||
|
||||
public ResponseModel() {
|
||||
}
|
||||
|
||||
public ResponseModel(String status, String msg) {
|
||||
this.status = status;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ResponseModel{" +
|
||||
"status='" + status + '\'' +
|
||||
", msg='" + msg + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.xxl.job.core.router.thread;
|
||||
|
||||
import com.xxl.job.core.router.model.RequestModel;
|
||||
import com.xxl.job.core.router.model.ResponseModel;
|
||||
import com.xxl.job.core.util.XxlJobNetCommUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/7/22.
|
||||
*/
|
||||
public class TriggerCallbackThread {
|
||||
private static Logger logger = LoggerFactory.getLogger(TriggerCallbackThread.class);
|
||||
|
||||
private static LinkedBlockingQueue<RequestModel> callBackQueue = new LinkedBlockingQueue<RequestModel>();
|
||||
static {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while(true){
|
||||
try {
|
||||
RequestModel callback = callBackQueue.take();
|
||||
if (callback != null) {
|
||||
try {
|
||||
ResponseModel responseModel = XxlJobNetCommUtil.postHex(XxlJobNetCommUtil.addressToUrl(callback.getLogAddress()), callback);
|
||||
logger.info(">>>>>>>>>>> xxl-job callback , RequestModel:{}, ResponseModel:{}", new Object[]{callback.toString(), responseModel.toString()});
|
||||
} catch (Exception e) {
|
||||
logger.info("JobThread Exception:", e);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
public static void pushCallBack(RequestModel callback){
|
||||
callBackQueue.add(callback);
|
||||
}
|
||||
|
||||
}
|
@ -1,130 +0,0 @@
|
||||
package com.xxl.job.core.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
/**
|
||||
* http util to send data
|
||||
* @author xuxueli
|
||||
* @version 2015-11-28 15:30:59
|
||||
*/
|
||||
public class HttpUtil {
|
||||
|
||||
/**
|
||||
* http remote callback
|
||||
*/
|
||||
public static class RemoteCallBack{
|
||||
public static final String SUCCESS = "SUCCESS";
|
||||
public static final String FAIL = "FAIL";
|
||||
|
||||
private String status;
|
||||
private String msg;
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RemoteCallBack [status=" + status + ", msg=" + msg + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* http post request
|
||||
* @param reqURL
|
||||
* @param params
|
||||
* @return [0]=responseMsg, [1]=exceptionMsg
|
||||
*/
|
||||
public static RemoteCallBack post(String reqURL, Map<String, String> params){
|
||||
RemoteCallBack callback = new RemoteCallBack();
|
||||
callback.setStatus(RemoteCallBack.FAIL);
|
||||
|
||||
// do post
|
||||
HttpPost httpPost = null;
|
||||
CloseableHttpClient httpClient = null;
|
||||
try{
|
||||
httpPost = new HttpPost(reqURL);
|
||||
if (params != null && !params.isEmpty()) {
|
||||
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
|
||||
for(Map.Entry<String,String> entry : params.entrySet()){
|
||||
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
|
||||
}
|
||||
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
|
||||
httpPost.setConfig(requestConfig);
|
||||
|
||||
//httpClient = HttpClients.createDefault(); // default retry 3 times
|
||||
httpClient = HttpClients.custom().disableAutomaticRetries().build();
|
||||
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
if (null != entity) {
|
||||
String responseMsg = EntityUtils.toString(entity, "UTF-8");
|
||||
callback = JacksonUtil.readValue(responseMsg, RemoteCallBack.class);
|
||||
if (callback == null) {
|
||||
callback = new RemoteCallBack();
|
||||
callback.setStatus(RemoteCallBack.FAIL);
|
||||
callback.setMsg("responseMsg parse json fail, responseMsg:" + responseMsg);
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
}
|
||||
} else {
|
||||
callback.setMsg("http statusCode error, statusCode:" + response.getStatusLine().getStatusCode());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
/*StringWriter out = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(out));
|
||||
callback.setMsg(out.toString());*/
|
||||
callback.setMsg(e.getMessage());
|
||||
} finally{
|
||||
if (httpPost!=null) {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
if (httpClient!=null) {
|
||||
try {
|
||||
httpClient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse address ip:port to url http://.../
|
||||
* @param address
|
||||
* @return
|
||||
*/
|
||||
public static String addressToUrl(String address){
|
||||
return "http://" + address + "/";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
package com.xxl.job.core.util;
|
||||
|
||||
import com.xxl.job.core.router.model.RequestModel;
|
||||
import com.xxl.job.core.router.model.ResponseModel;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* http util to send data
|
||||
* @author xuxueli
|
||||
* @version 2015-11-28 15:30:59
|
||||
*/
|
||||
public class XxlJobNetCommUtil {
|
||||
private static Logger logger = LoggerFactory.getLogger(XxlJobNetCommUtil.class);
|
||||
|
||||
// hex param key
|
||||
public static final String HEX = "hex";
|
||||
|
||||
/**
|
||||
* format object to hex-json
|
||||
* @param obj
|
||||
* @return
|
||||
*/
|
||||
public static String formatObj2HexJson(Object obj){
|
||||
String json = JacksonUtil.writeValueAsString(obj);
|
||||
String hex = ByteHexConverter.byte2hex(json.getBytes());
|
||||
return hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse hex-json to object
|
||||
* @param hex
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static <T> T parseHexJson2Obj(String hex, Class<T> clazz){
|
||||
String json = new String(ByteHexConverter.hex2Byte(hex));
|
||||
T obj = JacksonUtil.readValue(json, clazz);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(parseHexJson2Obj("7B22737461747573223A2253554343455353222C226D7367223A2254696D657374616D702054696D656F75742E227D", ResponseModel.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* http post request
|
||||
* @param reqURL
|
||||
*/
|
||||
public static ResponseModel postHex(String reqURL, RequestModel requestModel){
|
||||
|
||||
// parse RequestModel to hex-json
|
||||
String requestHex = XxlJobNetCommUtil.formatObj2HexJson(requestModel);
|
||||
|
||||
// msg
|
||||
String failMsg = null;
|
||||
|
||||
// do post
|
||||
HttpPost httpPost = null;
|
||||
CloseableHttpClient httpClient = null;
|
||||
try{
|
||||
httpPost = new HttpPost(reqURL);
|
||||
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
|
||||
formParams.add(new BasicNameValuePair(XxlJobNetCommUtil.HEX, requestHex));
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
|
||||
|
||||
|
||||
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
|
||||
httpPost.setConfig(requestConfig);
|
||||
|
||||
//httpClient = HttpClients.createDefault(); // default retry 3 times
|
||||
httpClient = HttpClients.custom().disableAutomaticRetries().build();
|
||||
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (response.getStatusLine().getStatusCode() == 200 && null != entity) {
|
||||
String responseHex = EntityUtils.toString(entity, "UTF-8");
|
||||
EntityUtils.consume(entity);
|
||||
|
||||
// i do not know why
|
||||
responseHex = responseHex.replace("\n", "");
|
||||
|
||||
// parse hex-json to ResponseModel
|
||||
ResponseModel responseModel = XxlJobNetCommUtil.parseHexJson2Obj(responseHex, ResponseModel.class);
|
||||
|
||||
if (responseModel!=null) {
|
||||
return responseModel;
|
||||
}
|
||||
} else {
|
||||
failMsg = "http statusCode error, statusCode:" + response.getStatusLine().getStatusCode();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.info("", e);
|
||||
/*StringWriter out = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(out));
|
||||
callback.setMsg(out.toString());*/
|
||||
failMsg = e.getMessage();
|
||||
} finally{
|
||||
if (httpPost!=null) {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
if (httpClient!=null) {
|
||||
try {
|
||||
httpClient.close();
|
||||
} catch (IOException e) {
|
||||
logger.info("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// other, default fail
|
||||
ResponseModel callback = new ResponseModel();
|
||||
callback.setStatus(ResponseModel.FAIL);
|
||||
callback.setMsg(failMsg);
|
||||
return callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse address ip:port to url http://.../
|
||||
* @param address
|
||||
* @return
|
||||
*/
|
||||
public static String addressToUrl(String address){
|
||||
return "http://" + address + "/";
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue