逐渐理解open api的意义,试了下阿里云的续费接口,续费一个月

 package com.test;

 /**
* @author
* @date 2018/12/05
*/
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.aliyuncs.AcsRequest;
import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
import com.aliyuncs.ecs.model.v20140526.RenewInstanceRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile; import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; public class AliyunRenewInstancesExample { private String accessKeyId = "your-accessKeyId";
private String accessSecret = "your-accessSecret"; /**
* 实例所属的地域ID
*/
private String regionId = "cn-beijing";
/**
* 指定的需要续费的实例 ID
*/
private String[] instanceIds = new String[] {"i-m************"};
/**
* 预付费续费时长
*/
private Integer period = 1;
/**
* 续费时长单位,取值:Week/Month
*/
private String periodUnit = "Month"; private Map<String, InstanceExpiredModel> instanceExpiredModelMap = new HashMap<String, InstanceExpiredModel>();
private static final ExecutorService RENEW_INSTANCE_POOL = Executors.newFixedThreadPool(500);
private static final Integer CHECK_EXPIRED_TIME_OUT_MILLISECOND = 60 * 1000;
private static final Integer CHECK_EXPIRED_TIME_INTERVAL_MILLISECOND = 2 * 1000;
private static final String INSTANCE_CHARGE_TYPE_PREPAID = "PrePaid";
private static final String DESCRIBE_INSTANCES_FILTER_KEY_EXPIRED_START_TIME = "ExpiredStartTime";
private static final String DESCRIBE_INSTANCES_FILTER_KEY_EXPIRED_END_TIME = "ExpiredEndTime"; public static void main(String[] args) {
/**
* 使用须知:
* 调用 OpenAPI 续费会自动扣费,请谨慎调用
* 您的账号必须支持账号余额支付或信用支付,请确保金额充足
*
* 调用续费API后会对 新的到期时间 和 原始的到期时间 进行比较并分别输出成功和失败的实例
*/
new AliyunRenewInstancesExample().callRenewInstance();
} private void callRenewInstance() {
/**
* 若需查询需要续费的实例,可查看 describeNeedRenewInstances 方法
*/
//List<String> needRenewInstanceIds = describeNeedRenewInstances();
List<String> instanceIdList = Arrays.asList(instanceIds);
List<DescribeInstancesResponse.Instance> instances = describeInstances(instanceIdList);
if (!checkInstanceIds(instances, instanceIdList)) {
return;
}
updateInstanceOriginalExpiredTime(instances);
for (final String instanceId : instanceIdList) {
RENEW_INSTANCE_POOL.execute(new Runnable() {
public void run() {
RenewInstanceRequest request = new RenewInstanceRequest();
request.setRegionId(regionId);
request.setInstanceId(instanceId);
request.setPeriod(period);
request.setPeriodUnit(periodUnit);
callOpenApi(request, instanceExpiredModelMap.get(instanceId));
}
});
}
try {
RENEW_INSTANCE_POOL.shutdown();
RENEW_INSTANCE_POOL.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
checkInstancesExpiredTime(instanceIdList);
}
/**
* 每2秒中检查一次实例的到期时间,超时时间设为1分钟
*
* @param instanceIds 需要检查的实例ID
*/
private void checkInstancesExpiredTime(List<String> instanceIds) {
Long startTime = System.currentTimeMillis();
for (;;) {
boolean allCompleted = true;
Long timeStamp = System.currentTimeMillis();
updateInstanceNewExpiredTime(instanceIds);
for (String instanceId : instanceExpiredModelMap.keySet()) {
InstanceExpiredModel instanceExpiredModel = instanceExpiredModelMap.get(instanceId);
if (instanceExpiredModel.newExpiredTime == null && instanceExpiredModel.errorCode == null) {
allCompleted = false;
}
}
if (allCompleted) {
logRenewResult();
return;
}
if (timeStamp - startTime > CHECK_EXPIRED_TIME_OUT_MILLISECOND) {
logInfo(String.format("Check instance new expiredTime timeout. Because it takes too much time, View the result detail: %s",
JSON.toJSONString(instanceExpiredModelMap, SerializerFeature.PrettyFormat)));
return;
}
sleepSomeTime(CHECK_EXPIRED_TIME_INTERVAL_MILLISECOND);
}
} private boolean checkInstanceIds(List<DescribeInstancesResponse.Instance> instances,
List<String> instanceIds) {
if (instances.size() != instanceIds.size()) {
List<String> responseInstanceIds = new ArrayList<String>();
List<String> invalidInstanceIds = new ArrayList<String>();
for (DescribeInstancesResponse.Instance instance : instances) {
responseInstanceIds.add(instance.getInstanceId());
}
for (String instanceId : instanceIds) {
if (!responseInstanceIds.contains(instanceId)) {
invalidInstanceIds.add(instanceId);
}
}
logInfo(String.format("Fail. CheckInstanceIds failure. Invalid InstanceIds: %s",
JSON.toJSONString(invalidInstanceIds)));
return false;
}
return true;
} /**
* 记录 instance 原始到期时间,以便在调用续费 OpenAPI 后 check 到期时间是否发生变化来判断续费是否成功
*
* @param instances
* @return
*/
private boolean updateInstanceOriginalExpiredTime(List<DescribeInstancesResponse.Instance> instances) {
for (DescribeInstancesResponse.Instance instance : instances) {
InstanceExpiredModel instanceExpiredModel = new InstanceExpiredModel();
instanceExpiredModel.instanceId = instance.getInstanceId();
instanceExpiredModel.originalExpiredTime = instance.getExpiredTime();
instanceExpiredModelMap.put(instance.getInstanceId(), instanceExpiredModel);
}
return true;
} private void updateInstanceNewExpiredTime(List<String> instanceIds) {
List<DescribeInstancesResponse.Instance> instances = describeInstances(instanceIds);
for (DescribeInstancesResponse.Instance instance : instances) {
InstanceExpiredModel instanceExpiredModel = instanceExpiredModelMap.get(instance
.getInstanceId());
if (!instance.getExpiredTime().equals(instanceExpiredModel.originalExpiredTime)) {
instanceExpiredModel.newExpiredTime = instance.getExpiredTime();
}
}
} private List<DescribeInstancesResponse.Instance> describeInstances(List<String> instanceIds) {
int offset = 0;
int pageSize = 100;
int totalCount = instanceIds.size();
List<DescribeInstancesResponse.Instance> instances = new ArrayList<DescribeInstancesResponse.Instance>();
while (offset < totalCount) {
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
describeInstancesRequest.setRegionId(regionId);
describeInstancesRequest.setPageSize(pageSize);
describeInstancesRequest.setInstanceChargeType(INSTANCE_CHARGE_TYPE_PREPAID);
describeInstancesRequest.setInstanceIds(JSON.toJSONString(instanceIds.subList(offset,
Math.min(totalCount, offset + pageSize))));
DescribeInstancesResponse response = callOpenApi(describeInstancesRequest, null);
if (response == null || response.getInstances().size() == 0) {
logInfo(String.format("Fail. Can not find instance. InstanceIds: %s", JSON.toJSONString(instanceIds)));
return instances;
}
instances.addAll(response.getInstances());
offset += response.getPageSize();
}
return instances;
} private Map<String, InstanceExpiredModel> getFailedInstances() {
Map<String, InstanceExpiredModel> failedInstances = new HashMap<String, InstanceExpiredModel>();
for (String instanceId : instanceExpiredModelMap.keySet()) {
InstanceExpiredModel instanceExpiredModel = instanceExpiredModelMap.get(instanceId);
if (instanceExpiredModel.errorCode != null) {
failedInstances.put(instanceId, instanceExpiredModel);
}
}
return failedInstances;
} private Map<String, InstanceExpiredModel> getSuccessInstances() {
Map<String, InstanceExpiredModel> successInstances = new HashMap<String, InstanceExpiredModel>();
for (String instanceId : instanceExpiredModelMap.keySet()) {
InstanceExpiredModel instanceExpiredModel = instanceExpiredModelMap.get(instanceId);
if (instanceExpiredModel.errorCode == null
&& instanceExpiredModel.newExpiredTime != null
&& !instanceExpiredModel.originalExpiredTime
.equals(instanceExpiredModel.newExpiredTime)) {
successInstances.put(instanceId, instanceExpiredModel);
}
}
return successInstances;
} /**
* 调用OpenAPI的方法,这里进行了错误处理
*
* @param request AcsRequest, Open API的请求
* @param <T> AcsResponse 请求所对应返回值
* @return 返回OpenAPI的调用结果. 如果调用失败,则会返回null
*/
private <T extends AcsResponse> T callOpenApi(AcsRequest<T> request, BaseResult errorResult) {
IAcsClient client = initClient();
try {
T response = client.getAcsResponse(request, false, 0);
logInfo(String.format("Success. OpenAPI Action: %s call successfully.",
request.getActionName()));
return response;
} catch (ClientException e) {
buildErrResult(errorResult, e);
}
return null;
} /**
* 需要填充账号的AccessKey ID,以及账号的Access Key Secret
*/
private IAcsClient initClient() {
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
return new DefaultAcsClient(profile);
} private void logRenewResult() {
logInfo("-------- Renew Instances Result --------");
Map<String, InstanceExpiredModel> failedInstances = getFailedInstances();
if (failedInstances.size() > 0) {
logInfo(String.format("Fail. Some instances renew failure, result: %s",
JSON.toJSONString(failedInstances, SerializerFeature.PrettyFormat)));
Map<String, InstanceExpiredModel> successInstances = getSuccessInstances();
if (successInstances.size() > 0) {
logInfo(String.format("Success. Some instances renew success, result: %s",
JSON.toJSONString(successInstances, SerializerFeature.PrettyFormat)));
}
} else {
logInfo(String.format("Success. RenewInstance result: %s", JSON.toJSONString(
instanceExpiredModelMap, SerializerFeature.PrettyFormat)));
}
} private static void sleepSomeTime(int sleepTime) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
} private static void buildErrResult(BaseResult errResult, ClientException e) {
if (errResult != null) {
errResult.errorCode = e.getErrCode();
errResult.requestId = e.getRequestId();
}
if (e instanceof ServerException) {
logInfo(String.format("Fail. Something with your connection with Aliyun go incorrect. ErrorCode: %s",
e.getErrCode()));
} else {
logInfo(String.format("Fail. Business error. ErrorCode: %s, RequestId: %s",
e.getErrCode(), e.getRequestId()));
}
} private static void logInfo(String message) {
System.out.println(message);
} private List<String> describeNeedRenewInstances() {
String instanceExpiredStartTimeUtc = "2018-10-21T16:00Z";
String instanceExpiredEndTimeUtc = "2018-12-01T16:00Z";
List<String> instanceIds = new ArrayList<String>(); int pageSize = 100;
int pageNumber = 1;
int totalCount = 1;
while (instanceIds.size() < totalCount) {
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
describeInstancesRequest.setRegionId(regionId);
describeInstancesRequest.setFilter3Key(DESCRIBE_INSTANCES_FILTER_KEY_EXPIRED_START_TIME);
describeInstancesRequest.setFilter3Value(instanceExpiredStartTimeUtc);
describeInstancesRequest.setFilter4Key(DESCRIBE_INSTANCES_FILTER_KEY_EXPIRED_END_TIME);
describeInstancesRequest.setFilter4Value(instanceExpiredEndTimeUtc);
describeInstancesRequest.setInstanceChargeType(INSTANCE_CHARGE_TYPE_PREPAID);
describeInstancesRequest.setPageSize(pageSize);
describeInstancesRequest.setPageNumber(pageNumber);
DescribeInstancesResponse response = callOpenApi(describeInstancesRequest, null);
if (response == null || response.getTotalCount() == 0) {
logInfo("Fail. DescribeNeedRenewInstances occurred error or response.getTotalCount() == 0");
return instanceIds;
}
for (DescribeInstancesResponse.Instance instance : response.getInstances()) {
instanceIds.add(instance.getInstanceId());
}
totalCount = response.getTotalCount();
pageNumber++;
}
logInfo(String.format("Success. DescribeNeedRenewInstances result: %s", instanceIds.toString()));
return instanceIds;
} class BaseResult {
public String errorCode;
public String requestId;
} class InstanceExpiredModel extends BaseResult {
public String instanceId;
public String originalExpiredTime;
public String newExpiredTime;
}
}

执行结果,用账户余额支付了一月个的续费

Success. OpenAPI Action: DescribeInstances call successfully.
Success. OpenAPI Action: RenewInstance call successfully.
Success. OpenAPI Action: DescribeInstances call successfully.
-------- Renew Instances Result --------
Success. RenewInstance result: {
"i-m**********":{
"instanceId":"i-m************",
"newExpiredTime":"2019-01-05T16:00Z",
"originalExpiredTime":"2018-12-05T16:00Z"
}
}

试了下阿里云的OPEN Api的更多相关文章

  1. 五:用JAVA写一个阿里云VPC Open API调用程序

    用JAVA写一个阿里云VPC Open API调用程序 摘要:用JAVA拼出来Open API的URL 引言 VPC提供了丰富的API接口,让网络工程是可以通过API调用的方式管理网络资源.用程序和软 ...

  2. 阿里云视频直播API签名机制源码

    阿里云视频直播API签名机制源码 本文展示:通过代码实现下阿里视频直播签名处理规则 阿里云视频直播签名机制,官方文档链接:https://help.aliyun.com/document_detail ...

  3. 阿里云学习之API网关

    注:此处仅供api的创建做一个补充参考,API网关的优缺点及创建过程中的参数详情,请参考阿里云开放文档:https://helpcdn.aliyun.com/document_detail/29478 ...

  4. PHP控制阿里云短信API接口实现短信群发功能

    阿里云短信支持先使用后支付的原则,价格为4分半1条. 通过SDK可以与网站功能的绑定,实现响应的短信发送功能 现已统一合并升级为:消息服务. 消息服务 阿里云消息服务(Message Service, ...

  5. 阿里云oss c# api 的使用 的使用

    API 文档下载地址:http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/32085/cn_zh/1515493045734 ...

  6. 基于阿里云 DNS API 实现的 DDNS 工具

    0.简要介绍 0.1 思路说明 AliDDNSNet 是基于 .NET Core 开发的动态 DNS 解析工具,借助于阿里云的 DNS API 来实现域名与动态 IP 的绑定功能.工具核心就是调用了阿 ...

  7. 【阿里云API】 阿里云API调用的若干说明

    阿里云API 为了监控我们使用的一些阿里云产品,需要些一些脚本,定时调用这些脚本来获得相关阿里云产品的信息. ■ 概述 调用阿里云API大约分成两类方法,一个是直接从HTTP协议开始,自己根据阿里云的 ...

  8. C#调用阿里云CDN API刷新缓存

    使用CDN必须要解决CDN缓存的问题,要么在每次更新文件时生成不同的URL,要么在每次更新文件时刷新CDN缓存.我们在一个实际应用场景中用到了后者,所以需要调用阿里云CDN的API进行缓存刷新的操作. ...

  9. 教你配置使用阿里云 Maven 库,体验秒下 jar 包的快感

    鉴于国内的网络环境,从默认 Maven 库下载 jar 包是非常的痛苦. 速度慢就不说了,还经常是下不下来,然后一运行就是各种 ClassNotFoundException,然后你得找到残留文件删掉重 ...

随机推荐

  1. python学习2:turtle的使用蟒蛇绘制的学习以及自己摸索的等边三角形绘制(跟随mooc学习)

    首先先放上蟒蛇的绘制程序 import turtle#引入外部库#def保留字用于 定义函数 def drawSnake(rad,angle,len,neckrad): for i in range( ...

  2. Linux删除软链接

    首先我们先来创建一个文件 #mkdir test_chk #touch test_chk/test.txt #vim test_chk/test.txt (这一步随便在这个test.txt里写点东东即 ...

  3. 网络编程 多线程/socketserver模块/ threading.local

    线程:进程中负责程序执行的执行单元. 多线程:在1个进程中存在多个线程. 进程只是用来把资源集中在一起,而线程才是cpu上的执行单位. 每个进程都会默认有一个控制线程也叫作主线程. 进程之间是竞争关系 ...

  4. Web服务器之Nginx详解(理论部分)

    大纲 一.前言 二.Web服务器提供服务的方式 三.多进程.多线程.异步模式的对比 四.Web 服务请求过程 五.Linux I/O 模型 六.Linux I/O 模型具体说明 七.Linux I/O ...

  5. hdu 1874 畅通工程续 floyed

    裸题 题意:求任意两点之间的最短路径 坑点:测试数据同一条路径有可能出现多次,然后值不一样,注意筛选最小边 #include <iostream> #include <cstdio& ...

  6. java-Calendar类

    1.Calendar类的概述和获取日期的方法 * A:Calendar类的概述 * Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR ...

  7. list实现大整数加法

    #include<iostream> #include<list> #include<string> using namespace std; list<in ...

  8. 《DSP using MATLAB》Problem 7.4

  9. HTML文本元素标签

    <b></b>表示关键字和产品名称如:<b>查看效果</b> 效果:加粗 查看效果 <strong></strong>表示重要的 ...

  10. 664. Strange Printer

    class Solution { public: int dp[100][100]; int dfs(const string &s, int i,int j) { if(i>j)ret ...