1、main.java
package com.sc.sre; public class Main { public static void main(String[] args) { String endPointName = "xxxxxxx"; /** *Open API 的接入点,设置为目标 Region */ String regionId = "xxxxxxxx"; /** *鉴权使用的 AccessKey,由阿里云官网控制台获取 */ String accessKey = "xxxxxxx"; /** *鉴权使用的 SecretKey,由阿里云官网控制台获取 */ String secretKey = "xxxxxxxxxxxxxxx"; /* *通过 Open API 访问的云产品名称,此处设置为 Ons 即可 */ String productName = "Ons"; /** *对应 endPoint 接入点的接入点域名 */ String domain = "ons.cn-hangzhou.aliyuncs.com"; String onsRegionPub = "xxxxxxx";//查询公网ons String onsRegionHD1 = "xxxxxxx";//查询华东1的ons
GetWasteTopics getWasteTopicsPub = new GetWasteTopics();
GetWasteTopics getWasteTopicsHD1 = new GetWasteTopics();
getWasteTopicsPub.getWasteTopics(regionId,accessKey,secretKey,productName,domain,onsRegionPub); getWasteTopicsHD1.getWasteTopics(regionId,accessKey,secretKey,productName,domain,onsRegionHD1); } }
2、GetWasteTopics.java
package com.sc.sre; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.FormatType; import com.aliyuncs.ons.model.v20170918.*; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.ons.model.v20170918.OnsTopicStatusResponse; import com.aliyuncs.ons.model.v20170918.OnsTopicStatusRequest; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.io.*; import java.io.FileWriter; public class GetWasteTopics { private String regionId; private String accessKey; private String secretKey; private String productName; private String domain; public void getWasteTopics(String regionId,String accessKey,String secretKey,String productName, String domain, String onsRegionId) { try { DefaultProfile.addEndpoint(regionId, regionId, productName, domain); } catch (ClientException e) { e.printStackTrace(); } IClientProfile profile = DefaultProfile.getProfile(regionId, accessKey, secretKey); IAcsClient iAcsClient = new DefaultAcsClient(profile); OnsRegionListRequest regionRequest = new OnsRegionListRequest(); regionRequest.setAcceptFormat(FormatType.JSON); regionRequest.setPreventCache(System.currentTimeMillis()); OnsTopicListRequest topicRequest = new OnsTopicListRequest(); OnsTopicStatusRequest topicStatusRequest = new OnsTopicStatusRequest(); topicStatusRequest.setAcceptFormat(FormatType.JSON); topicRequest.setOnsRegionId(onsRegionId); topicStatusRequest.setOnsRegionId(onsRegionId); topicRequest.setPreventCache(System.currentTimeMillis()); topicStatusRequest.setPreventCache(System.currentTimeMillis()); try { OnsTopicListResponse response = iAcsClient.getAcsResponse(topicRequest); List<OnsTopicListResponse.PublishInfoDo> publishInfoDoList = response.getData(); //System.out.println(publishInfoDoList.size()); int count = 0; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); File file = new File(onsRegionId+df.format(System.currentTimeMillis())); FileWriter fw = null; try { fw = new FileWriter(file); for (OnsTopicListResponse.PublishInfoDo publishInfoDo : publishInfoDoList) { topicStatusRequest.setTopic(publishInfoDo.getTopic()); OnsTopicStatusResponse topicStatusResponse = iAcsClient.getAcsResponse(topicStatusRequest); OnsTopicStatusResponse.Data data = topicStatusResponse.getData(); Long totalCount = data.getTotalCount(); Long lastTimeStamp = data.getLastTimeStamp(); Long temTimeStamp = System.currentTimeMillis() - lastTimeStamp; if (temTimeStamp / (1000 * 3600) >= 72) {// get unused topics in three days count++; fw.write(count+" "+publishInfoDo.getTopic()+"\n"); } } //fw.write("TotalCount:"+count+"\n"); fw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } //Convert Unix timestamp to normal date style public static String stampToDate(long s){ String lastTime = Long.toString(s); String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(lastTime); Date date = new Date(lt); res = simpleDateFormat.format(date); return res; } } 3、用IDE编译或者打成jar包运行,获取公网和华东1的ons 近三天未更新的topics列表。
reference:https://help.aliyun.com/document_detail/29593.html