xueli.xue 9 年之前
父節點
當前提交
f48ac05cdd

+ 19 - 1
xxl-job-core/src/main/java/com/xxl/job/core/util/ByteHexConverter.java 查看文件

@@ -42,7 +42,25 @@ public class ByteHexConverter {
42 42
 	public static byte[] radix2byte(String val, int radix){
43 43
 		return new BigInteger(val, radix).toByteArray();
44 44
 	}
45
-	 
45
+
46
+	/**
47
+	 * get length of string
48
+	 * @param str
49
+	 * @return
50
+	 */
51
+	public static int getByteLen(String str){
52
+		if (str==null || str.length()==0) {
53
+			return 0;
54
+		}
55
+		// because java base on unicode, and one china code's length is one, but it's cost 2 bytes.
56
+		int len = str.getBytes().length * 2;
57
+		if (len % 4 != 0) {
58
+			// Length is best in multiples of four
59
+			len = (len/4 + 1) * 4;
60
+		}
61
+		return len;
62
+	}
63
+
46 64
 	public static void main(String[] args) {
47 65
 		// hex - byte[] 方案A:位移
48 66
 		String temp = "1111111111113d1f3a51sd3f1a32sd1f32as1df2a13sd21f3a2s1df32a13sd2f123s2a3d13fa13sd9999999999";

+ 98 - 0
xxl-job-core/src/main/java/com/xxl/job/core/util/ByteReadFactory.java 查看文件

@@ -0,0 +1,98 @@
1
+package com.xxl.job.core.util;
2
+
3
+import org.slf4j.Logger;
4
+import org.slf4j.LoggerFactory;
5
+
6
+import java.io.UnsupportedEncodingException;
7
+
8
+/**
9
+ * byte read util
10
+ * @author xuxueli 2015-11-15 03:50:10
11
+ */
12
+public class ByteReadFactory {
13
+	private static transient Logger logger = LoggerFactory.getLogger(ByteReadFactory.class);
14
+	private int m_iPos;
15
+	private int m_iReqLen;
16
+	private byte[] m_byte = null;
17
+
18
+	public ByteReadFactory(byte[] hexBytes){
19
+		m_iPos = 0;
20
+		m_byte = hexBytes;
21
+		m_iReqLen = m_byte.length;
22
+	}
23
+	
24
+	public int readInt() {
25
+		if (m_iPos + 4 > m_iReqLen) {
26
+			return 0;
27
+		}
28
+		int iInt = (m_byte[m_iPos] & 0xff) 
29
+				| ((m_byte[m_iPos + 1] & 0xff) << 8)
30
+				| ((m_byte[m_iPos + 2] & 0xff) << 16)
31
+				| ((m_byte[m_iPos + 3] & 0xff) << 24);		
32
+		m_iPos += 4;
33
+		return iInt;
34
+	}
35
+	
36
+	public long readLong() {
37
+		if (m_iPos + 8 > m_iReqLen) {
38
+			return 0;
39
+		}
40
+		long iLong = (m_byte[m_iPos] & 0xff) 
41
+				| ((m_byte[m_iPos + 1] & 0xff) << 8)
42
+				| ((m_byte[m_iPos + 2] & 0xff) << 16)
43
+				| ((m_byte[m_iPos + 3] & 0xff) << 24)
44
+				| ((m_byte[m_iPos + 4] & 0xff) << 32)
45
+				| ((m_byte[m_iPos + 5] & 0xff) << 40)
46
+				| ((m_byte[m_iPos + 6] & 0xff) << 48)
47
+				| ((m_byte[m_iPos + 7] & 0xff) << 56);
48
+		m_iPos += 8;
49
+		return iLong;
50
+	}
51
+	
52
+	public String readString(int length) {
53
+		if (m_iPos + length > m_iReqLen) {
54
+			logger.error("[byte stream factory read string length error.]");
55
+			return "";
56
+		}
57
+		
58
+		int index = 0;
59
+		for (index = 0; index < length; index++) {
60
+			if (m_byte[m_iPos + index] == 0) {
61
+				break;
62
+			}
63
+		}
64
+		String msg = "";
65
+		try {
66
+			msg = new String(m_byte, m_iPos, index, "UTF-8");
67
+		} catch (UnsupportedEncodingException e) {
68
+			logger.error("[byte stream factory read string exception.]", e);
69
+		}
70
+		m_iPos += length;
71
+		
72
+		return msg;
73
+	}
74
+	
75
+	public byte[] read(int length) {
76
+		if (m_iPos + length > m_iReqLen || length<=0) {
77
+			logger.error("[byte stream factory read string length error.]");
78
+			return null;
79
+		}
80
+		for (int i = 0; i < length; i++) {
81
+			if (m_byte[m_iPos + i] == 0) {
82
+				break;
83
+			}
84
+		}
85
+		
86
+		byte[] result = new byte[length];
87
+		for (int i = 0; i < length; i++) {
88
+			result[i] = m_byte[m_iPos + i];
89
+		}
90
+		m_iPos += length;
91
+		return result;
92
+	}
93
+	
94
+	public byte[] readByteAll() {
95
+		return read(m_iReqLen - m_iPos);
96
+	}
97
+	
98
+}

+ 64 - 0
xxl-job-core/src/main/java/com/xxl/job/core/util/ByteWriteFactory.java 查看文件

@@ -0,0 +1,64 @@
1
+package com.xxl.job.core.util;
2
+
3
+import org.slf4j.Logger;
4
+import org.slf4j.LoggerFactory;
5
+
6
+import java.io.UnsupportedEncodingException;
7
+import java.nio.ByteBuffer;
8
+
9
+/**
10
+ * byte write util
11
+ * @author xuxueli 2015-11-15 03:49:36
12
+ */
13
+public class ByteWriteFactory {
14
+	private static transient Logger logger = LoggerFactory.getLogger(ByteWriteFactory.class);
15
+	private ByteBuffer m_byteBuf = null;	
16
+	public ByteWriteFactory() {
17
+		m_byteBuf = ByteBuffer.allocate(1024 * 4);
18
+	}
19
+	
20
+	public void writeInt(int intValue) {
21
+		byte[] intBytes = new byte[4];
22
+		for (int index = 0; index < 4; index++) {
23
+			intBytes[index] = (byte) (intValue >>> (index * 8));
24
+		}
25
+		m_byteBuf.put(intBytes);
26
+	}
27
+	
28
+	public void write(int[] intArr) {
29
+		for (int index = 0; index < intArr.length; index++) {
30
+			writeInt(intArr[index]);
31
+		}
32
+	}
33
+	
34
+	public void write(byte[] byteArr) {
35
+		m_byteBuf.put(byteArr);
36
+	}
37
+	
38
+	public void writeString(String value, int length) {
39
+		byte[] bytes = new byte[length];
40
+		if (value != null && value.trim().length() > 0) {
41
+			try {
42
+				byte[] infoBytes = value.getBytes("UTF-8");
43
+				int len = infoBytes.length < length ? infoBytes.length : length;			
44
+				System.arraycopy(infoBytes, 0, bytes, 0, len);
45
+			} catch (UnsupportedEncodingException e) {
46
+				logger.error("[response stream factory encoding exception.]", e);
47
+			}
48
+		}	
49
+		m_byteBuf.put(bytes);	
50
+	}
51
+	
52
+	public byte[] getBytes() {
53
+		m_byteBuf.flip();
54
+		if (m_byteBuf.limit() == 0) {
55
+			return null;
56
+		}
57
+		
58
+		byte[] bytes = new byte[m_byteBuf.limit()];
59
+		m_byteBuf.get(bytes);
60
+		
61
+		return bytes;
62
+	}
63
+	
64
+}

+ 32 - 6
xxl-job-core/src/main/java/com/xxl/job/core/util/XxlJobNetCommUtil.java 查看文件

@@ -30,14 +30,25 @@ public class XxlJobNetCommUtil {
30 30
     // hex param key
31 31
     public static final String HEX = "hex";
32 32
 
33
+
33 34
     /**
34 35
      * format object to hex-json
35 36
      * @param obj
36 37
      * @return
37 38
      */
38 39
     public static String formatObj2HexJson(Object obj){
40
+    	// obj to json
39 41
         String json = JacksonUtil.writeValueAsString(obj);
40
-        String hex = ByteHexConverter.byte2hex(json.getBytes());
42
+		int len = ByteHexConverter.getByteLen(json);
43
+
44
+		// json to byte[]
45
+		ByteWriteFactory byteWriteFactory = new ByteWriteFactory();
46
+		byteWriteFactory.writeInt(len);
47
+		byteWriteFactory.writeString(json, len);
48
+		byte[] bytes = byteWriteFactory.getBytes();
49
+
50
+		// byte to hex
51
+        String hex = ByteHexConverter.byte2hex(bytes);
41 52
         return hex;
42 53
     }
43 54
 
@@ -48,14 +59,25 @@ public class XxlJobNetCommUtil {
48 59
      * @return
49 60
      */
50 61
     public static <T> T parseHexJson2Obj(String hex, Class<T> clazz){
51
-        String json = new String(ByteHexConverter.hex2Byte(hex));
62
+    	// hex to byte[]
63
+    	byte[] bytes = ByteHexConverter.hex2Byte(hex);
64
+
65
+		// byte[] to json
66
+		ByteReadFactory byteReadFactory = new ByteReadFactory(bytes);
67
+		String json = byteReadFactory.readString(byteReadFactory.readInt());
68
+
69
+		// json to obj
52 70
         T obj = JacksonUtil.readValue(json, clazz);
53 71
         return obj;
54 72
     }
55 73
 
56 74
     public static void main(String[] args) {
57
-		System.out.println(parseHexJson2Obj("7B2274696D657374616D70223A313436393432323136303032362C22616374696F6E223A2252554E222C226A6F6247726F7570223A2264656661756C7473222C226A6F624E616D65223A22323031363037323530393030353730363632222C226578656375746F7248616E646C6572223A2264656D6F4A6F6248616E646C6572222C226578656375746F72506172616D73223A2231303030303030222C22676C7565537769746368223A66616C73652C226C6F6741646472657373223A2231302E35372E3132332E32383A38383838222C226C6F674964223A3138382C226C6F674461746554696D223A302C22737461747573223A2253554343455353222C226D7367223A6E756C6C7D", RequestModel.class));
58
-        System.out.println(parseHexJson2Obj("7B22737461747573223A2253554343455353222C226D7367223A6E756C6C7D", ResponseModel.class));
75
+		RequestModel requestModel = new RequestModel();
76
+		requestModel.setJobGroup("group");
77
+
78
+		String hex = formatObj2HexJson(requestModel);
79
+		System.out.println(hex);
80
+        System.out.println(parseHexJson2Obj(hex, RequestModel.class));
59 81
     }
60 82
 
61 83
 	/**
@@ -94,8 +116,12 @@ public class XxlJobNetCommUtil {
94 116
 				EntityUtils.consume(entity);
95 117
 
96 118
                 // i do not know why
97
-                responseHex = responseHex.replace("\n", "");
98
-                responseHex = responseHex.replace("\r", "");
119
+                //responseHex = responseHex.replace("\n", "");
120
+                //responseHex = responseHex.replace("\r", "");
121
+
122
+				if (responseHex!=null) {
123
+					responseHex = responseHex.trim();
124
+				}
99 125
 
100 126
                 // parse hex-json to ResponseModel
101 127
                 ResponseModel responseModel = XxlJobNetCommUtil.parseHexJson2Obj(responseHex, ResponseModel.class);

+ 1 - 1
xxl-job-executor-example/pom.xml 查看文件

@@ -101,7 +101,7 @@
101 101
 				<artifactId>maven-war-plugin</artifactId>
102 102
 				<version>2.2</version>
103 103
 				<configuration>
104
-					<archiveClasses>true</archiveClasses>
104
+					<archiveClasses>false</archiveClasses>
105 105
 				</configuration>
106 106
 			</plugin>
107 107
 			<plugin>