阻塞直到所有 EC2 实例都处于特定状态?
我编写了一个函数,该函数应该阻塞,直到列表中的所有实例都不处于特定状态为止,所以这就是我所拥有的:
private void waitForInstanceState(List<String> instancesId, InstanceStateName state) {
int numAchievedState = 0;
while (numAchievedState != instancesId.size()) {
numAchievedState = 0;
DescribeInstanceStatusRequest describeRequest = new DescribeInstanceStatusRequest().withInstanceIds(instancesId);
DescribeInstanceStatusResult instanceStatus = ec2.describeInstanceStatus(describeRequest);
for (InstanceStatus status : instanceStatus.getInstanceStatuses()) {
if (status.getInstanceState().getName().equals(state.toString())) {
numAchievedState++;
}
}
try {
Thread.sleep(15000);
} catch (InterruptedException ex) {
Logger.getLogger(AmazonLibrary.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
调用上述函数的代码是这样的:
public void startInstace(List<String> instancesId) {
StartInstancesRequest startRequest = new StartInstancesRequest(instancesId);
ec2.startInstances(startRequest);
waitForInstanceState(instancesId, InstanceStateName.Running);
System.out.println("The instance has been started");
}
问题是有时描述实例请求返回 null对于 InstanceStatus ,我认为永远不会发生,因为亚马逊中的实例总是有一个状态?此外,当在 instanceRun 函数中使用时,它可以工作,但当 InstanceStateName 设置为 Stopped 运行时,它不会。不会引发异常或错误。
I wrote a functions which is supposed to block until all instances in a list are not in a particular state, so here is what I have:
private void waitForInstanceState(List<String> instancesId, InstanceStateName state) {
int numAchievedState = 0;
while (numAchievedState != instancesId.size()) {
numAchievedState = 0;
DescribeInstanceStatusRequest describeRequest = new DescribeInstanceStatusRequest().withInstanceIds(instancesId);
DescribeInstanceStatusResult instanceStatus = ec2.describeInstanceStatus(describeRequest);
for (InstanceStatus status : instanceStatus.getInstanceStatuses()) {
if (status.getInstanceState().getName().equals(state.toString())) {
numAchievedState++;
}
}
try {
Thread.sleep(15000);
} catch (InterruptedException ex) {
Logger.getLogger(AmazonLibrary.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
The code that invokes the above function is like that:
public void startInstace(List<String> instancesId) {
StartInstancesRequest startRequest = new StartInstancesRequest(instancesId);
ec2.startInstances(startRequest);
waitForInstanceState(instancesId, InstanceStateName.Running);
System.out.println("The instance has been started");
}
The thing is sometimes the describe instance request returns null for InstanceStatus which I think should never happen since an instance in amazon always have a state? Also when used in the instanceRun function it works, when run with InstanceStateName set to Stopped it doesn't. No exceptions or errors are thrown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下工作完成了:
The following did the job: