|
|
@@ -1,8 +1,10 @@
|
|
1
|
1
|
package com.xxl.job.core.executor.jetty;
|
|
2
|
2
|
|
|
3
|
|
-import com.xxl.job.core.router.HandlerRouter;
|
|
4
|
3
|
import com.xxl.job.core.handler.IJobHandler;
|
|
5
|
4
|
import com.xxl.job.core.handler.annotation.JobHander;
|
|
|
5
|
+import com.xxl.job.core.registry.RegistHelper;
|
|
|
6
|
+import com.xxl.job.core.router.HandlerRouter;
|
|
|
7
|
+import com.xxl.job.core.util.IpUtil;
|
|
6
|
8
|
import org.eclipse.jetty.server.Connector;
|
|
7
|
9
|
import org.eclipse.jetty.server.Handler;
|
|
8
|
10
|
import org.eclipse.jetty.server.Server;
|
|
|
@@ -16,6 +18,7 @@ import org.springframework.context.ApplicationContext;
|
|
16
|
18
|
import org.springframework.context.ApplicationContextAware;
|
|
17
|
19
|
|
|
18
|
20
|
import java.util.Map;
|
|
|
21
|
+import java.util.concurrent.TimeUnit;
|
|
19
|
22
|
|
|
20
|
23
|
/**
|
|
21
|
24
|
* Created by xuxueli on 2016/3/2 21:14.
|
|
|
@@ -24,15 +27,23 @@ public class XxlJobExecutor implements ApplicationContextAware {
|
|
24
|
27
|
private static final Logger logger = LoggerFactory.getLogger(XxlJobExecutor.class);
|
|
25
|
28
|
|
|
26
|
29
|
private int port = 9999;
|
|
|
30
|
+ private String appName;
|
|
|
31
|
+ private RegistHelper registHelper;
|
|
27
|
32
|
public void setPort(int port) {
|
|
28
|
33
|
this.port = port;
|
|
29
|
34
|
}
|
|
|
35
|
+ public void setAppName(String appName) {
|
|
|
36
|
+ this.appName = appName;
|
|
|
37
|
+ }
|
|
|
38
|
+ public void setRegistHelper(RegistHelper registHelper) {
|
|
|
39
|
+ this.registHelper = registHelper;
|
|
|
40
|
+ }
|
|
30
|
41
|
|
|
31
|
42
|
// ---------------------------------- job server ------------------------------------
|
|
32
|
43
|
Server server = null;
|
|
33
|
44
|
public void start() throws Exception {
|
|
34
|
45
|
|
|
35
|
|
- new Thread(new Runnable() {
|
|
|
46
|
+ Thread executorTnread = new Thread(new Runnable() {
|
|
36
|
47
|
@Override
|
|
37
|
48
|
public void run() {
|
|
38
|
49
|
server = new Server();
|
|
|
@@ -52,14 +63,16 @@ public class XxlJobExecutor implements ApplicationContextAware {
|
|
52
|
63
|
try {
|
|
53
|
64
|
server.start();
|
|
54
|
65
|
logger.info(">>>>>>>>>>>> xxl-job jetty server start success at port:{}.", port);
|
|
55
|
|
- server.join(); // block until server ready
|
|
|
66
|
+ registryBeat();
|
|
|
67
|
+ server.join(); // block until thread stopped
|
|
56
|
68
|
logger.info(">>>>>>>>>>>> xxl-job jetty server join success at port:{}.", port);
|
|
57
|
69
|
} catch (Exception e) {
|
|
58
|
70
|
e.printStackTrace();
|
|
59
|
71
|
}
|
|
60
|
72
|
}
|
|
61
|
|
- }).start();
|
|
62
|
|
-
|
|
|
73
|
+ });
|
|
|
74
|
+ executorTnread.setDaemon(true); // daemon, service jvm, user thread leave >>> daemon leave >>> jvm leave
|
|
|
75
|
+ executorTnread.start();
|
|
63
|
76
|
}
|
|
64
|
77
|
|
|
65
|
78
|
public void destroy(){
|
|
|
@@ -72,6 +85,28 @@ public class XxlJobExecutor implements ApplicationContextAware {
|
|
72
|
85
|
}
|
|
73
|
86
|
}
|
|
74
|
87
|
|
|
|
88
|
+ private void registryBeat(){
|
|
|
89
|
+ if (registHelper==null && appName==null || appName.trim().length()==0) {
|
|
|
90
|
+ return;
|
|
|
91
|
+ }
|
|
|
92
|
+ Thread registryThread = new Thread(new Runnable() {
|
|
|
93
|
+ @Override
|
|
|
94
|
+ public void run() {
|
|
|
95
|
+ while (true) {
|
|
|
96
|
+ try {
|
|
|
97
|
+ String address = IpUtil.getIp().concat(":").concat(String.valueOf(port));
|
|
|
98
|
+ registHelper.registry(RegistHelper.RegistType.EXECUTOR.name(), appName, address);
|
|
|
99
|
+ TimeUnit.SECONDS.sleep(15);
|
|
|
100
|
+ } catch (Exception e) {
|
|
|
101
|
+ e.printStackTrace();
|
|
|
102
|
+ }
|
|
|
103
|
+ }
|
|
|
104
|
+ }
|
|
|
105
|
+ });
|
|
|
106
|
+ registryThread.setDaemon(true);
|
|
|
107
|
+ registryThread.start();
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
75
|
110
|
// ---------------------------------- init job handler ------------------------------------
|
|
76
|
111
|
public static ApplicationContext applicationContext;
|
|
77
|
112
|
@Override
|