/** * 新加列表 * * @author [email protected] * @param nqdmList * @since 1.0 */ public synchronized void addList(List<NoticeQueueDM> nqdmList) { if (null != nqdmList && 0 != nqdmList.size()) { for (NoticeQueueDM dm : nqdmList) { this.countMap.put(dm.getId(), 0); dm.setTime(new Date()); dm.setInitTime(new Date()); // this.nqdmList.add(dm); this.nqdmMap.put(dm.getId(), dm); } } }
@Scheduled(cron = "0 0/5 * * * ?") public void monitor() { log.info("发送监控测试请求"); NoticeQueueDM mdm = new NoticeQueueDM(); Date now = new Date(); MONITOR_COUNT = MONITOR_COUNT + 1; mdm.setUrl( MONITOR_URL + "?c=" + MONITOR_COUNT + "&tm=" + now.getTime() + "&ts=" + sdf.format(now)); mdm.setId(MONITOR_ID); addList(Arrays.asList(new NoticeQueueDM[] {mdm})); }
/** * 发送 * * @author [email protected] * @param nqdm * @return true:对方收到并成功处理,false:对方未收到或未处理 * @since 1.0 */ private boolean send(NoticeQueueDM nqdm) { log.debug("发送:" + nqdm.getUrl()); CloseableHttpResponse res = null; try { HttpPost post = new HttpPost(nqdm.getUrl().trim()); List<NameValuePair> nvpList = new ArrayList<NameValuePair>(); Map<String, String> dataMap = ObjectTools.string2Map(nqdm.getData(), ",", "="); if (null == nqdm.getAppKey() || "".equals(nqdm.getAppKey())) { } else { dataMap.put("appKey", nqdm.getAppKey()); String sign = SignUtil.sign(dataMap, null); dataMap.put("paySign", sign); dataMap.remove("appKey"); } for (Object key : dataMap.keySet()) { log.debug("发送参数:" + key.toString() + ":" + dataMap.get(key).toString()); nvpList.add(new BasicNameValuePair(key.toString(), dataMap.get(key).toString())); } post.setEntity(new UrlEncodedFormEntity(nvpList, "utf-8")); res = httpclient.execute(post); log.info(res.getStatusLine()); HttpEntity entity = res.getEntity(); if (null != entity) { String str = streamToString(entity.getContent()); str = str.toLowerCase(); return str.contains("success"); } } catch (Exception e) { log.debug(e.getMessage(), e); } finally { if (res != null) { try { res.close(); } catch (IOException e) { log.debug(e.getMessage(), e); } } } return false; }
/** * 8次定时发送发送 * * @author 朱伟亮 */ @Scheduled(cron = "0/1 * * * * ?") public void sending8() { List<NoticeQueueDM> returnList = null; Set<String> removeId = null; try { if (nqdmMap.isEmpty() && countMap.isEmpty()) { } else { log.info("当前队列数:" + nqdmMap.size() + " " + countMap.size()); } now = new Date().getTime(); returnList = new ArrayList<>(); removeId = new HashSet<>(); for (NoticeQueueDM nqdm : nqdmMap.values()) { if (null == nqdm || null == nqdm.getId()) { continue; } Integer value = countMap.get(nqdm.getId()); int count = 0; if (null == value) { countMap.put(nqdm.getId(), new Integer(0)); } else { count = value.intValue(); } if (msIntervals.length <= count || count < 0) { nqdm.setStatus(NoticeStatus.STOP.getValue()); removeId.add(nqdm.getId()); } long send = nqdm.getInitTime().getTime() + msIntervals[count].longValue(); if (now <= send) { continue; } else { boolean isSuccess = send(nqdm); count = count + 1; log.debug("发送处理:" + nqdm.getUrl() + " 发送次数:" + count); countMap.put(nqdm.getId(), count); if (isSuccess) { nqdm.setStatus(NoticeStatus.RECEIVED.getValue()); removeId.add(nqdm.getId()); } else { if (count >= msIntervals.length) { nqdm.setStatus(NoticeStatus.STOP.getValue()); removeId.add(nqdm.getId()); } } nqdm.setTime(new Date()); if (!MONITOR_ID.equals(nqdm.getId())) { returnList.add(nqdm); } } } if (0 < returnList.size()) { noticeBO.sent(returnList); } for (String id : removeId) { nqdmMap.remove(id); countMap.remove(id); } } catch (Exception e) { nqdmMap = Collections.synchronizedMap(new HashMap<String, NoticeQueueDM>()); countMap = Collections.synchronizedMap(new HashMap<String, Integer>()); } }