7a0d1eb660bb559a742f4e1c61d1c4b28ab0a6c6.svn-base 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.chinacreator.process.job;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.chinacreator.common.util.DESUtil;
  4. import com.chinacreator.process.dao.DictionaryDao;
  5. import com.chinacreator.process.dao.QueryCapOrderDao;
  6. import com.chinacreator.process.exception.BusinessException;
  7. import com.chinacreator.process.util.URLUtil;
  8. import com.chinacreator.process.util.fakeid.MD5;
  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 java.sql.SQLException;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.concurrent.*;
  18. @PersistJobDataAfterExecution
  19. @DisallowConcurrentExecution
  20. public class QueryCapOrderJob {
  21. private Logger log = Logger.getLogger("queryCapOrder");
  22. @Autowired
  23. private DictionaryDao dictionaryDao;
  24. @Autowired
  25. private QueryCapOrderDao queryCapOrderDao;
  26. private String channel="kcqueryorder";
  27. private String pwd="315b5de2";
  28. public void doProcess() throws Exception {
  29. List<HashMap> capList = queryCapOrderDao.findCapList();
  30. if (capList!=null && capList.size()>0){
  31. log.info("需查询cap的条数=="+capList.size());
  32. CountDownLatch threadSignal = new CountDownLatch(capList.size());//一个线程等待其他线程各自执行完毕后再执行。
  33. ExecutorService executorService = new ThreadPoolExecutor(10, 15, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
  34. for (HashMap hashMap : capList) {
  35. QueryCapOrderService queryCapOrderService = new QueryCapOrderService(hashMap, capList.size(), threadSignal);
  36. executorService.execute(queryCapOrderService);
  37. }
  38. executorService.shutdown();
  39. try {
  40. executorService.awaitTermination(5L, TimeUnit.MINUTES);
  41. } catch (InterruptedException e) {
  42. e.printStackTrace();
  43. }
  44. log.info("处理完成");
  45. }
  46. }
  47. class QueryCapOrderService implements Runnable{
  48. private Logger log = Logger.getLogger("queryCapOrder");
  49. private Map params;
  50. private int totalSize;
  51. private CountDownLatch threadSignal;
  52. public QueryCapOrderService(Map params, int totalSize, CountDownLatch threadSignal) {
  53. this.params = params;
  54. this.totalSize = totalSize;
  55. this.threadSignal = threadSignal;
  56. }
  57. @Override
  58. public void run() {
  59. String resultCode="2";
  60. String resp="";
  61. try {
  62. String url = dictionaryDao.getValue("queryCapOrderUrl");
  63. String userid = String.valueOf(params.get("USERID"));
  64. String cpid = String.valueOf(params.get("CPID"));
  65. String spid = String.valueOf(params.get("SPID"));
  66. String timestamp = System.currentTimeMillis()/1000+"";
  67. userid= DESUtil.encode(userid,pwd);
  68. String signature= MD5.MD5Encode(cpid + spid + userid + channel + timestamp + pwd);
  69. HashMap<String, String> map = new HashMap<String, String>();
  70. map.put("userid",userid);
  71. map.put("cpid",cpid);
  72. map.put("spid",spid);
  73. map.put("channel",channel);
  74. map.put("timestamp",timestamp);
  75. map.put("signature",signature);
  76. resp = URLUtil.postJson(url, JSONObject.toJSONString(map));
  77. Map respMap= JSONObject.parseObject(resp, Map.class);
  78. if (respMap!=null &&resp.length()>0){
  79. resultCode = String.valueOf(respMap.get("resultCode"));
  80. if (!("0".equals(resultCode)||"-1".equals(resultCode)||"1".equals(resultCode))){
  81. throw new BusinessException(resultCode,String.valueOf(respMap.get("resultInfo")));
  82. }
  83. }else {
  84. throw new BusinessException("9001","查询cap返回结果为null");
  85. }
  86. queryCapOrderDao.updateStatus(resultCode, String.valueOf(params.get("ID")));
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. try {
  90. queryCapOrderDao.updateStatus("3", String.valueOf(params.get("ID")));
  91. } catch (SQLException ex) {
  92. ex.printStackTrace();
  93. }
  94. } finally {
  95. threadSignal.countDown();
  96. log.info("需处理的参数:"+params+"查询cap返回结果: "+resp);
  97. }
  98. }
  99. }
  100. }