任务回调逻辑优化

v1.3
xueli.xue 9 years ago
parent 71e12272c3
commit aba36d3139

@ -28,15 +28,15 @@ public class XxlJobLogCallbackServerHandler extends AbstractHandler {
httpServletResponse.setCharacterEncoding("UTF-8"); httpServletResponse.setCharacterEncoding("UTF-8");
// parse param // parse param
String trigger_log_id = httpServletRequest.getParameter("trigger_log_id"); String log_id = httpServletRequest.getParameter("log_id");
String status = httpServletRequest.getParameter("status"); String status = httpServletRequest.getParameter("status");
String msg = httpServletRequest.getParameter("msg"); String msg = httpServletRequest.getParameter("msg");
// process // process
RemoteCallBack callBack = new RemoteCallBack(); RemoteCallBack callBack = new RemoteCallBack();
callBack.setStatus(RemoteCallBack.FAIL); callBack.setStatus(RemoteCallBack.FAIL);
if (StringUtils.isNumeric(trigger_log_id) && StringUtils.isNotBlank(status)) { if (StringUtils.isNumeric(log_id) && StringUtils.isNotBlank(status)) {
XxlJobLog log = DynamicSchedulerUtil.xxlJobLogDao.load(Integer.valueOf(trigger_log_id)); XxlJobLog log = DynamicSchedulerUtil.xxlJobLogDao.load(Integer.valueOf(log_id));
if (log!=null) { if (log!=null) {
log.setHandleTime(new Date()); log.setHandleTime(new Date());
log.setHandleStatus(status); log.setHandleStatus(status);

@ -317,27 +317,27 @@ $(function() {
// GLUE模式开启 // GLUE模式开启
$("#addModal .form .ifGLUE").click(function(){ $("#addModal .form .ifGLUE").click(function(){
var ifGLUE = $(this).is(':checked'); var ifGLUE = $(this).is(':checked');
var $handler_name = $("#addModal .form input[name='handler_name']"); var $executorHandler = $("#addModal .form input[name='executorHandler']");
var $glueSwitch = $("#addModal .form input[name='glueSwitch']"); var $glueSwitch = $("#addModal .form input[name='glueSwitch']");
if (ifGLUE) { if (ifGLUE) {
$handler_name.val(""); $executorHandler.val("");
$handler_name.attr("readonly","readonly"); $executorHandler.attr("readonly","readonly");
$glueSwitch.val(1); $glueSwitch.val(1);
} else { } else {
$handler_name.removeAttr("readonly"); $executorHandler.removeAttr("readonly");
$glueSwitch.val(0); $glueSwitch.val(0);
} }
}); });
$("#updateModal .form .ifGLUE").click(function(){ $("#updateModal .form .ifGLUE").click(function(){
var ifGLUE = $(this).is(':checked'); var ifGLUE = $(this).is(':checked');
var $handler_name = $("#updateModal .form input[name='handler_name']"); var $executorHandler = $("#updateModal .form input[name='executorHandler']");
var $glueSwitch = $("#updateModal .form input[name='glueSwitch']"); var $glueSwitch = $("#updateModal .form input[name='glueSwitch']");
if (ifGLUE) { if (ifGLUE) {
$handler_name.val(""); $executorHandler.val("");
$handler_name.attr("readonly","readonly"); $executorHandler.attr("readonly","readonly");
$glueSwitch.val(1); $glueSwitch.val(1);
} else { } else {
$handler_name.removeAttr("readonly"); $executorHandler.removeAttr("readonly");
$glueSwitch.val(0); $glueSwitch.val(0);
} }
}); });
@ -358,12 +358,12 @@ $(function() {
// GLUE check // GLUE check
var $glueSwitch = $("#updateModal .form input[name='glueSwitch']"); var $glueSwitch = $("#updateModal .form input[name='glueSwitch']");
var $handler_name = $("#updateModal .form input[name='handler_name']"); var $executorHandler = $("#updateModal .form input[name='executorHandler']");
if ($glueSwitch.val() != 0) { if ($glueSwitch.val() != 0) {
$handler_name.attr("readonly","readonly"); $executorHandler.attr("readonly","readonly");
$("#updateModal .form .ifGLUE").attr("checked", true); $("#updateModal .form .ifGLUE").attr("checked", true);
} else { } else {
$handler_name.removeAttr("readonly"); $executorHandler.removeAttr("readonly");
$("#updateModal .form .ifGLUE").attr("checked", false); $("#updateModal .form .ifGLUE").attr("checked", false);
} }

@ -1,14 +1,17 @@
package com.xxl.job.core.handler; package com.xxl.job.core.handler;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.xxl.job.core.handler.impl.GlueJobHandler; import com.xxl.job.core.handler.impl.GlueJobHandler;
import com.xxl.job.core.log.XxlJobFileAppender; import com.xxl.job.core.log.XxlJobFileAppender;
import com.xxl.job.core.util.HttpUtil;
import com.xxl.job.core.util.HttpUtil.RemoteCallBack; import com.xxl.job.core.util.HttpUtil.RemoteCallBack;
import com.xxl.job.core.util.JacksonUtil; import com.xxl.job.core.util.JacksonUtil;
@ -195,4 +198,31 @@ public class HandlerRepository {
return JacksonUtil.writeValueAsString(callback); return JacksonUtil.writeValueAsString(callback);
} }
// ----------------------- for callback log -----------------------
private static LinkedBlockingQueue<HashMap<String, String>> callBackQueue = new LinkedBlockingQueue<HashMap<String, String>>();
static {
new Thread(new Runnable() {
@Override
public void run() {
try {
HashMap<String, String> item = callBackQueue.poll();
if (item != null) {
RemoteCallBack callback = null;
try {
callback = HttpUtil.post(item.get("_address"), item);
} catch (Exception e) {
logger.info("HandlerThread Exception:", e);
}
logger.info(">>>>>>>>>>> xxl-job callback , params:{}, result:{}", new Object[]{item, callback});
}
} catch (Exception e) {
}
}
});
}
public static void pushCallBack(String address, HashMap<String, String> params){
params.put("_address", address);
callBackQueue.add(params);
}
} }

@ -15,7 +15,6 @@ import com.xxl.job.core.handler.HandlerRepository.HandlerParamEnum;
import com.xxl.job.core.handler.IJobHandler.JobHandleStatus; import com.xxl.job.core.handler.IJobHandler.JobHandleStatus;
import com.xxl.job.core.log.XxlJobFileAppender; import com.xxl.job.core.log.XxlJobFileAppender;
import com.xxl.job.core.util.HttpUtil; import com.xxl.job.core.util.HttpUtil;
import com.xxl.job.core.util.HttpUtil.RemoteCallBack;
/** /**
* handler thread * handler thread
@ -61,10 +60,10 @@ public class HandlerThread extends Thread{
Map<String, String> handlerData = handlerDataQueue.poll(); Map<String, String> handlerData = handlerDataQueue.poll();
if (handlerData!=null) { if (handlerData!=null) {
i= 0; i= 0;
String trigger_log_address = handlerData.get(HandlerParamEnum.LOG_ADDRESS.name()); String log_address = handlerData.get(HandlerParamEnum.LOG_ADDRESS.name());
String trigger_log_id = handlerData.get(HandlerParamEnum.LOG_ID.name()); String log_id = handlerData.get(HandlerParamEnum.LOG_ID.name());
String handler_params = handlerData.get(HandlerParamEnum.EXECUTOR_PARAMS.name()); String handler_params = handlerData.get(HandlerParamEnum.EXECUTOR_PARAMS.name());
logIdSet.remove(trigger_log_id); logIdSet.remove(log_id);
// parse param // parse param
String[] handlerParams = null; String[] handlerParams = null;
@ -78,7 +77,7 @@ public class HandlerThread extends Thread{
JobHandleStatus _status = JobHandleStatus.FAIL; JobHandleStatus _status = JobHandleStatus.FAIL;
String _msg = null; String _msg = null;
try { try {
XxlJobFileAppender.contextHolder.set(trigger_log_id); XxlJobFileAppender.contextHolder.set(log_id);
logger.info(">>>>>>>>>>> xxl-job handle start."); logger.info(">>>>>>>>>>> xxl-job handle start.");
_status = handler.execute(handlerParams); _status = handler.execute(handlerParams);
} catch (Exception e) { } catch (Exception e) {
@ -91,19 +90,13 @@ public class HandlerThread extends Thread{
new Object[]{handlerParams, _status, _msg}); new Object[]{handlerParams, _status, _msg});
// callback handler info // callback handler info
if (!toStop) {
HashMap<String, String> params = new HashMap<String, String>(); HashMap<String, String> params = new HashMap<String, String>();
params.put("trigger_log_id", trigger_log_id); params.put("log_id", log_id);
params.put("status", _status.name()); params.put("status", _status.name());
params.put("msg", _msg); params.put("msg", _msg);
RemoteCallBack callback = null; HandlerRepository.pushCallBack(HttpUtil.addressToUrl(log_address), params);
logger.info(">>>>>>>>>>> xxl-job callback start.");
try {
callback = HttpUtil.post(HttpUtil.addressToUrl(trigger_log_address), params);
} catch (Exception e) {
logger.info("HandlerThread Exception:", e);
} }
logger.info(">>>>>>>>>>> xxl-job callback end, params:{}, result:{}", new Object[]{params, callback.toString()});
} else { } else {
i++; i++;
logIdSet.clear(); logIdSet.clear();

@ -1,5 +1,7 @@
package com.xxl.job.executor.service.jobhandler; package com.xxl.job.executor.service.jobhandler;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -26,6 +28,11 @@ public class DemoJobHandler extends IJobHandler {
@Override @Override
public JobHandleStatus execute(String... params) throws Exception { public JobHandleStatus execute(String... params) throws Exception {
logger.info("XXL-JOB, Hello World."); logger.info("XXL-JOB, Hello World.");
for (int i = 0; i < 10; i++) {
System.out.println(i);
TimeUnit.SECONDS.sleep(2);
}
return JobHandleStatus.SUCCESS; return JobHandleStatus.SUCCESS;
} }

Loading…
Cancel
Save