Skip to content

Commit 8aa4fc5

Browse files
committed
增加和spring集成的例子
1 parent e8af9d1 commit 8aa4fc5

File tree

22 files changed

+762
-81
lines changed

22 files changed

+762
-81
lines changed

job-client/src/main/java/com/lts/job/client/RetryJobClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.lts.job.core.file.Line;
99
import com.lts.job.core.support.RetryScheduler;
1010
import com.lts.job.core.util.JSONUtils;
11+
import com.lts.job.core.util.StringUtils;
1112

1213
import java.util.ArrayList;
1314
import java.util.Arrays;

job-core/src/main/java/com/lts/job/core/support/RetryScheduler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void start() {
5959
}
6060

6161
// 一次最多提交maxSentSize个, 保证文件所也能被其他线程拿到
62-
private static int maxSentSize = 20;
62+
private int maxSentSize = 20;
6363

6464
public void stop() {
6565
RETRY_EXECUTOR_SERVICE.shutdown();
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.lts.job.core.util;
2+
3+
import java.util.Collection;
4+
import java.util.Map;
5+
6+
/**
7+
* Created by hugui on 3/6/15.
8+
*/
9+
public class Assert {
10+
11+
public static void isTrue(boolean expression, String message) {
12+
if (!expression) {
13+
throw new IllegalArgumentException(message);
14+
}
15+
}
16+
17+
public static void isTrue(boolean expression) {
18+
isTrue(expression, "[Assertion failed] - this expression must be true");
19+
}
20+
21+
public static void isNull(Object object, String message) {
22+
if (object != null) {
23+
throw new IllegalArgumentException(message);
24+
}
25+
}
26+
27+
public static void isNull(Object object) {
28+
isNull(object, "[Assertion failed] - the object argument must be null");
29+
}
30+
31+
public static void notNull(Object object, String message) {
32+
if (object == null) {
33+
throw new IllegalArgumentException(message);
34+
}
35+
}
36+
37+
public static void notNull(Object object) {
38+
notNull(object, "[Assertion failed] - this argument is required; it must not be null");
39+
}
40+
41+
public static void hasLength(String text, String message) {
42+
if (!StringUtils.hasLength(text)) {
43+
throw new IllegalArgumentException(message);
44+
}
45+
}
46+
47+
public static void hasLength(String text) {
48+
hasLength(text,
49+
"[Assertion failed] - this String argument must have length; it must not be null or empty");
50+
}
51+
52+
public static void hasText(String text, String message) {
53+
if (!StringUtils.hasText(text)) {
54+
throw new IllegalArgumentException(message);
55+
}
56+
}
57+
58+
public static void hasText(String text) {
59+
hasText(text,
60+
"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
61+
}
62+
63+
public static void doesNotContain(String textToSearch, String substring, String message) {
64+
if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
65+
textToSearch.contains(substring)) {
66+
throw new IllegalArgumentException(message);
67+
}
68+
}
69+
70+
public static void doesNotContain(String textToSearch, String substring) {
71+
doesNotContain(textToSearch, substring,
72+
"[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
73+
}
74+
75+
public static void notEmpty(Object[] array, String message) {
76+
if (array == null || array.length == 0) {
77+
throw new IllegalArgumentException(message);
78+
}
79+
}
80+
81+
public static void notEmpty(Object[] array) {
82+
notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
83+
}
84+
85+
public static void noNullElements(Object[] array, String message) {
86+
if (array != null) {
87+
for (Object element : array) {
88+
if (element == null) {
89+
throw new IllegalArgumentException(message);
90+
}
91+
}
92+
}
93+
}
94+
95+
public static void noNullElements(Object[] array) {
96+
noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
97+
}
98+
99+
public static void notEmpty(Collection collection, String message) {
100+
if (CollectionUtils.isEmpty(collection)) {
101+
throw new IllegalArgumentException(message);
102+
}
103+
}
104+
105+
public static void notEmpty(Collection collection) {
106+
notEmpty(collection,
107+
"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
108+
}
109+
110+
public static void notEmpty(Map map, String message) {
111+
if (CollectionUtils.isEmpty(map)) {
112+
throw new IllegalArgumentException(message);
113+
}
114+
}
115+
116+
public static void notEmpty(Map map) {
117+
notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
118+
}
119+
120+
public static void isInstanceOf(Class<?> clazz, Object obj) {
121+
isInstanceOf(clazz, obj, "");
122+
}
123+
124+
public static void isInstanceOf(Class<?> type, Object obj, String message) {
125+
notNull(type, "Type to check against must not be null");
126+
if (!type.isInstance(obj)) {
127+
throw new IllegalArgumentException(
128+
(StringUtils.hasLength(message) ? message + " " : "") +
129+
"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
130+
"] must be an instance of " + type);
131+
}
132+
}
133+
134+
public static void isAssignable(Class<?> superType, Class<?> subType) {
135+
isAssignable(superType, subType, "");
136+
}
137+
138+
public static void isAssignable(Class<?> superType, Class<?> subType, String message) {
139+
notNull(superType, "Type to check against must not be null");
140+
if (subType == null || !superType.isAssignableFrom(subType)) {
141+
throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);
142+
}
143+
}
144+
145+
public static void state(boolean expression, String message) {
146+
if (!expression) {
147+
throw new IllegalStateException(message);
148+
}
149+
}
150+
151+
public static void state(boolean expression) {
152+
state(expression, "[Assertion failed] - this state invariant must be true");
153+
}
154+
155+
}

job-core/src/main/java/com/lts/job/core/util/StringUtils.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.lts.job.core.util;
22

3+
import org.slf4j.helpers.FormattingTuple;
4+
import org.slf4j.helpers.MessageFormatter;
5+
36
/**
47
* @author Robert HG (254963746@qq.com) on 7/24/14.
58
*/
@@ -16,8 +19,8 @@ public static boolean isNotEmpty(String s) {
1619
return !isEmpty(s);
1720
}
1821

19-
public static String generateUUID(){
20-
return StringUtils.replace(java.util.UUID.randomUUID().toString(),"-","").toUpperCase();
22+
public static String generateUUID() {
23+
return StringUtils.replace(java.util.UUID.randomUUID().toString(), "-", "").toUpperCase();
2124
}
2225

2326
public static String replace(String text, String repl, String with) {
@@ -49,4 +52,26 @@ public static String replace(String text, String repl, String with, int max) {
4952
buf.append(text.substring(start));
5053
return buf.toString();
5154
}
55+
56+
public static boolean hasLength(String str) {
57+
return (str != null && str.length() > 0);
58+
}
59+
60+
public static boolean hasText(String str) {
61+
if (!hasLength(str)) {
62+
return false;
63+
}
64+
int strLen = str.length();
65+
for (int i = 0; i < strLen; i++) {
66+
if (!Character.isWhitespace(str.charAt(i))) {
67+
return true;
68+
}
69+
}
70+
return false;
71+
}
72+
73+
public static String format(String format, Object...args){
74+
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
75+
return ft.getMessage();
76+
}
5277
}

job-example/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@
2626
<artifactId>job-client</artifactId>
2727
<version>${project.version}</version>
2828
</dependency>
29+
<dependency>
30+
<groupId>com.lts</groupId>
31+
<artifactId>job-ext-spring</artifactId>
32+
<version>${project.version}</version>
33+
</dependency>
2934
</dependencies>
3035
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.lts.job.example.api;
2+
3+
import com.lts.job.client.JobClient;
4+
import com.lts.job.client.RetryJobClient;
5+
import com.lts.job.example.support.BaseJobClientTest;
6+
import com.lts.job.example.support.JobFinishedHandlerImpl;
7+
import com.lts.job.example.support.MasterNodeChangeListenerImpl;
8+
9+
import java.io.IOException;
10+
11+
/**
12+
* @author Robert HG (254963746@qq.com) on 8/13/14.
13+
*/
14+
public class JobClientTest extends BaseJobClientTest {
15+
16+
public static void main(String[] args) throws IOException {
17+
18+
JobClient jobClient = new RetryJobClient();
19+
// final JobClient jobClient = new JobClient();
20+
jobClient.setNodeGroup("TEST");
21+
// jobClient.setClusterName("lts");
22+
jobClient.setZookeeperAddress("localhost:2181");
23+
// 任务重试保存地址,默认用户目录下
24+
// jobClient.setJobInfoSavePath(Constants.USER_HOME);
25+
jobClient.setJobFinishedHandler(new JobFinishedHandlerImpl());
26+
jobClient.addMasterNodeChangeListener(new MasterNodeChangeListenerImpl());
27+
jobClient.start();
28+
29+
JobClientTest jobClientTest = new JobClientTest();
30+
jobClientTest.jobClient = jobClient;
31+
jobClientTest.startConsole();
32+
}
33+
34+
}

job-example/src/main/java/com/lts/job/example/jobtracker/JobTrackerTest.java renamed to job-example/src/main/java/com/lts/job/example/api/JobTrackerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.lts.job.example.jobtracker;
1+
package com.lts.job.example.api;
22

33
import com.lts.job.store.Config;
44
import com.lts.job.tracker.JobTracker;
@@ -16,7 +16,7 @@ public static void main(String[] args) {
1616
// 节点信息配置
1717
jobTracker.setZookeeperAddress("localhost:2181");
1818
// jobTracker.setListenPort(35001); // 默认 35001
19-
// jobTracker.setClusterName("QN");
19+
// jobTracker.setClusterName("lts");
2020

2121
// mongo 配置 (也可以配置在 mongo.properties中)
2222
Config config = new Config();

job-example/src/main/java/com/lts/job/example/tasktracker/TaskTrackerTest.java renamed to job-example/src/main/java/com/lts/job/example/api/TaskTrackerTest.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package com.lts.job.example.tasktracker;
1+
package com.lts.job.example.api;
22

3-
import com.lts.job.core.cluster.Node;
4-
import com.lts.job.core.listener.MasterNodeChangeListener;
3+
import com.lts.job.example.support.MasterNodeChangeListenerImpl;
4+
import com.lts.job.example.support.TestJobRunner;
55
import com.lts.job.task.tracker.TaskTracker;
66

77
import java.io.IOException;
@@ -17,10 +17,10 @@ public static void main(String[] args) {
1717

1818
taskTracker.setZookeeperAddress("localhost:2181");
1919
taskTracker.setNodeGroup("TEST_TRADE");
20-
// taskTracker.setClusterName("QN");
20+
// taskTracker.setClusterName("lts");
2121
taskTracker.setWorkThreads(20);
2222
// taskTracker.setJobInfoSavePath(Constants.USER_HOME);
23-
taskTracker.addMasterNodeChangeListener(new MasterListener());
23+
taskTracker.addMasterNodeChangeListener(new MasterNodeChangeListenerImpl());
2424

2525
taskTracker.start();
2626

@@ -38,19 +38,4 @@ public void run() {
3838
}
3939

4040
}
41-
}
42-
43-
class MasterListener implements MasterNodeChangeListener{
44-
45-
/**
46-
* master 为 master节点
47-
* isMaster 表示当前节点是不是master节点
48-
* @param master
49-
* @param isMaster
50-
*/
51-
@Override
52-
public void change(Node master, boolean isMaster) {
53-
54-
// 一个节点组master节点变化后的处理
55-
}
5641
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.lts.job.example.spring;
2+
3+
import com.lts.job.client.JobClient;
4+
import com.lts.job.example.support.BaseJobClientTest;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
import java.io.IOException;
8+
import java.text.ParseException;
9+
10+
/**
11+
* Created by hugui on 3/6/15.
12+
*/
13+
14+
public class JobClientTest extends BaseJobClientTest {
15+
16+
public static void main(String[] args) throws ParseException, IOException {
17+
18+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("lts-spring-job-client.xml");
19+
final JobClient jobClient = (JobClient) context.getBean("jobClient");
20+
21+
JobClientTest jobClientTest = new JobClientTest();
22+
jobClientTest.jobClient = jobClient;
23+
jobClientTest.startConsole();
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lts.job.example.spring;
2+
3+
import com.lts.job.tracker.JobTracker;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
/**
7+
* @author Robert HG (254963746@qq.com) on 8/13/14.
8+
*/
9+
public class JobTrackerTest {
10+
11+
public static void main(String[] args) {
12+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("lts-spring-job-tacker.xml");
13+
JobTracker jobTracker = (JobTracker) context.getBean("jobTracker");
14+
}
15+
}

0 commit comments

Comments
 (0)