From aba36d3139dec154eb50d222469ed41edd46c84b Mon Sep 17 00:00:00 2001 From: "xueli.xue" Date: Sat, 28 May 2016 18:55:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=9B=9E=E8=B0=83=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../XxlJobLogCallbackServerHandler.java | 6 ++-- .../main/webapp/static/js/jobinfo.index.1.js | 22 +++++++------- .../job/core/handler/HandlerRepository.java | 30 +++++++++++++++++++ .../xxl/job/core/handler/HandlerThread.java | 29 +++++++----------- .../service/jobhandler/DemoJobHandler.java | 7 +++++ 5 files changed, 62 insertions(+), 32 deletions(-) diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/callback/XxlJobLogCallbackServerHandler.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/callback/XxlJobLogCallbackServerHandler.java index 9c9a3d31..af5ef762 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/callback/XxlJobLogCallbackServerHandler.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/callback/XxlJobLogCallbackServerHandler.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); diff --git a/xxl-job-admin/src/main/webapp/static/js/jobinfo.index.1.js b/xxl-job-admin/src/main/webapp/static/js/jobinfo.index.1.js index 59740805..b0e640f8 100644 --- a/xxl-job-admin/src/main/webapp/static/js/jobinfo.index.1.js +++ b/xxl-job-admin/src/main/webapp/static/js/jobinfo.index.1.js @@ -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); } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerRepository.java b/xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerRepository.java index 5fe5966d..3a55f9c4 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerRepository.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerRepository.java @@ -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> callBackQueue = new LinkedBlockingQueue>(); + static { + new Thread(new Runnable() { + @Override + public void run() { + try { + HashMap 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 params){ + params.put("_address", address); + callBackQueue.add(params); + } + } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerThread.java b/xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerThread.java index 4384d707..67a4e5d8 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerThread.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/handler/HandlerThread.java @@ -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 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 params = new HashMap(); - 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 params = new HashMap(); + 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(); diff --git a/xxl-job-executor-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java b/xxl-job-executor-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java index 58a53c4f..f95654d9 100644 --- a/xxl-job-executor-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java +++ b/xxl-job-executor-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java @@ -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; }