86a345219a5c2681cee9e00eb570589a7a8f4796.svn-base 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package com.chinacreator.process.job;
  2. import java.net.URLEncoder;
  3. import java.sql.SQLException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Set;
  14. import java.util.concurrent.CountDownLatch;
  15. import java.util.concurrent.ExecutorService;
  16. import java.util.concurrent.LinkedBlockingQueue;
  17. import java.util.concurrent.ThreadPoolExecutor;
  18. import java.util.concurrent.TimeUnit;
  19. import org.apache.log4j.Logger;
  20. import org.quartz.DisallowConcurrentExecution;
  21. import org.quartz.PersistJobDataAfterExecution;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.util.StringUtils;
  24. import com.alibaba.fastjson.JSON;
  25. import com.alibaba.fastjson.JSONObject;
  26. import com.chinacreator.common.exception.BusinessException;
  27. import com.chinacreator.common.util.DESUtil;
  28. import com.chinacreator.process.bean.KuaishouPushBean;
  29. import com.chinacreator.process.dao.DictionaryDao;
  30. import com.chinacreator.process.dao.KuaishouDao;
  31. import com.chinacreator.process.dao.VipSmsSpidSendDao;
  32. import com.chinacreator.process.util.JsonUtil;
  33. import com.chinacreator.process.util.SHAUtil;
  34. import com.chinacreator.process.util.URLUtil;
  35. import com.chinacreator.process.util.WriteLogUtil;
  36. /**
  37. * 手动领取会员短信发送
  38. * 每10分钟执行一次
  39. * @author xu.zhou
  40. * @date 20220407
  41. */
  42. @PersistJobDataAfterExecution
  43. @DisallowConcurrentExecution
  44. public class VipSmsSpidSendJob {
  45. private static Logger logger = Logger.getLogger("vipSmsSpidSend");
  46. @Autowired
  47. private VipSmsSpidSendDao vipSmsSpidSendDao; // = new VipSmsSpidSendDao();
  48. @Autowired
  49. private DictionaryDao dictionaryDao; // = new DictionaryDao();
  50. public void doProcess() throws Exception {
  51. //System.out.println("VipSmsSpidSendJob定时任务开始"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
  52. WriteLogUtil.writeLong("VipSmsSpidSendJob定时任务开始", logger, "VipSmsSpidSendJob");
  53. int count = 0;
  54. long beginTime = System.currentTimeMillis();
  55. //当前月份
  56. String currmonth = new SimpleDateFormat("yyyyMM").format(new Date());
  57. int rows = 800; //每次取数据条数
  58. //获取字典表的配置
  59. String confrows = dictionaryDao.getValue("vipSmsSpidSendrows");
  60. if (!StringUtils.isEmpty(confrows)) {
  61. try {
  62. rows = Integer.parseInt(confrows);
  63. } catch (Exception e) {
  64. rows = 800;
  65. }
  66. }
  67. //采集表分区标识,分区标识从T_HASH_P01到T_HASH_P50
  68. String partition = "T_HASH_P";
  69. for(int i = 1; i <= 50; i++){
  70. count = 0;
  71. partition = "T_HASH_P";
  72. try {
  73. if(i < 10){
  74. partition = partition + "0" + i;
  75. }else{
  76. partition = partition + i;
  77. }
  78. //按分区标识获取订购数据
  79. List<HashMap> dataList = vipSmsSpidSendDao.getDataByPart(partition,rows, currmonth);
  80. count = (dataList != null ? dataList.size() : 0);
  81. logger.info(partition+",用户数:"+(dataList != null ? dataList.size() : "0"));
  82. if(dataList != null && dataList.size() > 0){
  83. CountDownLatch threadSignal = new CountDownLatch(dataList.size());
  84. ExecutorService executorService = new ThreadPoolExecutor(10, 20, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
  85. //把数据更新为正在处理状态
  86. for(HashMap hm : dataList){
  87. boolean res = vipSmsSpidSendDao.updExecing(hm.get("ID").toString());
  88. }
  89. for(HashMap hm : dataList){
  90. VipSmsSpidSendService continueService = new VipSmsSpidSendService(dataList.size(),threadSignal,hm,currmonth,vipSmsSpidSendDao,dictionaryDao);
  91. executorService.execute(continueService);
  92. }
  93. executorService.shutdown();
  94. try {
  95. executorService.awaitTermination(5L, TimeUnit.MINUTES);
  96. } catch (InterruptedException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. } catch (Exception e) {
  101. logger.info(partition+",执行出现异常,"+e.getMessage());
  102. e.printStackTrace();
  103. }
  104. Thread.sleep(100);
  105. }
  106. WriteLogUtil.writeLong("VipSmsSpidSendJob定时任务完成,耗时:"+(System.currentTimeMillis()-beginTime)/1000+" 秒", logger, "VipSmsSpidSendJob");
  107. }
  108. public static void main(String[] args) {
  109. VipSmsSpidSendJob job = new VipSmsSpidSendJob();
  110. try {
  111. job.doProcess();
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. }
  117. class VipSmsSpidSendService implements Runnable {
  118. private static Logger logger = Logger.getLogger("vipSmsSpidSend");
  119. private int totalSize;
  120. private CountDownLatch threadSignal;
  121. private List<String> spidsList;
  122. private String currmonth;
  123. private VipSmsSpidSendDao vipSmsSpidSendDao;
  124. private HashMap hm;
  125. private DictionaryDao dictionaryDao;
  126. public VipSmsSpidSendService(int _totalSize,CountDownLatch _threadSignal,HashMap _hm,String _currmonth,
  127. VipSmsSpidSendDao _vipSmsSpidSendDao, DictionaryDao _dictionaryDao){
  128. this.totalSize = _totalSize;
  129. this.threadSignal = _threadSignal;
  130. this.hm = _hm;
  131. this.currmonth = _currmonth;
  132. this.vipSmsSpidSendDao = _vipSmsSpidSendDao;
  133. this.dictionaryDao = _dictionaryDao;
  134. }
  135. @Override
  136. /**
  137. * 业务处理
  138. * 1. 查询是否配置
  139. * 2. 判断当前时间是否在指定时间
  140. * 2. 查询当月是否已发送过短信
  141. * 3. 发送短信
  142. * 4. 更新处理结果
  143. */
  144. public void run() {
  145. long startime = System.currentTimeMillis();
  146. Map logMap = new HashMap();
  147. logMap.put("data", hm);
  148. String resultcode = "-1";
  149. String errorinfo = "";
  150. String id = hm.get("ID").toString();
  151. try {
  152. String userid = hm.get("USERID").toString();
  153. String confid = hm.get("CONFID").toString();
  154. //获取所有配置信息
  155. HashMap dataconf = vipSmsSpidSendDao.getVipsmsspidconf(confid);
  156. //System.out.println("配置数据"+dataconf);
  157. if(dataconf == null || dataconf.size() == 0){
  158. throw new BusinessException("9057","无发送配置");
  159. }
  160. String smstxt = (String)dataconf.get("SMSTXT");
  161. if(!StringUtils.isEmpty(smstxt) && smstxt.trim().length()>0){
  162. //替换手机号码
  163. smstxt = smstxt.replaceAll("#USERID#", userid);
  164. }else{
  165. throw new BusinessException("9053","无短信内容配置");
  166. }
  167. //发送时间验证
  168. String dateformat = (String)dataconf.get("DATEFORMAT");
  169. String sendstarttime = (String)dataconf.get("SENDSTARTTIME");
  170. String sendendtime = (String)dataconf.get("SENDENDTIME");
  171. if(!StringUtils.isEmpty(dateformat)){
  172. String currtime = new SimpleDateFormat(dateformat).format(new Date());
  173. logMap.put("currtime", currtime);
  174. //有配置短信发送开始时间,但当前时间小于开始时间
  175. if(!StringUtils.isEmpty(sendstarttime) && Long.parseLong(currtime) < Long.parseLong(sendstarttime)){
  176. throw new BusinessException("9055","未到短信发送开始时间");
  177. }
  178. //有配置短信发送结束时间,但当前时间大于结束时间
  179. if(!StringUtils.isEmpty(sendendtime) && Long.parseLong(currtime) > Long.parseLong(sendendtime)){
  180. throw new BusinessException("9054","已过短信发送结束时间");
  181. }
  182. }
  183. if(vipSmsSpidSendDao.hasAlreadySend(userid,confid,currmonth)){
  184. throw new BusinessException("9056","重复发送短信");
  185. }
  186. HashMap<String, String> params = new HashMap<String, String>();
  187. params.put("USERID", userid);
  188. params.put("CONFID", confid);
  189. params.put("SENDMONTH", currmonth);
  190. params.put("COLLECTID", id);
  191. //添加发送记录,有唯一约束,添加成功才发短信
  192. vipSmsSpidSendDao.addSendLog(params);
  193. //发送短信
  194. String result = send(userid,smstxt);
  195. errorinfo = result;
  196. if(!StringUtils.isEmpty(result)){
  197. JSONObject obj = JSON.parseObject(result);
  198. resultcode = obj.getString("resultcode");
  199. }else{
  200. resultcode = "5000";
  201. errorinfo = "调发送短信接口无返回信息";
  202. }
  203. } catch (Exception e) {
  204. if (e instanceof BusinessException) {
  205. errorinfo = ((BusinessException) e).getMessage();
  206. resultcode = ((BusinessException) e).getCode();
  207. }else{
  208. e.printStackTrace();
  209. if (e.getMessage() != null && e.getMessage().contains("ORA-00001")) {
  210. resultcode = "6002";
  211. errorinfo += "当月已发送";
  212. }else{
  213. resultcode = "8000";
  214. errorinfo += "处理数据出现异常,"+e.getMessage();
  215. }
  216. }
  217. }finally {
  218. threadSignal.countDown();
  219. boolean updres = false;
  220. try{
  221. if(!StringUtils.isEmpty(errorinfo) && errorinfo.length()>300){
  222. errorinfo = errorinfo.substring(0,300);
  223. }
  224. if("-1".equals(resultcode)){
  225. resultcode = "8001";
  226. errorinfo = "处理异常";
  227. }
  228. //更新处理结果
  229. updres = vipSmsSpidSendDao.updExecRes(id,resultcode,errorinfo);
  230. }catch(Exception e){
  231. e.printStackTrace();
  232. errorinfo += "更新数据出现异常,"+e.getMessage();
  233. resultcode = "8001";
  234. }
  235. //写日志
  236. logMap.put("jobname", "VipSmsSpidSendJob");
  237. logMap.put("resultcode", resultcode);
  238. logMap.put("errorinfo", errorinfo);
  239. logMap.put("updres", updres);
  240. logger.info(JsonUtil.objectToJson(logMap));
  241. }
  242. }
  243. //发送短信
  244. public String send(String userid,String content) throws Exception {
  245. String url = dictionaryDao.getValue("contractpushurl");
  246. // String url = "http://111.206.133.54/smsbusi/sms/send";
  247. String smsid = "10655117";
  248. String pwd = "wo6bslq2";
  249. userid = DESUtil.encode(userid,pwd);
  250. String timestamp = System.currentTimeMillis()/1000+"";
  251. String sign = SHAUtil.shaEncode(smsid+userid+timestamp+content+pwd).toLowerCase();
  252. userid = URLEncoder.encode(userid,"utf-8");
  253. content = URLEncoder.encode(content,"utf-8");
  254. url = url+"?smsid="+smsid+"&userid="+userid+"&timestamp="+timestamp+"&sign="+sign+"&content="+content;
  255. String result = URLUtil.get(url);
  256. return result;
  257. }
  258. }