天天看點

簡單通路第三方接口

public void setAlarmMap(){
		//從緩存中取資料
		List<Map<String ,Object>> AlarmLogList = ConstantUtil.COLLECTION_ALAEMLOG_LIST;
		
		List<Map<String ,Object>> list=new ArrayList<Map<String, Object>>();
		String AlarmLogResult="";
		try{
			URL url3 = new URL(ContextUtil.getMsgByKey("THXC_API_URL")+"/wce/api/am/messages");
			HttpURLConnection httpURLConnection3 = (HttpURLConnection) url3.openConnection();
			httpURLConnection3.setRequestMethod("POST");// 送出模式
			httpURLConnection3.setDoOutput(true);
			httpURLConnection3.setDoInput(true);
			httpURLConnection3.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			httpURLConnection3.setRequestProperty("Accept-Charset", "charset=UTF-8");
			httpURLConnection3.setRequestProperty("Authorization", "Basic"+ContextUtil.getMsgByKey("Authorization_Basic"));
			// 擷取URLConnection對象對應的輸出流
			OutputStreamWriter out = new OutputStreamWriter(httpURLConnection3.getOutputStream(), "UTF-8");
			// 發送請求參數
			String json=JSONUtil.toJson("");
			out.write(json);
			out.flush();
			// 開始擷取資料
			BufferedReader in3 = new BufferedReader(new InputStreamReader(httpURLConnection3.getInputStream(), "UTF-8"));
			String line3;
			while ((line3 = in3.readLine()) != null) {
				AlarmLogResult += line3;
			}
			Map<String, Object>	AlarmLogMap = JSONUtil.toMap(AlarmLogResult);
			list = (List<Map<String ,Object>>) AlarmLogMap.get("alarms");
			//存儲告警Id的集合
			
			List<String> list3=new ArrayList<String>();
			//将查詢的資料發送 AlarmUtil.send
			for(int i=0;i<list.size();i++){
				Map<String, Object> map1=(Map<String, Object>) list.get(i);
				list3.add(NotNulls(map1.get("warningId")));
				String resId = NotNulls(map1.get("warningId"));
				String item = NotNulls(map1.get("objectId"));
				String resName = NotNulls(map1.get("name"));
				String type = NotNulls(map1.get("type"));
				String content =NotNulls(map1.get("body"));
				Date alarmDate = DateNotNull(map1.get("createTime"));
				int level = 0;
				if(map1.get("priority")!=null){
					level = Integer.parseInt(map1.get("priority").toString());
				}
				Integer state = null;
				if(map1.get("readed")!=null){
					state = Integer.valueOf(map1.get("readed").toString());
				}
				AlarmUtil.send(null,null,level,type,item,content,resId,resName,alarmDate,null,null,state); 
			}
			//循環從緩存中擷取的告警集合
			for(int i=0;i<AlarmLogList.size();i++){
				Map<String, Object> map2=(Map<String, Object>) AlarmLogList.get(i);
				//如果不存在  說明已經處理--将已經處理的告警恢複
				if(!list3.contains(NotNulls(map2.get("warningId")))){
					String resId = NotNulls(map2.get("warningId"));
					String item = NotNulls(map2.get("objectId"));
					String type = NotNulls(map2.get("type"));
					Date restoreDate = new Date();
					//恢複告警
					AlarmUtil.restore(null,null,type,item,resId,restoreDate);
				}
			}
		} catch (Exception e) {
	            System.out.println("擷取告警資訊失敗!" + e);
	            e.printStackTrace();
	        }
		ConstantUtil.COLLECTION_ALAEMLOG_LIST=list;
	}
           

繼續閱讀