use

1、main.java
package com.strain.sre;

public class Main {
    public static void main(String[] args) {
         String endPointName = "xxxxxxx" ;
         /**
          *Open API access point, set as the target Region
          */
         String regionId = "xxxxxxxx" ;
         /**
          *AccessKey used for authentication, obtained from the Alibaba Cloud official website console
          */
         String accessKey = "xxxxxxx" ;
         /**
          *SecretKey used for authentication, obtained from the Alibaba Cloud official website console
          */
         String secretKey = "xxxxxxxxxxxxxxx" ;
         /*
          *The name of the cloud product accessed through the Open API, which is set to Ons here Just
          */
         String productName = "Ons" ;
         /**
          *The access point domain name corresponding to the endPoint access point
         */
        String domain = "ons.cn-hangzhou.aliyuncs.com";

        String onsRegionPub = "xxxxxxx" ; //Query public network ons
         ​​String onsRegionHD1 = "xxxxxxx" ; //Query ons in East China 1
        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.souche.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. Compile with IDE or run it as a jar package to obtain a list of topics that have not been updated in the past three days on the public network and on East China 1.
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324930790&siteId=291194637
use
use