作者 王勇

sdk,初步完成

1 package com.sunyo.wlpt.message.bus.sdk; 1 package com.sunyo.wlpt.message.bus.sdk;
2 2
3 -import com.sunyo.wlpt.message.bus.sdk.request.MessageRequest;  
4 import com.sunyo.wlpt.message.bus.sdk.response.BusResult; 3 import com.sunyo.wlpt.message.bus.sdk.response.BusResult;
5 4
6 -import java.io.IOException;  
7 -  
8 5
9 /** 6 /**
10 * @author 子诚 7 * @author 子诚
  8 + * Description:
  9 + * 时间:2020/9/7 11:00
11 */ 10 */
12 public class App { 11 public class App {
13 - public static void main(String[] args) throws IOException 12 + public static void main(String[] args)
14 { 13 {
15 - MessageRequest request = new MessageRequest();  
16 - BusResult resultOne = request.getMessageOne("zicheng", "S_zicheng",  
17 - "V_zicheng", "V_zicheng_zicheng_R");  
18 - BusResult resultMore = request.getMessageMore("zicheng", "S_zicheng",  
19 - "V_zicheng", "V_zicheng_zicheng_R", 5);  
20 -  
21 - System.out.println(resultOne);  
22 - System.out.println(resultMore); 14 + MyTestMethods myTestMethods = new MyTestMethods();
  15 +
  16 +// BusResult busResult = myTestMethods.product_old();
  17 +
  18 +// BusResult busResult2 = myTestMethods.product_new();
  19 +
  20 +// BusResult busResult3 = myTestMethods.consumerOne();
  21 +// System.out.println(busResult3);
  22 +
  23 + BusResult busResult4 = myTestMethods.consumerMore();
  24 + System.out.println(busResult4);
  25 +
23 } 26 }
24 } 27 }
  1 +package com.sunyo.wlpt.message.bus.sdk;
  2 +
  3 +import com.sunyo.wlpt.message.bus.sdk.request.MessageRequest;
  4 +import com.sunyo.wlpt.message.bus.sdk.response.BusResult;
  5 +
  6 +import java.io.IOException;
  7 +
  8 +/**
  9 + * @author 子诚
  10 + * Description:
  11 + * 时间:2020/9/7 14:36
  12 + */
  13 +public class MyTestMethods {
  14 + private static final String url_product_new = "http://192.168.1.53:12343/message-bus-send/bus/rabbit/product_new";
  15 + private static final String url_product_old = "http://192.168.1.53:12343/message-bus-send/bus/rabbit/product_old";
  16 + private static final String url_consumer_one = "http://192.168.1.53:12343/message-bus-receive/bus/rabbit/consumer_one";
  17 + private static final String url_consumer_more = "http://192.168.1.53:12343/message-bus-receive/bus/rabbit/consumer_more2";
  18 +
  19 + private static final String token =
  20 + "Bearer eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ6aWNoZW5nIiwiZXhwIjoxNTk5NDk3MzU1fQ.YI69vs8pV99v7hnt6xGX5c4wvMPBj6tgsjiI4JJbW2Irj-sUW1stskQ25-G5cDRI-qoZFQTlvRvGVFR97arFD9gwAIB8Gfz4Sog-QC1wLDS3xStvygsCDabQmaF8JaE_zWbtJ7Izs7Jd45VOiI4CS_uZY5dsgODB5IqW1hwj-JY";
  21 +
  22 + /**
  23 + * 报文消息发送
  24 + *
  25 + * @return
  26 + */
  27 + public BusResult product_old()
  28 + {
  29 + long start = System.currentTimeMillis();
  30 + String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <MSG> <META> <SNDR>zicheng</SNDR> \t\t<RCVR></RCVR> \t\t<DDTM>20200902164413</DDTM> \t\t<TYPE>E_zicheng</TYPE> \t\t<STYP>R_zicheng</STYP> \t\t<SEQN>本性难移</SEQN> \t\t<TOKN>江山易老</TOKN> \t\t<VSHT>V_zicheng</VSHT> \t\t<SERV>S_zicheng</SERV> \t</META> \t<BODY>中元节,即是鬼节</BODY> </MSG>";
  31 +
  32 + MessageRequest messageRequest = new MessageRequest();
  33 + BusResult busResult = messageRequest.sendMessage(url_product_old, token, content);
  34 + long end = System.currentTimeMillis();
  35 + System.out.println("发送一条报文消息,耗时:" + (end - start) + "毫秒");
  36 + return busResult;
  37 + }
  38 +
  39 + public BusResult product_new()
  40 + {
  41 + long start = System.currentTimeMillis();
  42 + MessageRequest messageRequest = new MessageRequest();
  43 + BusResult busResult = messageRequest.sendMessage(url_product_new, "S_zicheng", "V_zicheng", "E_zicheng", "R_zicheng", "江山易老,本性难移", "zicheng"
  44 + , "20200907164413", token, "序列号");
  45 + long end = System.currentTimeMillis();
  46 + System.out.println("发送一条参数消息,耗时:" + (end - start) + "毫秒");
  47 + return busResult;
  48 + }
  49 +
  50 + public BusResult consumerOne()
  51 + {
  52 + long start = System.currentTimeMillis();
  53 + MessageRequest messageRequest = new MessageRequest();
  54 + BusResult busResult = messageRequest.getMessageOne(url_consumer_one, token, "zicheng", "S_zicheng", "V_zicheng", "V_zicheng_zicheng_R");
  55 + long end = System.currentTimeMillis();
  56 + System.out.println("接收一条消息,耗时:" + (end - start) + "毫秒");
  57 + return busResult;
  58 + }
  59 +
  60 + public BusResult consumerMore()
  61 + {
  62 + int num = 100;
  63 + long start = System.currentTimeMillis();
  64 + MessageRequest messageRequest = new MessageRequest();
  65 + BusResult busResult = messageRequest.getMessageMore(url_consumer_more, token, "zicheng", "S_zicheng",
  66 + "V_zicheng", "V_zicheng_zicheng_R",
  67 + num);
  68 + long end = System.currentTimeMillis();
  69 + System.out.println("接收" + num + "条消息,耗时:" + (end - start) + "毫秒");
  70 + return busResult;
  71 + }
  72 +}
  1 +package com.sunyo.wlpt.message.bus.sdk;
  2 +
  3 +import com.sunyo.wlpt.message.bus.sdk.request.MessageRequest;
  4 +import com.sunyo.wlpt.message.bus.sdk.response.BusResult;
  5 +import com.sunyo.wlpt.message.bus.sdk.utils.GsonUtil;
  6 +import sun.security.krb5.internal.Ticket;
  7 +
  8 +import java.io.IOException;
  9 +import java.text.SimpleDateFormat;
  10 +import java.util.concurrent.*;
  11 +import java.util.concurrent.locks.Lock;
  12 +import java.util.concurrent.locks.ReentrantLock;
  13 +
  14 +/**
  15 + * @author 子诚
  16 + * Description:
  17 + * 时间:2020/9/7 10:34
  18 + */
  19 +public class Test2 {
  20 + public static void main(String[] args) throws IOException
  21 + {
  22 + // 1031 15:45:58 15:48:57
  23 + Test2 t2 = new Test2();
  24 + t2.moreThread();
  25 + }
  26 +
  27 + void moreThread()
  28 + {
  29 + Tickets thread = new Tickets();
  30 +
  31 +
  32 + ExecutorService executorService = new ThreadPoolExecutor(
  33 + 32, 64,
  34 + 0L,
  35 + TimeUnit.MILLISECONDS,
  36 + new LinkedBlockingQueue<>(10),
  37 + Executors.defaultThreadFactory(),
  38 + new ThreadPoolExecutor.AbortPolicy());
  39 + for (int i = 0; i < 32; i++) {
  40 + executorService.submit(thread);
  41 + }
  42 + executorService.shutdown();
  43 + }
  44 +}
  45 +
  46 +class Tickets implements Runnable {
  47 + private int ticket = 1000;
  48 +
  49 + @Override
  50 + public void run()
  51 + {
  52 + while (true) {
  53 + try {
  54 + if (ticket <= 0) {
  55 + break;
  56 + }
  57 + MyTestMethods myTestMethods = new MyTestMethods();
  58 + // 发送一条报文消息
  59 + BusResult busResult = myTestMethods.product_old();
  60 + System.out.println(Thread.currentThread().getName() + "发送了第" + ticket + "条消息");
  61 + ticket--;
  62 + } finally {
  63 +
  64 + }
  65 + }
  66 + }
  67 +
  68 +}
1 -package com.sunyo.wlpt.message.bus.sdk.request;  
2 -  
3 -import com.sunyo.wlpt.message.bus.sdk.response.ResultJson;  
4 -import com.sunyo.wlpt.message.bus.sdk.utils.GsonUtil;  
5 -import com.sunyo.wlpt.message.bus.sdk.utils.OkHttpUtils;  
6 -import okhttp3.MediaType;  
7 -  
8 -  
9 -import java.io.FileInputStream;  
10 -import java.io.FileNotFoundException;  
11 -import java.io.IOException;  
12 -import java.util.HashMap;  
13 -import java.util.Properties;  
14 -  
15 -/**  
16 - * @author 子诚  
17 - * Description:  
18 - * 时间:2020/9/3 15:18  
19 - */  
20 -public class BusMessage {  
21 -  
22 - /**  
23 - * 指定队列,接收一条消息  
24 - *  
25 - * @param RUSR 接收者  
26 - * @param SERV 服务器名称  
27 - * @param VSHT 虚拟主机名称  
28 - * @param RCVR 队列名称  
29 - * @return  
30 - * @throws IOException  
31 - */  
32 - public static ResultJson consumerOne(String RUSR, String SERV, String VSHT, String RCVR) throws IOException  
33 - {  
34 - HashMap<String, Object> params = new HashMap<>();  
35 - params.put("RUSR", RUSR);  
36 - params.put("SERV", SERV);  
37 - params.put("VSHT", VSHT);  
38 - params.put("RCVR", RCVR);  
39 -  
40 - OkHttpUtils okHttp = new OkHttpUtils();  
41 - String url_consumer_one = getUrl("URL_CONSUMER_ONE");  
42 - String json = okHttp.doGet(url_consumer_one, params);  
43 - ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);  
44 - return result;  
45 - }  
46 -  
47 - /**  
48 - * 指定队列,接收多条消息  
49 - *  
50 - * @param RUSR 接收者  
51 - * @param SERV 服务器名称  
52 - * @param VSHT 虚拟主机名称  
53 - * @param RCVR 队列名称  
54 - * @param CUNT 接收消息的数量  
55 - * @return  
56 - * @throws IOException  
57 - */  
58 - public static ResultJson consumerMore(String RUSR, String SERV, String VSHT, String RCVR, Integer CUNT) throws IOException  
59 - {  
60 - HashMap<String, Object> params = new HashMap<>();  
61 - params.put("RUSR", RUSR);  
62 - params.put("SERV", SERV);  
63 - params.put("VSHT", VSHT);  
64 - params.put("RCVR", RCVR);  
65 - params.put("CUNT", CUNT);  
66 -  
67 - OkHttpUtils okHttp = new OkHttpUtils();  
68 - String url_consumer_more = getUrl("URL_CONSUMER_MORE");  
69 - String json = okHttp.doGet(url_consumer_more, params);  
70 - ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);  
71 - return result;  
72 - }  
73 -  
74 -  
75 - /**  
76 - * 根据url名称,动态获取url  
77 - *  
78 - * @param urlName url名称  
79 - * @return url信息  
80 - * @throws IOException  
81 - */  
82 - public static String getUrl(String urlName) throws IOException  
83 - {  
84 - Properties properties = new Properties();  
85 - FileInputStream fis = new FileInputStream("config\\url.properties");  
86 - properties.load(fis);  
87 - fis.close();  
88 - String url = properties.getProperty(urlName);  
89 - return url;  
90 - }  
91 -}  
@@ -4,6 +4,7 @@ import com.sunyo.wlpt.message.bus.sdk.response.BusResult; @@ -4,6 +4,7 @@ import com.sunyo.wlpt.message.bus.sdk.response.BusResult;
4 import com.sunyo.wlpt.message.bus.sdk.response.ResultJson; 4 import com.sunyo.wlpt.message.bus.sdk.response.ResultJson;
5 5
6 import java.io.IOException; 6 import java.io.IOException;
  7 +import java.text.SimpleDateFormat;
7 import java.util.List; 8 import java.util.List;
8 9
9 /** 10 /**
@@ -12,8 +13,106 @@ import java.util.List; @@ -12,8 +13,106 @@ import java.util.List;
12 * 时间:2020/9/3 18:04 13 * 时间:2020/9/3 18:04
13 */ 14 */
14 public class MessageRequest { 15 public class MessageRequest {
  16 + private static final String SUCCESS_SEND_CODE = "10200";
  17 + private static final String SUCCESS_RECEIVE_CODE = "20200";
15 18
16 - private static final String SUCCESS_ONE_CODE = "20200"; 19 + /**
  20 + * 发送消息,xml报文形式
  21 + *
  22 + * @param xmlMessage xml报文
  23 + * @return
  24 + * @throws IOException
  25 + */
  26 + public BusResult sendMessage(String url, String token, String xmlMessage)
  27 + {
  28 + MessageService messageService = new MessageService();
  29 + ResultJson resultJson = messageService.productOld(url, token, xmlMessage);
  30 +
  31 + if (resultJson == null) {
  32 + BusResult result = new BusResult(false, "10400", "请检查url、token以及以及报文格式");
  33 + return result;
  34 + }
  35 + String code = resultJson.getCode();
  36 + String msg = resultJson.getMsg();
  37 + if (!SUCCESS_SEND_CODE.equals(resultJson.getCode())) {
  38 + String resultMessage = "错误代码:" + code + ";错误信息:" + msg;
  39 + BusResult errorResult = new BusResult(false, code, resultMessage);
  40 + return errorResult;
  41 + }
  42 + BusResult result = new BusResult(true, code, msg);
  43 + return result;
  44 + }
  45 +
  46 + final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  47 +
  48 + /**
  49 + * 发送消息,根据参数
  50 + *
  51 + * @param SERV 服务器名称
  52 + * @param VSHT 虚拟主机名称
  53 + * @param TYPE 大类型(交换机名称)
  54 + * @param STYP 小类型(路由键名称)
  55 + * @param MSG 发送消息内容
  56 + * @param SNDR 发送者名称
  57 + * @param DDTM yyyyMMddHHmmss格式的发送时间
  58 + * @param TOKN token令牌
  59 + * @param SEQN 序列号
  60 + * @param RCVR 指定接收队列(非必须)
  61 + * @return
  62 + * @throws IOException
  63 + */
  64 + public BusResult sendMessage(String url, String SERV, String VSHT, String TYPE, String STYP, String MSG,
  65 + String SNDR, String DDTM, String TOKN, String SEQN, String RCVR)
  66 + {
  67 + ResultJson resultJson = MessageService.productNew(url, SERV, VSHT, TYPE, STYP, MSG, SNDR, DDTM, TOKN, SEQN, RCVR);
  68 + if (resultJson == null) {
  69 + BusResult result = new BusResult(false, "10400", "请检查url、token以及以及参数内容与位置");
  70 + return result;
  71 + }
  72 + String code = resultJson.getCode();
  73 + String msg = resultJson.getMsg();
  74 + if (!SUCCESS_SEND_CODE.equals(resultJson.getCode())) {
  75 + String resultMessage = "错误代码:" + code + ";错误信息:" + msg;
  76 + BusResult errorResult = new BusResult(false, code, resultMessage);
  77 + return errorResult;
  78 + }
  79 + BusResult result = new BusResult(true, code, msg);
  80 + return result;
  81 + }
  82 +
  83 + /**
  84 + * 发送消息,根据参数
  85 + *
  86 + * @param SERV 服务器名称
  87 + * @param VSHT 虚拟主机名称
  88 + * @param TYPE 大类型(交换机名称)
  89 + * @param STYP 小类型(路由键名称)
  90 + * @param MSG 发送消息内容
  91 + * @param SNDR 发送者名称
  92 + * @param DDTM yyyyMMddHHmmss格式的发送时间
  93 + * @param TOKN token令牌
  94 + * @param SEQN 序列号
  95 + * @return
  96 + * @throws IOException
  97 + */
  98 + public BusResult sendMessage(String url, String SERV, String VSHT, String TYPE, String STYP,
  99 + String MSG, String SNDR, String DDTM, String TOKN, String SEQN)
  100 + {
  101 + ResultJson resultJson = MessageService.productNew(url, SERV, VSHT, TYPE, STYP, MSG, SNDR, DDTM, TOKN, SEQN);
  102 + if (resultJson == null) {
  103 + BusResult result = new BusResult(false, "10400", "请检查url、token以及以及参数内容与位置");
  104 + return result;
  105 + }
  106 + String code = resultJson.getCode();
  107 + String msg = resultJson.getMsg();
  108 + if (!SUCCESS_SEND_CODE.equals(resultJson.getCode())) {
  109 + String resultMessage = "错误代码:" + code + ";错误信息:" + msg;
  110 + BusResult errorResult = new BusResult(false, code, resultMessage);
  111 + return errorResult;
  112 + }
  113 + BusResult result = new BusResult(true, code, msg);
  114 + return result;
  115 + }
17 116
18 /** 117 /**
19 * 接收消息,一条 118 * 接收消息,一条
@@ -24,15 +123,16 @@ public class MessageRequest { @@ -24,15 +123,16 @@ public class MessageRequest {
24 * @param RCVR 接收队列名称 123 * @param RCVR 接收队列名称
25 * @return 124 * @return
26 */ 125 */
27 - public BusResult getMessageOne(String RUSR, String SERV, String VSHT, String RCVR) throws IOException 126 + public BusResult getMessageOne(String url, String token, String RUSR, String SERV, String VSHT, String RCVR)
28 { 127 {
29 - ResultJson resultJson = BusMessage.consumerOne(RUSR, SERV, VSHT, RCVR); 128 + ResultJson resultJson = MessageService.consumerOne(url, token, RUSR, SERV, VSHT, RCVR);
30 if (resultJson == null) { 129 if (resultJson == null) {
31 - 130 + BusResult result = new BusResult(false, "20400", "请检查url、token以及以及参数内容与位置");
  131 + return result;
32 } 132 }
33 String code = resultJson.getCode(); 133 String code = resultJson.getCode();
34 String msg = resultJson.getMsg(); 134 String msg = resultJson.getMsg();
35 - if (!SUCCESS_ONE_CODE.equals(resultJson.getCode())) { 135 + if (!SUCCESS_RECEIVE_CODE.equals(resultJson.getCode())) {
36 String resultMessage = "错误代码:" + code + ";错误信息:" + msg; 136 String resultMessage = "错误代码:" + code + ";错误信息:" + msg;
37 BusResult errorResult = new BusResult(false, code, resultMessage); 137 BusResult errorResult = new BusResult(false, code, resultMessage);
38 return errorResult; 138 return errorResult;
@@ -42,16 +142,27 @@ public class MessageRequest { @@ -42,16 +142,27 @@ public class MessageRequest {
42 return result; 142 return result;
43 } 143 }
44 144
45 -  
46 - public BusResult getMessageMore(String RUSR, String SERV, String VSHT, String RCVR, Integer CUNT) throws IOException 145 + /**
  146 + * 接收消息,多条
  147 + *
  148 + * @param RUSR 接收人名称
  149 + * @param SERV 服务器名称
  150 + * @param VSHT 虚拟主机名称
  151 + * @param RCVR 接收队列名称
  152 + * @param CUNT 消息的数量
  153 + * @return
  154 + * @throws IOException
  155 + */
  156 + public BusResult getMessageMore(String url, String token, String RUSR, String SERV, String VSHT, String RCVR, Integer CUNT)
47 { 157 {
48 - ResultJson resultJson = BusMessage.consumerMore(RUSR, SERV, VSHT, RCVR, CUNT); 158 + ResultJson resultJson = MessageService.consumerMore(url, token, RUSR, SERV, VSHT, RCVR, CUNT);
49 if (resultJson == null) { 159 if (resultJson == null) {
50 - 160 + BusResult result = new BusResult(false, "20400", "请检查url、token以及以及参数内容与位置");
  161 + return result;
51 } 162 }
52 String code = resultJson.getCode(); 163 String code = resultJson.getCode();
53 String msg = resultJson.getMsg(); 164 String msg = resultJson.getMsg();
54 - if (!SUCCESS_ONE_CODE.equals(resultJson.getCode())) { 165 + if (!SUCCESS_RECEIVE_CODE.equals(resultJson.getCode())) {
55 String resultMessage = "错误代码:" + code + ";错误信息:" + msg; 166 String resultMessage = "错误代码:" + code + ";错误信息:" + msg;
56 BusResult errorResult = new BusResult(false, code, resultMessage); 167 BusResult errorResult = new BusResult(false, code, resultMessage);
57 return errorResult; 168 return errorResult;
@@ -60,4 +171,6 @@ public class MessageRequest { @@ -60,4 +171,6 @@ public class MessageRequest {
60 BusResult result = new BusResult(true, code, msg, dataList); 171 BusResult result = new BusResult(true, code, msg, dataList);
61 return result; 172 return result;
62 } 173 }
  174 +
  175 +
63 } 176 }
  1 +package com.sunyo.wlpt.message.bus.sdk.request;
  2 +
  3 +import com.sunyo.wlpt.message.bus.sdk.response.ResultJson;
  4 +import com.sunyo.wlpt.message.bus.sdk.utils.GsonUtil;
  5 +import com.sunyo.wlpt.message.bus.sdk.utils.OkHttpUtils;
  6 +import org.springframework.util.StringUtils;
  7 +
  8 +import java.io.IOException;
  9 +import java.util.HashMap;
  10 +
  11 +/**
  12 + * @author 子诚
  13 + * Description:
  14 + * 时间:2020/9/3 15:18
  15 + */
  16 +public class MessageService {
  17 +
  18 + /**
  19 + * 发送消息,根据参数
  20 + *
  21 + * @param url 地址
  22 + * @param SERV 服务器名称
  23 + * @param VSHT 虚拟主机名称
  24 + * @param TYPE 大类型(交换机名称)
  25 + * @param STYP 小类型(路由键名称)
  26 + * @param MSG 发送消息内容
  27 + * @param SNDR 发送者名称
  28 + * @param DDTM yyyyMMddHHmmss格式的发送时间
  29 + * @param TOKN token令牌
  30 + * @param SEQN 序列号
  31 + * @param RCVR 指定接收队列(非必须)
  32 + * @return
  33 + * @throws IOException
  34 + */
  35 + public static ResultJson productNew(String url, String SERV, String VSHT, String TYPE, String STYP,
  36 + String MSG, String SNDR, String DDTM, String TOKN, String SEQN, String RCVR)
  37 + {
  38 + HashMap<String, String> params = new HashMap<>();
  39 + params.put("SERV", SERV);
  40 + params.put("VSHT", VSHT);
  41 + params.put("TYPE", TYPE);
  42 + params.put("STYP", STYP);
  43 + params.put("MSG", MSG);
  44 + params.put("SNDR", SNDR);
  45 + params.put("DDTM", DDTM);
  46 + params.put("TOKN", TOKN);
  47 + params.put("SEQN", SEQN);
  48 + params.put("RCVR", RCVR);
  49 +
  50 + OkHttpUtils okHttp = new OkHttpUtils();
  51 + String json = okHttp.doPostForm(url, TOKN, params);
  52 + if (StringUtils.isEmpty(json)) {
  53 + return new ResultJson<>("10400", "请检查url、token以及以及参数内容与位置");
  54 + }
  55 + ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);
  56 + return result;
  57 + }
  58 +
  59 + /**
  60 + * 发送消息,根据参数
  61 + *
  62 + * @param url 地址
  63 + * @param SERV 服务器名称
  64 + * @param VSHT 虚拟主机名称
  65 + * @param TYPE 大类型(交换机名称)
  66 + * @param STYP 小类型(路由键名称)
  67 + * @param MSG 发送消息内容
  68 + * @param SNDR 发送者名称
  69 + * @param DDTM yyyyMMddHHmmss格式的发送时间
  70 + * @param TOKN token令牌
  71 + * @param SEQN 序列号
  72 + * @return
  73 + * @throws IOException
  74 + */
  75 + public static ResultJson productNew(String url, String SERV, String VSHT, String TYPE, String STYP,
  76 + String MSG, String SNDR, String DDTM, String TOKN, String SEQN)
  77 + {
  78 + HashMap<String, String> params = new HashMap<>();
  79 + params.put("SERV", SERV);
  80 + params.put("VSHT", VSHT);
  81 + params.put("TYPE", TYPE);
  82 + params.put("STYP", STYP);
  83 + params.put("MSG", MSG);
  84 + params.put("SNDR", SNDR);
  85 + params.put("DDTM", DDTM);
  86 + params.put("TOKN", TOKN);
  87 + params.put("SEQN", SEQN);
  88 +
  89 + OkHttpUtils okHttp = new OkHttpUtils();
  90 + String json = okHttp.doPostForm(url, TOKN, params);
  91 + if (StringUtils.isEmpty(json)) {
  92 + return new ResultJson<>("10400", "请检查url、token以及以及参数内容与位置");
  93 + }
  94 + ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);
  95 + return result;
  96 + }
  97 +
  98 + /**
  99 + * 发送消息,xml报文形式
  100 + *
  101 + * @param xmlMessage xml报文
  102 + * @return
  103 + * @throws IOException
  104 + */
  105 + public ResultJson productOld(String url, String token, String xmlMessage)
  106 + {
  107 + HashMap<String, String> params = new HashMap<>();
  108 + params.put("xmlMessage", xmlMessage);
  109 +
  110 + OkHttpUtils okHttp = new OkHttpUtils();
  111 + String json = okHttp.doPostForm(url, token, params);
  112 + if (StringUtils.isEmpty(json)) {
  113 + return new ResultJson<>("10400", "请检查url、token以及报文格式");
  114 + }
  115 + ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);
  116 + return result;
  117 + }
  118 +
  119 + /**
  120 + * 指定队列,接收一条消息
  121 + *
  122 + * @param RUSR 接收者
  123 + * @param SERV 服务器名称
  124 + * @param VSHT 虚拟主机名称
  125 + * @param RCVR 队列名称
  126 + * @return
  127 + * @throws IOException
  128 + */
  129 + public static ResultJson consumerOne(String url, String token, String RUSR, String SERV, String VSHT, String RCVR)
  130 + {
  131 + HashMap<String, Object> params = new HashMap<>();
  132 + params.put("RUSR", RUSR);
  133 + params.put("SERV", SERV);
  134 + params.put("VSHT", VSHT);
  135 + params.put("RCVR", RCVR);
  136 +
  137 + OkHttpUtils okHttp = new OkHttpUtils();
  138 + String json = okHttp.doGet(url, token, params);
  139 + if (StringUtils.isEmpty(json)) {
  140 + return new ResultJson<>("20400", "请检查url、token以及以及参数内容与位置");
  141 + }
  142 + ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);
  143 + return result;
  144 + }
  145 +
  146 + /**
  147 + * 指定队列,接收多条消息
  148 + *
  149 + * @param RUSR 接收者
  150 + * @param SERV 服务器名称
  151 + * @param VSHT 虚拟主机名称
  152 + * @param RCVR 队列名称
  153 + * @param CUNT 接收消息的数量
  154 + * @return
  155 + * @throws IOException
  156 + */
  157 + public static ResultJson consumerMore(String url, String token, String RUSR, String SERV, String VSHT, String RCVR, Integer CUNT)
  158 + {
  159 + HashMap<String, Object> params = new HashMap<>();
  160 + params.put("RUSR", RUSR);
  161 + params.put("SERV", SERV);
  162 + params.put("VSHT", VSHT);
  163 + params.put("RCVR", RCVR);
  164 + params.put("CUNT", CUNT);
  165 +
  166 + OkHttpUtils okHttp = new OkHttpUtils();
  167 + String json = okHttp.doGet(url, token, params);
  168 + if (StringUtils.isEmpty(json)) {
  169 + return new ResultJson<>("20400", "请检查url、token以及以及参数内容与位置");
  170 + }
  171 + ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);
  172 + return result;
  173 + }
  174 +}
@@ -59,6 +59,11 @@ public class BusResult { @@ -59,6 +59,11 @@ public class BusResult {
59 this.dataList = dataList; 59 this.dataList = dataList;
60 } 60 }
61 61
  62 + public BusResult()
  63 + {
  64 +
  65 + }
  66 +
62 public Boolean getOk() 67 public Boolean getOk()
63 { 68 {
64 return isOk; 69 return isOk;
1 package com.sunyo.wlpt.message.bus.sdk.utils; 1 package com.sunyo.wlpt.message.bus.sdk.utils;
2 2
  3 +import lombok.extern.slf4j.Slf4j;
3 import okhttp3.*; 4 import okhttp3.*;
4 import org.apache.commons.lang3.StringUtils; 5 import org.apache.commons.lang3.StringUtils;
5 -import org.apache.log4j.Logger;  
6 6
7 7
8 import java.net.URLEncoder; 8 import java.net.URLEncoder;
@@ -15,8 +15,9 @@ import java.util.concurrent.TimeUnit; @@ -15,8 +15,9 @@ import java.util.concurrent.TimeUnit;
15 * Description: 15 * Description:
16 * 时间:2020/9/3 15:22 16 * 时间:2020/9/3 15:22
17 */ 17 */
  18 +@Slf4j
18 public class OkHttpUtils { 19 public class OkHttpUtils {
19 - private static Logger log = Logger.getLogger(OkHttpUtils.class); 20 +
20 21
21 private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); 22 private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
22 private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"); 23 private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
@@ -28,11 +29,11 @@ public class OkHttpUtils { @@ -28,11 +29,11 @@ public class OkHttpUtils {
28 { 29 {
29 okHttpClient = new OkHttpClient.Builder() 30 okHttpClient = new OkHttpClient.Builder()
30 //10秒连接超时 31 //10秒连接超时
31 - .connectTimeout(10, TimeUnit.SECONDS) 32 + .connectTimeout(60, TimeUnit.SECONDS)
32 //10m秒写入超时 33 //10m秒写入超时
33 - .writeTimeout(10, TimeUnit.SECONDS) 34 + .writeTimeout(600, TimeUnit.SECONDS)
34 //10秒读取超时 35 //10秒读取超时
35 - .readTimeout(10, TimeUnit.SECONDS) 36 + .readTimeout(600, TimeUnit.SECONDS)
36 .build(); 37 .build();
37 } 38 }
38 39
@@ -57,12 +58,12 @@ public class OkHttpUtils { @@ -57,12 +58,12 @@ public class OkHttpUtils {
57 return commonRequest(request); 58 return commonRequest(request);
58 } 59 }
59 60
60 - public String doGet(String url, HashMap<String, Object> params) 61 + public String doGet(String url, String token, HashMap<String, Object> params)
61 { 62 {
62 if (isBlankUrl(url)) { 63 if (isBlankUrl(url)) {
63 return null; 64 return null;
64 } 65 }
65 - Request request = getRequestForGet(url, params); 66 + Request request = getRequestForGet(url, token, params);
66 return commonRequest(request); 67 return commonRequest(request);
67 } 68 }
68 69
@@ -75,21 +76,21 @@ public class OkHttpUtils { @@ -75,21 +76,21 @@ public class OkHttpUtils {
75 return commonRequest(request); 76 return commonRequest(request);
76 } 77 }
77 78
78 - public String doPostForm(String url, Map<String, String> params) 79 + public String doPostForm(String url, String token, Map<String, String> params)
79 { 80 {
80 if (isBlankUrl(url)) { 81 if (isBlankUrl(url)) {
81 return null; 82 return null;
82 } 83 }
83 - Request request = getRequestForPostForm(url, params); 84 + Request request = getRequestForPostForm(url, token, params);
84 return commonRequest(request); 85 return commonRequest(request);
85 } 86 }
86 87
87 private Boolean isBlankUrl(String url) 88 private Boolean isBlankUrl(String url)
88 { 89 {
89 if (StringUtils.isBlank(url)) { 90 if (StringUtils.isBlank(url)) {
90 - log.info("url is not blank");  
91 return true; 91 return true;
92 } else { 92 } else {
  93 + log.info("url is blank-->" + url);
93 return false; 94 return false;
94 } 95 }
95 } 96 }
@@ -102,12 +103,12 @@ public class OkHttpUtils { @@ -102,12 +103,12 @@ public class OkHttpUtils {
102 Response response = call.execute(); 103 Response response = call.execute();
103 if (response.isSuccessful()) { 104 if (response.isSuccessful()) {
104 re = response.body().string(); 105 re = response.body().string();
105 -// log.info("request url:{};response:{}", request.url().toString(), re);  
106 } else { 106 } else {
107 -// log.info("request failure url:{};message:{}", request.url().toString(), response.message()); 107 + log.error("request failure url:{};message:{}", request.url().toString(), response.message());
108 } 108 }
109 } catch (Exception e) { 109 } catch (Exception e) {
110 log.error("request execute failure", e); 110 log.error("request execute failure", e);
  111 + System.out.println("失败");
111 } 112 }
112 return re; 113 return re;
113 } 114 }
@@ -123,7 +124,7 @@ public class OkHttpUtils { @@ -123,7 +124,7 @@ public class OkHttpUtils {
123 } 124 }
124 125
125 126
126 - private Request getRequestForPostForm(String url, Map<String, String> params) 127 + private Request getRequestForPostForm(String url, String token, Map<String, String> params)
127 { 128 {
128 if (params == null) { 129 if (params == null) {
129 params = new HashMap<>(); 130 params = new HashMap<>();
@@ -137,15 +138,17 @@ public class OkHttpUtils { @@ -137,15 +138,17 @@ public class OkHttpUtils {
137 RequestBody requestBody = builder.build(); 138 RequestBody requestBody = builder.build();
138 Request request = new Request.Builder() 139 Request request = new Request.Builder()
139 .url(url) 140 .url(url)
  141 + .addHeader("Authorization", token)
140 .post(requestBody) 142 .post(requestBody)
141 .build(); 143 .build();
142 return request; 144 return request;
143 } 145 }
144 146
145 - private Request getRequestForGet(String url, HashMap<String, Object> params) 147 + private Request getRequestForGet(String url, String token, HashMap<String, Object> params)
146 { 148 {
147 Request request = new Request.Builder() 149 Request request = new Request.Builder()
148 .url(getUrlStringForGet(url, params)) 150 .url(getUrlStringForGet(url, params))
  151 + .addHeader("Authorization", token)
149 .build(); 152 .build();
150 return request; 153 return request;
151 } 154 }
@@ -166,7 +169,6 @@ public class OkHttpUtils { @@ -166,7 +169,6 @@ public class OkHttpUtils {
166 if (params != null && params.size() > 0) { 169 if (params != null && params.size() > 0) {
167 for (Map.Entry<String, Object> entry : params.entrySet()) { 170 for (Map.Entry<String, Object> entry : params.entrySet()) {
168 try { 171 try {
169 -  
170 urlBuilder.append("&").append(entry.getKey()).append("=").append( 172 urlBuilder.append("&").append(entry.getKey()).append("=").append(
171 "CUNT".equals(entry.getKey()) 173 "CUNT".equals(entry.getKey())
172 ? entry.getValue() 174 ? entry.getValue()