Skip to content

Commit 9e9d28d

Browse files
committed
Merge pull request #4 from qq254963746/develop
实现 Logger 支持 slf4j, jcl, jdk ,log4j 扩展
2 parents f87a3f1 + d783e50 commit 9e9d28d

File tree

84 files changed

+2132
-447
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2132
-447
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import com.lts.job.remoting.netty.NettyRequestProcessor;
2323
import com.lts.job.remoting.netty.ResponseFuture;
2424
import com.lts.job.remoting.protocol.RemotingCommand;
25-
import org.slf4j.Logger;
26-
import org.slf4j.LoggerFactory;
25+
import com.lts.job.core.logger.Logger;
26+
import com.lts.job.core.logger.LoggerFactory;
2727

2828
import java.util.Arrays;
2929
import java.util.List;

job-client/src/main/java/com/lts/job/client/processor/JobFinishedProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import com.lts.job.remoting.exception.RemotingCommandException;
1010
import com.lts.job.remoting.protocol.RemotingCommand;
1111
import io.netty.channel.ChannelHandlerContext;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
12+
import com.lts.job.core.logger.Logger;
13+
import com.lts.job.core.logger.LoggerFactory;
1414

1515
import java.util.List;
1616

job-core/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,68 @@
1010
<modelVersion>4.0.0</modelVersion>
1111
<packaging>jar</packaging>
1212
<artifactId>job-core</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.google.code.morphia</groupId>
17+
<artifactId>morphia</artifactId>
18+
<scope>provided</scope>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.mongodb</groupId>
22+
<artifactId>mongo-java-driver</artifactId>
23+
<scope>provided</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.alibaba</groupId>
27+
<artifactId>fastjson</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.slf4j</groupId>
31+
<artifactId>slf4j-api</artifactId>
32+
<scope>provided</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.slf4j</groupId>
36+
<artifactId>slf4j-log4j12</artifactId>
37+
<scope>provided</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>log4j</groupId>
41+
<artifactId>log4j</artifactId>
42+
<scope>provided</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>commons-logging</groupId>
46+
<artifactId>commons-logging-api</artifactId>
47+
<scope>provided</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>io.netty</groupId>
51+
<artifactId>netty-all</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.github.sgroschupf</groupId>
55+
<artifactId>zkclient</artifactId>
56+
<scope>provided</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.netflix.curator</groupId>
60+
<artifactId>curator-framework</artifactId>
61+
<scope>provided</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>redis.clients</groupId>
65+
<artifactId>jedis</artifactId>
66+
<scope>provided</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.javassist</groupId>
70+
<artifactId>javassist</artifactId>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.fusesource.leveldbjni</groupId>
74+
<artifactId>leveldbjni-all</artifactId>
75+
</dependency>
76+
</dependencies>
1377
</project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.lts.job.core;
2+
3+
import com.lts.job.core.logger.Logger;
4+
import com.lts.job.core.logger.LoggerFactory;
5+
import com.lts.job.core.util.ClassHelper;
6+
7+
import java.net.URL;
8+
import java.security.CodeSource;
9+
import java.util.Enumeration;
10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
/**
14+
* 参考dubbo
15+
* @author Robert HG (254963746@qq.com) on 5/19/15.
16+
*/
17+
public final class Version {
18+
19+
private Version() {}
20+
21+
private static final Logger LOGGER = LoggerFactory.getLogger(Version.class);
22+
23+
private static final String VERSION = getVersion(Version.class, "1.4");
24+
25+
static {
26+
// 检查是否存在重复的jar包
27+
Version.checkDuplicate(Version.class);
28+
}
29+
30+
public static String getVersion(){
31+
return VERSION;
32+
}
33+
34+
public static String getVersion(Class<?> cls, String defaultVersion) {
35+
try {
36+
// 首先查找MANIFEST.MF规范中的版本号
37+
String version = cls.getPackage().getImplementationVersion();
38+
if (version == null || version.length() == 0) {
39+
version = cls.getPackage().getSpecificationVersion();
40+
}
41+
if (version == null || version.length() == 0) {
42+
// 如果规范中没有版本号,基于jar包名获取版本号
43+
CodeSource codeSource = cls.getProtectionDomain().getCodeSource();
44+
if(codeSource == null) {
45+
LOGGER.info("No codeSource for class " + cls.getName() + " when getVersion, use default version " + defaultVersion);
46+
}
47+
else {
48+
String file = codeSource.getLocation().getFile();
49+
if (file != null && file.length() > 0 && file.endsWith(".jar")) {
50+
file = file.substring(0, file.length() - 4);
51+
int i = file.lastIndexOf('/');
52+
if (i >= 0) {
53+
file = file.substring(i + 1);
54+
}
55+
i = file.indexOf("-");
56+
if (i >= 0) {
57+
file = file.substring(i + 1);
58+
}
59+
while (file.length() > 0 && ! Character.isDigit(file.charAt(0))) {
60+
i = file.indexOf("-");
61+
if (i >= 0) {
62+
file = file.substring(i + 1);
63+
} else {
64+
break;
65+
}
66+
}
67+
version = file;
68+
}
69+
}
70+
}
71+
// 返回版本号,如果为空返回缺省版本号
72+
return version == null || version.length() == 0 ? defaultVersion : version;
73+
} catch (Throwable e) { // 防御性容错
74+
// 忽略异常,返回缺省版本号
75+
LOGGER.error("return default version, ignore exception " + e.getMessage(), e);
76+
return defaultVersion;
77+
}
78+
}
79+
80+
public static void checkDuplicate(Class<?> cls, boolean failOnError) {
81+
checkDuplicate(cls.getName().replace('.', '/') + ".class", failOnError);
82+
}
83+
84+
public static void checkDuplicate(Class<?> cls) {
85+
checkDuplicate(cls, false);
86+
}
87+
88+
public static void checkDuplicate(String path, boolean failOnError) {
89+
try {
90+
// 在ClassPath搜文件
91+
Enumeration<URL> urls = ClassHelper.getCallerClassLoader(Version.class).getResources(path);
92+
Set<String> files = new HashSet<String>();
93+
while (urls.hasMoreElements()) {
94+
URL url = urls.nextElement();
95+
if (url != null) {
96+
String file = url.getFile();
97+
if (file != null && file.length() > 0) {
98+
files.add(file);
99+
}
100+
}
101+
}
102+
// 如果有多个,就表示重复
103+
if (files.size() > 1) {
104+
String error = "Duplicate class " + path + " in " + files.size() + " jar " + files;
105+
if (failOnError) {
106+
throw new IllegalStateException(error);
107+
} else {
108+
LOGGER.error(error);
109+
}
110+
}
111+
} catch (Throwable e) { // 防御性容错
112+
LOGGER.error(e.getMessage(), e);
113+
}
114+
}
115+
116+
}

job-core/src/main/java/com/lts/job/core/cluster/AbstractJobNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import com.lts.job.ec.EventSubscriber;
1717
import com.lts.job.ec.JvmEventCenter;
1818
import com.lts.job.ec.Observer;
19-
import org.slf4j.Logger;
20-
import org.slf4j.LoggerFactory;
19+
import com.lts.job.core.logger.Logger;
20+
import com.lts.job.core.logger.LoggerFactory;
2121

2222
import java.util.ArrayList;
2323
import java.util.List;

job-core/src/main/java/com/lts/job/core/cluster/MasterElector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.lts.job.core.Application;
44
import com.lts.job.core.listener.MasterChangeListener;
55
import com.lts.job.core.util.CollectionUtils;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
6+
import com.lts.job.core.logger.Logger;
7+
import com.lts.job.core.logger.LoggerFactory;
88

99
import java.util.ArrayList;
1010
import java.util.List;

job-core/src/main/java/com/lts/job/core/cluster/SubscribedNodeManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import com.lts.job.core.listener.NodeChangeListener;
66
import com.lts.job.core.util.CollectionUtils;
77
import com.lts.job.core.util.ListUtils;
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
8+
import com.lts.job.core.logger.Logger;
9+
import com.lts.job.core.logger.LoggerFactory;
1010

1111
import java.util.List;
1212
import java.util.concurrent.ConcurrentHashMap;

job-core/src/main/java/com/lts/job/core/compiler/Compiler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
/**
66
* Compiler. (SPI, Singleton, ThreadSafe)
7-
* @author william.liangf
87
*/
98
@SPI("javassist")
109
public interface Compiler {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* 该Compiler的封装基于 dubbo 修改,这里做统一说明
3+
* @author Robert HG (254963746@qq.com) on 5/19/15.
4+
*/
5+
package com.lts.job.core.compiler;

job-core/src/main/java/com/lts/job/core/compiler/support/AbstractCompiler.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
/*
2-
* Copyright 1999-2011 Alibaba Group.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package com.lts.job.core.compiler.support;
172

183
import com.lts.job.core.util.ClassHelper;
@@ -23,7 +8,6 @@
238

249
/**
2510
* Abstract compiler. (SPI, Prototype, ThreadSafe)
26-
* @author william.liangf
2711
*/
2812
public abstract class AbstractCompiler implements Compiler {
2913

0 commit comments

Comments
 (0)