9b375d6c4c417397ab2f167ee827edf15bd686f1.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package com.chinacreator.process.job;
  2. import com.alibaba.fastjson.JSON;
  3. import com.chinacreator.process.dao.HuaweiWalletOrderDao;
  4. import com.chinacreator.process.util.HttpInvoke;
  5. import com.chinacreator.process.util.RsaUtil;
  6. import com.chinacreator.process.util.URLUtil;
  7. import net.sf.json.JSONObject;
  8. import org.apache.commons.lang.StringUtils;
  9. import org.apache.log4j.Logger;
  10. import org.quartz.DisallowConcurrentExecution;
  11. import org.quartz.PersistJobDataAfterExecution;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.scheduling.annotation.Scheduled;
  14. import java.net.URLEncoder;
  15. import java.sql.SQLException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.*;
  18. import java.util.concurrent.*;
  19. @PersistJobDataAfterExecution
  20. @DisallowConcurrentExecution
  21. public class HuaweiWalletNotifyJob {
  22. private static Logger logger = Logger.getLogger("HuaweiWalletNotify");
  23. @Autowired
  24. private HuaweiWalletOrderDao huaweiWalletOrderDao;
  25. @Scheduled(cron = "0/10 * * * * *")
  26. public void doProcess() throws Exception{
  27. logger.info("开始处理华为钱包订单回调job");
  28. //获取表中需要订购的数据
  29. List<HashMap> dataList = huaweiWalletOrderDao.queryNotifyList();
  30. logger.info("获取数据条数" + dataList.size());
  31. if (dataList!=null&&dataList.size()>0){
  32. CountDownLatch threadSignal = new CountDownLatch(dataList.size());
  33. ExecutorService executorService = new ThreadPoolExecutor(10, 20, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
  34. for (HashMap<String,String> data : dataList){
  35. huaweiWalletOrderDao.updataQueryNotifyList(data.get("ID"),"2");
  36. }
  37. for (HashMap<String, String> data : dataList) {
  38. HuaweiWalletNotifyService continueService = new HuaweiWalletNotifyService(dataList.size(), threadSignal, data, huaweiWalletOrderDao);
  39. executorService.execute(continueService);
  40. }
  41. executorService.shutdown();
  42. try {
  43. executorService.awaitTermination(5L, TimeUnit.MINUTES);
  44. } catch (InterruptedException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. //每取一轮数据,休眠100毫秒
  49. Thread.sleep(100);
  50. try{
  51. //更新执行状态为2且执行时间超过10分钟的数据为待处理
  52. int res = huaweiWalletOrderDao.updExecTimeout();
  53. if(res > 0){
  54. logger.info("更新状态为2处理中且执行时间超过2分钟的数据为待处理的异常数据条数【"+res+"】");
  55. }
  56. }catch (Exception e){
  57. e.printStackTrace();
  58. logger.info("更新状态为2处理中且执行时间超过2分钟的数据失败");
  59. }
  60. logger.info("结束华为钱包订单回调job");
  61. }
  62. }
  63. /**
  64. * 处理类
  65. * @author xinlong.liu
  66. * @date 20220721
  67. */
  68. class HuaweiWalletNotifyService implements Runnable {
  69. private static Logger logger = Logger.getLogger("HuaweiWalletNotify");
  70. private int totalSize;
  71. private CountDownLatch threadSignal;
  72. private HuaweiWalletOrderDao huaweiWalletOrderDao;
  73. private HashMap<String,String> data; //订购数据
  74. public HuaweiWalletNotifyService(int totalSize,CountDownLatch threadSignal,HashMap<String,String> data,HuaweiWalletOrderDao huaweiWalletOrderDao){
  75. this.totalSize = totalSize;
  76. this.threadSignal = threadSignal;
  77. this.data = data;
  78. this.huaweiWalletOrderDao = huaweiWalletOrderDao;
  79. }
  80. @Override
  81. public void run() {
  82. String isnotify = "8000";
  83. String returnCode = "8000";
  84. String returnDesc = "回调出错";
  85. //组件回调参数
  86. HashMap<String,Object> logmap = new HashMap<String, Object>();
  87. HashMap<String,String> map = new HashMap<String, String>();
  88. logmap.put("data",data);
  89. int notifycount = 0;
  90. try{
  91. notifycount = Integer.parseInt(data.get("NOTIFYCOUNT"));
  92. String spOrderNo = data.get("ID");
  93. String orderNo = data.get("ORDERNO");
  94. String spid = data.get("HWSPID");
  95. String orderTime = data.get("ORDERTIME");
  96. String rechargeTime = data.get("RECHARGETIME");
  97. String status = data.get("STATUS");
  98. String phoneNumber = data.get("USERID");
  99. String productCode = data.get("SPID");
  100. String productType = data.get("PRODUCTYPE");
  101. String faceValue = data.get("FACEVALUE");
  102. String callbackParam1 = data.get("NOTIFYPARAM1");
  103. String callbackParam2 = data.get("NOTIFYPARAM2");
  104. String callbackParam3 = data.get("NOTIFYPARAM3");
  105. String signType = data.get("SIGNTYPE");
  106. String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
  107. String notifyUrl = data.get("NOTIFYURL");
  108. logmap.put("url",notifyUrl);
  109. String signStr = signByRSA(data);
  110. logmap.put("签名原文signStr",signStr);
  111. map.put("spOrderNo",spOrderNo);
  112. map.put("orderNo",orderNo);
  113. map.put("spid",spid);
  114. map.put("orderTime",orderTime);
  115. map.put("rechargeTime",rechargeTime);
  116. map.put("status",status);
  117. map.put("phoneNumber",phoneNumber);
  118. map.put("productCode",productCode);
  119. map.put("productType",productType);
  120. map.put("faceValue",faceValue);
  121. map.put("callbackParam1",callbackParam1);
  122. map.put("callbackParam2",callbackParam2);
  123. map.put("callbackParam3",callbackParam3);
  124. map.put("signType",signType);
  125. map.put("timestamp",timestamp);
  126. // http://testapi.mige.tv/univideo/huaweinotify
  127. String url = huaweiWalletOrderDao.getValue("huaweinotify");
  128. String sign = URLUtil.get(url+"?signStr="+ URLEncoder.encode(signStr));
  129. map.put("sign",sign);
  130. logmap.put("reqDate",map);
  131. Map headMap = new HashMap();
  132. headMap.put("certVersion","0");
  133. headMap.put("requestId",timestamp+ UUID.randomUUID().toString().replaceAll("-", "").substring(0, 7));
  134. logmap.put("headle",headMap);
  135. String res = "";
  136. if (notifyUrl.indexOf("https") != -1) {
  137. res = HttpInvoke.sendhttpsReq("POST", notifyUrl, JSON.toJSONString(map), headMap);
  138. } else {
  139. res = URLUtil.postJson(notifyUrl, JSON.toJSONString(map), 5000);
  140. }
  141. // String res = URLUtil.postJson(notifyUrl, JSON.toJSONString(map),5000);
  142. logmap.put("res",res);
  143. com.alibaba.fastjson.JSONObject obj = JSON.parseObject(res);
  144. if("0".equals(obj.getString("returnCode"))){
  145. isnotify = "0";
  146. returnCode = "0";
  147. returnDesc = "success";
  148. }else {
  149. returnCode = obj.getString("returnCode");
  150. returnDesc = res;
  151. }
  152. }catch (Exception e){
  153. e.printStackTrace();
  154. logger.info("回调出错:"+e);
  155. }finally {
  156. logger.info(JSON.toJSONString(logmap));
  157. try {
  158. huaweiWalletOrderDao.updataQueryNotifyList(data.get("ID"),isnotify,notifycount+1,returnCode,returnDesc);
  159. } catch (SQLException throwables) {
  160. throwables.printStackTrace();
  161. logger.info("修改回调状态失败"+data.get("ID"));
  162. }
  163. }
  164. }
  165. /**
  166. * 生成签名原文
  167. * @param data
  168. * @return
  169. */
  170. public String signByRSA(HashMap<String,String> data) {
  171. String str = "";
  172. String spOrderNo = data.get("ID");
  173. String orderNo = data.get("ORDERNO");
  174. String spid = data.get("HWSPID");
  175. String orderTime = data.get("ORDERTIME");
  176. String rechargeTime = data.get("RECHARGETIME");
  177. String status = data.get("STATUS");
  178. String phoneNumber = data.get("USERID");
  179. String productCode = data.get("SPID");
  180. String productType = data.get("PRODUCTYPE");
  181. String faceValue = data.get("FACEVALUE");
  182. String callbackParam1 = data.get("CALLBACKPARAM1");
  183. String callbackParam2 = data.get("CALLBACKPARAM2");
  184. String callbackParam3 = data.get("CALLBACKPARAM3");
  185. String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
  186. boolean flag = false;
  187. if (!StringUtils.isEmpty(callbackParam1)){
  188. str += "callbackParam1="+callbackParam1;
  189. flag =true;
  190. }
  191. if (!StringUtils.isEmpty(callbackParam2)){
  192. if (flag){
  193. str += "&callbackParam2="+callbackParam2;
  194. }else {
  195. str += "callbackParam2="+callbackParam2;
  196. }
  197. flag =true;
  198. }
  199. if (!StringUtils.isEmpty(callbackParam3)){
  200. if (flag){
  201. str += "&callbackParam3="+callbackParam3;
  202. }else {
  203. str += "callbackParam3="+callbackParam3;
  204. }
  205. flag = true;
  206. }
  207. if (!StringUtils.isEmpty(faceValue)){
  208. if (flag){
  209. str += "&faceValue="+faceValue;
  210. }else {
  211. str += "faceValue="+faceValue;
  212. }
  213. }
  214. if (!StringUtils.isEmpty(orderNo)){
  215. str += "&orderNo="+orderNo;
  216. }
  217. if (!StringUtils.isEmpty(orderTime)){
  218. str += "&orderTime="+orderTime;
  219. }
  220. if (!StringUtils.isEmpty(phoneNumber)){
  221. str += "&phoneNumber="+phoneNumber;
  222. }
  223. if (!StringUtils.isEmpty(productCode)){
  224. str += "&productCode="+productCode;
  225. }
  226. if (!StringUtils.isEmpty(productType)){
  227. str += "&productType="+productType;
  228. }
  229. if (!StringUtils.isEmpty(rechargeTime)){
  230. str += "&rechargeTime="+rechargeTime;
  231. }
  232. if (!StringUtils.isEmpty(spOrderNo)){
  233. str += "&spOrderNo="+spOrderNo;
  234. }
  235. if (!StringUtils.isEmpty(spid)){
  236. str += "&spid="+spid;
  237. }
  238. if (!StringUtils.isEmpty(status)){
  239. str += "&status="+status;
  240. }
  241. if (!StringUtils.isEmpty(timestamp)){
  242. str += "&timestamp="+timestamp;
  243. }
  244. return str;
  245. }
  246. }