Bläddra i källkod

重构通讯逻辑

xueli.xue 9 år sedan
förälder
incheckning
c1363c7567

+ 1 - 1
README.md Visa fil

@@ -49,7 +49,7 @@ git.osc地址:http://git.oschina.net/xuxueli0323/xxl-job
49 49
 
50 50
 # 新版本 V1.3.x,新特性
51 51
 	1、遗弃“本地任务”模式,推荐使用“远程任务”,易于系统解耦,任务对应的JobHander统称为“执行器”;
52
-	2、遗弃“servlet”方式底层系统通讯,推荐使用JETTY方式,重构通讯逻辑;
52
+	2、遗弃“servlet”方式底层系统通讯,推荐使用JETTY方式,调度+回调双向通讯,重构通讯逻辑;
53 53
 	3、UI交互优化:左侧菜单展开状态优化,菜单项选中状态优化,任务列表打开表格有压缩优化;
54 54
 	4、【重要】“执行器”细分为:BEAN、GLUE两种开发模式,简介见下文:
55 55
 	

+ 2 - 3
xxl-job-admin/src/main/java/com/xxl/job/controller/JobLogController.java Visa fil

@@ -19,7 +19,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
19 19
 import com.xxl.job.client.handler.HandlerRepository;
20 20
 import com.xxl.job.client.util.HttpUtil;
21 21
 import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
22
-import com.xxl.job.controller.annotation.PermessionLimit;
23 22
 import com.xxl.job.client.util.JacksonUtil;
24 23
 import com.xxl.job.core.constant.Constants.JobGroupEnum;
25 24
 import com.xxl.job.core.model.ReturnT;
@@ -76,7 +75,7 @@ public class JobLogController {
76 75
 		return maps;
77 76
 	}
78 77
 	
79
-	@RequestMapping("/save")
78
+	/*@RequestMapping("/save")
80 79
 	@ResponseBody
81 80
 	@PermessionLimit(limit=false)
82 81
 	public RemoteCallBack triggerLog(int trigger_log_id, String status, String msg) {
@@ -92,7 +91,7 @@ public class JobLogController {
92 91
 			return callBack;
93 92
 		}
94 93
 		return callBack;
95
-	}
94
+	}*/
96 95
 	
97 96
 	@RequestMapping("/logDetail")
98 97
 	@ResponseBody

+ 62 - 0
xxl-job-admin/src/main/java/com/xxl/job/core/callback/XxlJobCallbackServer.java Visa fil

@@ -0,0 +1,62 @@
1
+package com.xxl.job.core.callback;
2
+
3
+import org.eclipse.jetty.server.Connector;
4
+import org.eclipse.jetty.server.Handler;
5
+import org.eclipse.jetty.server.Server;
6
+import org.eclipse.jetty.server.handler.HandlerCollection;
7
+import org.eclipse.jetty.server.nio.SelectChannelConnector;
8
+import org.eclipse.jetty.util.thread.ExecutorThreadPool;
9
+import org.slf4j.Logger;
10
+import org.slf4j.LoggerFactory;
11
+
12
+import com.xxl.job.client.util.IpUtil;
13
+
14
+/**
15
+ * Created by xuxueli on 2016-5-22 11:15:42
16
+ */
17
+public class XxlJobCallbackServer {
18
+    private static final Logger logger = LoggerFactory.getLogger(XxlJobCallbackServer.class);
19
+
20
+    private static String trigger_log_address;
21
+    public static String getTrigger_log_address() {
22
+		return trigger_log_address;
23
+	}
24
+    
25
+    public void start(int callBackPort) throws Exception {
26
+    	// init address
27
+    	
28
+    	String ip = IpUtil.getIp();
29
+    	trigger_log_address = ip.concat(":").concat(String.valueOf(callBackPort));
30
+		
31
+    	final int port = Integer.valueOf(callBackPort);
32
+        new Thread(new Runnable() {
33
+            @Override
34
+            public void run() {
35
+                Server server = new Server();
36
+                server.setThreadPool(new ExecutorThreadPool(200, 200, 30000));	// 非阻塞
37
+
38
+                // connector
39
+                SelectChannelConnector connector = new SelectChannelConnector();
40
+                connector.setPort(port);
41
+                connector.setMaxIdleTime(30000);
42
+                server.setConnectors(new Connector[] { connector });
43
+
44
+                // handler
45
+                HandlerCollection handlerc =new HandlerCollection();
46
+                handlerc.setHandlers(new Handler[]{new XxlJobCallbackServerHandler()});
47
+                server.setHandler(handlerc);
48
+
49
+                try {
50
+                    server.start();
51
+                    logger.info(">>>>>>>>>>>> xxl-job XxlJobCallbackServer start success at port:{}.", port);
52
+                    server.join();  // block until server ready
53
+                    logger.info(">>>>>>>>>>>> xxl-job XxlJobCallbackServer join success at port:{}.", port);
54
+                } catch (Exception e) {
55
+                    e.printStackTrace();
56
+                }
57
+            }
58
+        }).start();
59
+
60
+    }
61
+
62
+}

+ 57 - 0
xxl-job-admin/src/main/java/com/xxl/job/core/callback/XxlJobCallbackServerHandler.java Visa fil

@@ -0,0 +1,57 @@
1
+package com.xxl.job.core.callback;
2
+
3
+import java.io.IOException;
4
+import java.util.Date;
5
+
6
+import javax.servlet.ServletException;
7
+import javax.servlet.http.HttpServletRequest;
8
+import javax.servlet.http.HttpServletResponse;
9
+
10
+import org.apache.commons.lang.StringUtils;
11
+import org.eclipse.jetty.server.Request;
12
+import org.eclipse.jetty.server.handler.AbstractHandler;
13
+
14
+import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
15
+import com.xxl.job.client.util.JacksonUtil;
16
+import com.xxl.job.core.model.XxlJobLog;
17
+import com.xxl.job.core.util.DynamicSchedulerUtil;
18
+
19
+/**
20
+ * Created by xuxueli on 2016-5-22 11:15:42
21
+ */
22
+public class XxlJobCallbackServerHandler extends AbstractHandler {
23
+
24
+	@Override
25
+	public void handle(String s, Request baseRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
26
+
27
+		httpServletRequest.setCharacterEncoding("UTF-8");
28
+		httpServletResponse.setCharacterEncoding("UTF-8");
29
+
30
+		// parse param
31
+		String trigger_log_id = httpServletRequest.getParameter("trigger_log_id");
32
+		String status = httpServletRequest.getParameter("status");
33
+		String msg = httpServletRequest.getParameter("msg");
34
+		
35
+		// process
36
+		RemoteCallBack callBack = new RemoteCallBack();
37
+		callBack.setStatus(RemoteCallBack.FAIL);
38
+		if (StringUtils.isNumeric(trigger_log_id) && StringUtils.isNotBlank(status)) {
39
+			XxlJobLog log = DynamicSchedulerUtil.xxlJobLogDao.load(Integer.valueOf(trigger_log_id));
40
+			if (log!=null) {
41
+				log.setHandleTime(new Date());
42
+				log.setHandleStatus(status);
43
+				log.setHandleMsg(msg);
44
+				DynamicSchedulerUtil.xxlJobLogDao.updateHandleInfo(log);
45
+				callBack.setStatus(RemoteCallBack.SUCCESS);
46
+			}
47
+		}
48
+		String resp = JacksonUtil.writeValueAsString(callBack);
49
+
50
+		// response
51
+		httpServletResponse.setContentType("text/html;charset=utf-8");
52
+		httpServletResponse.setStatus(HttpServletResponse.SC_OK);
53
+		baseRequest.setHandled(true);
54
+		httpServletResponse.getWriter().println(resp);
55
+	}
56
+
57
+}

+ 17 - 0
xxl-job-admin/src/main/java/com/xxl/job/core/util/DynamicSchedulerUtil.java Visa fil

@@ -32,6 +32,7 @@ import org.springframework.context.ApplicationContextAware;
32 32
 import org.springframework.util.Assert;
33 33
 
34 34
 import com.xxl.job.client.util.JacksonUtil;
35
+import com.xxl.job.core.callback.XxlJobCallbackServer;
35 36
 import com.xxl.job.core.model.XxlJobInfo;
36 37
 import com.xxl.job.dao.IXxlJobInfoDao;
37 38
 import com.xxl.job.dao.IXxlJobLogDao;
@@ -49,6 +50,22 @@ public final class DynamicSchedulerUtil implements ApplicationContextAware, Init
49 50
 		DynamicSchedulerUtil.scheduler = scheduler;
50 51
 	}
51 52
     
53
+    // trigger callback port
54
+    private int callBackPort = 8888;
55
+    public void setCallBackPort(int callBackPort) {
56
+		this.callBackPort = callBackPort;
57
+	}
58
+    
59
+    // init
60
+    public void init(){
61
+    	try {
62
+    		// start callback server
63
+			new XxlJobCallbackServer().start(callBackPort);
64
+		} catch (Exception e) {
65
+			e.printStackTrace();
66
+		}
67
+    }
68
+    
52 69
     // xxlJobLogDao、xxlJobInfoDao
53 70
     public static IXxlJobLogDao xxlJobLogDao;
54 71
     public static IXxlJobInfoDao xxlJobInfoDao;

+ 3 - 6
xxl-job-admin/src/main/java/com/xxl/job/service/job/RemoteHttpJobBean.java Visa fil

@@ -16,11 +16,11 @@ import com.xxl.job.client.handler.HandlerRepository;
16 16
 import com.xxl.job.client.util.HttpUtil;
17 17
 import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
18 18
 import com.xxl.job.client.util.JacksonUtil;
19
+import com.xxl.job.core.callback.XxlJobCallbackServer;
19 20
 import com.xxl.job.core.model.XxlJobInfo;
20 21
 import com.xxl.job.core.model.XxlJobLog;
21 22
 import com.xxl.job.core.thread.JobMonitorHelper;
22 23
 import com.xxl.job.core.util.DynamicSchedulerUtil;
23
-import com.xxl.job.core.util.PropertiesUtil;
24 24
 
25 25
 /**
26 26
  * http job bean
@@ -58,8 +58,8 @@ public class RemoteHttpJobBean extends QuartzJobBean {
58 58
 		params.put(HandlerRepository.TRIGGER_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
59 59
 		params.put(HandlerRepository.NAMESPACE, HandlerRepository.NameSpaceEnum.RUN.name());
60 60
 		
61
-		params.put(HandlerRepository.TRIGGER_LOG_URL, PropertiesUtil.getString(HandlerRepository.TRIGGER_LOG_URL));
62 61
 		params.put(HandlerRepository.TRIGGER_LOG_ID, String.valueOf(jobLog.getId()));
62
+		params.put(HandlerRepository.TRIGGER_LOG_ADDRESS, XxlJobCallbackServer.getTrigger_log_address());
63 63
 		
64 64
 		params.put(HandlerRepository.HANDLER_NAME, jobDataMap.get(HandlerRepository.HANDLER_NAME));
65 65
 		params.put(HandlerRepository.HANDLER_PARAMS, jobDataMap.get(HandlerRepository.HANDLER_PARAMS));
@@ -71,11 +71,8 @@ public class RemoteHttpJobBean extends QuartzJobBean {
71 71
 
72 72
 		// handler address, jetty (servlet dead)
73 73
 		String handler_address = jobDataMap.get(HandlerRepository.HANDLER_ADDRESS);
74
-		if (!handler_address.startsWith("http")){
75
-			handler_address = "http://" + handler_address + "/";
76
-		}
77 74
 
78
-		RemoteCallBack callback = HttpUtil.post(handler_address, params);
75
+		RemoteCallBack callback = HttpUtil.post(HttpUtil.addressToUrl(handler_address), params);
79 76
 		logger.info(">>>>>>>>>>> xxl-job trigger http response, jobLog.id:{}, jobLog:{}, callback:{}", jobLog.getId(), jobLog, callback);
80 77
 
81 78
 		// update trigger info

+ 2 - 1
xxl-job-admin/src/main/resources/applicationcontext-trigger-db.xml Visa fil

@@ -18,9 +18,10 @@
18 18
 	</bean>
19 19
 	
20 20
 	<!-- 协同-调度器 -->
21
-	<bean id="dynamicSchedulerUtil" class="com.xxl.job.core.util.DynamicSchedulerUtil">
21
+	<bean id="dynamicSchedulerUtil" class="com.xxl.job.core.util.DynamicSchedulerUtil" init-method="init">
22 22
 		<!-- (轻易不要变更“调度器名称”, 任务创建时会绑定该“调度器名称”) -->
23 23
         <property name="scheduler" ref="quartzScheduler"/>
24
+        <property name="callBackPort" value="8888"/>
24 25
     </bean>
25 26
 
26 27
 </beans>

+ 0 - 3
xxl-job-admin/src/main/resources/config.properties Visa fil

@@ -1,6 +1,3 @@
1
-# for trigger log callback
2
-trigger_log_url=http://localhost:8080/xxl-job-admin/joblog/save
3
-
4 1
 # for email
5 2
 mail.host=smtp.163.com
6 3
 mail.port=25

+ 1 - 1
xxl-job-client/src/main/java/com/xxl/job/client/handler/HandlerRepository.java Visa fil

@@ -31,7 +31,7 @@ public class HandlerRepository {
31 31
 	public static final String HANDLER_JOB_NAME = "handler_job_name";
32 32
 	
33 33
 	public static final String TRIGGER_LOG_ID = "trigger_log_id";
34
-	public static final String TRIGGER_LOG_URL = "trigger_log_url";
34
+	public static final String TRIGGER_LOG_ADDRESS = "trigger_log_address";
35 35
 	public static final String TRIGGER_TIMESTAMP = "trigger_timestamp";
36 36
 	
37 37
 	public static ConcurrentHashMap<String, HandlerThread> handlerTreadMap = new ConcurrentHashMap<String, HandlerThread>();

+ 2 - 2
xxl-job-client/src/main/java/com/xxl/job/client/handler/HandlerThread.java Visa fil

@@ -60,7 +60,7 @@ public class HandlerThread extends Thread{
60 60
 				Map<String, String> handlerData = handlerDataQueue.poll();
61 61
 				if (handlerData!=null) {
62 62
 					i= 0;
63
-					String trigger_log_url = handlerData.get(HandlerRepository.TRIGGER_LOG_URL);
63
+					String trigger_log_address = handlerData.get(HandlerRepository.TRIGGER_LOG_ADDRESS);
64 64
 					String trigger_log_id = handlerData.get(HandlerRepository.TRIGGER_LOG_ID);
65 65
 					String handler_params = handlerData.get(HandlerRepository.HANDLER_PARAMS);
66 66
 					logIdSet.remove(trigger_log_id);
@@ -97,7 +97,7 @@ public class HandlerThread extends Thread{
97 97
 					RemoteCallBack callback = null;
98 98
 					logger.info(">>>>>>>>>>> xxl-job callback start.");
99 99
 					try {
100
-						callback = HttpUtil.post(trigger_log_url, params);
100
+						callback = HttpUtil.post(HttpUtil.addressToUrl(trigger_log_address), params);
101 101
 					} catch (Exception e) {
102 102
 						logger.info("HandlerThread Exception:", e);
103 103
 					}

+ 10 - 0
xxl-job-client/src/main/java/com/xxl/job/client/util/HttpUtil.java Visa fil

@@ -116,4 +116,14 @@ public class HttpUtil {
116 116
 		
117 117
 		return callback;
118 118
 	}
119
+	
120
+	/**
121
+	 * parse address ip:port to url http://.../ 
122
+	 * @param address
123
+	 * @return
124
+	 */
125
+	public static String addressToUrl(String address){
126
+		return "http://" + address + "/";
127
+	}
128
+	
119 129
 }

+ 50 - 0
xxl-job-client/src/main/java/com/xxl/job/client/util/IpUtil.java Visa fil

@@ -0,0 +1,50 @@
1
+package com.xxl.job.client.util;
2
+
3
+import java.net.InetAddress;
4
+import java.net.NetworkInterface;
5
+import java.net.UnknownHostException;
6
+import java.util.Enumeration;
7
+
8
+import org.slf4j.Logger;
9
+import org.slf4j.LoggerFactory;
10
+
11
+/**
12
+ * get ip
13
+ * @author xuxueli 2016-5-22 11:38:05
14
+ */
15
+public class IpUtil {
16
+	private static final Logger logger = LoggerFactory.getLogger(IpUtil.class);
17
+
18
+	/**
19
+	 * 获取本机ip
20
+	 * @return
21
+	 */
22
+	public static String getIp() {
23
+		try {
24
+			Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
25
+			InetAddress address = null;
26
+			while (interfaces.hasMoreElements()) {
27
+				NetworkInterface ni = interfaces.nextElement();
28
+				Enumeration<InetAddress> addresses = ni.getInetAddresses();
29
+				while (addresses.hasMoreElements()) {
30
+					address = addresses.nextElement();
31
+					if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1) {
32
+						return address.getHostAddress();
33
+					}
34
+				}
35
+			}
36
+			logger.info("xxl job getHostAddress fail");
37
+			return null;
38
+		} catch (Throwable t) {
39
+			logger.error("xxl job getHostAddress error, {}", t);
40
+			return null;
41
+		}
42
+	}
43
+
44
+	public static void main(String[] args) throws UnknownHostException {
45
+		System.out.println(InetAddress.getLocalHost().getCanonicalHostName());
46
+		System.out.println(InetAddress.getLocalHost().getHostName());
47
+		System.out.println(getIp());
48
+	}
49
+
50
+}