任务回调逻辑优化

master
xueli.xue 9 years ago
parent 71e12272c3
commit aba36d3139
  1. 6
      xxl-job-admin/src/main/java/com/xxl/job/admin/core/callback/XxlJobLogCallbackServerHandler.java
  2. 22
      xxl-job-admin/src/main/webapp/static/js/jobinfo.index.1.js
  3. 30
      xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerRepository.java
  4. 29
      xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerThread.java
  5. 7
      xxl-job-executor-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java

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

@ -317,27 +317,27 @@ $(function() {
// GLUE模式开启
$("#addModal .form .ifGLUE").click(function(){
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']");
if (ifGLUE) {
$handler_name.val("");
$handler_name.attr("readonly","readonly");
$executorHandler.val("");
$executorHandler.attr("readonly","readonly");
$glueSwitch.val(1);
} else {
$handler_name.removeAttr("readonly");
$executorHandler.removeAttr("readonly");
$glueSwitch.val(0);
}
});
$("#updateModal .form .ifGLUE").click(function(){
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']");
if (ifGLUE) {
$handler_name.val("");
$handler_name.attr("readonly","readonly");
$executorHandler.val("");
$executorHandler.attr("readonly","readonly");
$glueSwitch.val(1);
} else {
$handler_name.removeAttr("readonly");
$executorHandler.removeAttr("readonly");
$glueSwitch.val(0);
}
});
@ -358,12 +358,12 @@ $(function() {
// GLUE check
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) {
$handler_name.attr("readonly","readonly");
$executorHandler.attr("readonly","readonly");
$("#updateModal .form .ifGLUE").attr("checked", true);
} else {
$handler_name.removeAttr("readonly");
$executorHandler.removeAttr("readonly");
$("#updateModal .form .ifGLUE").attr("checked", false);
}

@ -1,14 +1,17 @@
package com.xxl.job.core.handler;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xxl.job.core.handler.impl.GlueJobHandler;
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.JacksonUtil;
@ -195,4 +198,31 @@ public class HandlerRepository {
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.log.XxlJobFileAppender;
import com.xxl.job.core.util.HttpUtil;
import com.xxl.job.core.util.HttpUtil.RemoteCallBack;
/**
* handler thread
@ -61,10 +60,10 @@ public class HandlerThread extends Thread{
Map<String, String> handlerData = handlerDataQueue.poll();
if (handlerData!=null) {
i= 0;
String trigger_log_address = handlerData.get(HandlerParamEnum.LOG_ADDRESS.name());
String trigger_log_id = handlerData.get(HandlerParamEnum.LOG_ID.name());
String log_address = handlerData.get(HandlerParamEnum.LOG_ADDRESS.name());
String log_id = handlerData.get(HandlerParamEnum.LOG_ID.name());
String handler_params = handlerData.get(HandlerParamEnum.EXECUTOR_PARAMS.name());
logIdSet.remove(trigger_log_id);
logIdSet.remove(log_id);
// parse param
String[] handlerParams = null;
@ -78,7 +77,7 @@ public class HandlerThread extends Thread{
JobHandleStatus _status = JobHandleStatus.FAIL;
String _msg = null;
try {
XxlJobFileAppender.contextHolder.set(trigger_log_id);
XxlJobFileAppender.contextHolder.set(log_id);
logger.info(">>>>>>>>>>> xxl-job handle start.");
_status = handler.execute(handlerParams);
} catch (Exception e) {
@ -89,21 +88,15 @@ public class HandlerThread extends Thread{
}
logger.info(">>>>>>>>>>> xxl-job handle end, handlerParams:{}, _status:{}, _msg:{}",
new Object[]{handlerParams, _status, _msg});
// callback handler info
HashMap<String, String> params = new HashMap<String, String>();
params.put("trigger_log_id", trigger_log_id);
params.put("status", _status.name());
params.put("msg", _msg);
RemoteCallBack callback = null;
logger.info(">>>>>>>>>>> xxl-job callback start.");
try {
callback = HttpUtil.post(HttpUtil.addressToUrl(trigger_log_address), params);
} catch (Exception e) {
logger.info("HandlerThread Exception:", e);
if (!toStop) {
HashMap<String, String> params = new HashMap<String, String>();
params.put("log_id", log_id);
params.put("status", _status.name());
params.put("msg", _msg);
HandlerRepository.pushCallBack(HttpUtil.addressToUrl(log_address), params);
}
logger.info(">>>>>>>>>>> xxl-job callback end, params:{}, result:{}", new Object[]{params, callback.toString()});
} else {
i++;
logIdSet.clear();

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

Loading…
Cancel
Save