栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Java中以编程方式启动和停止Amazon EC2实例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Java中以编程方式启动和停止Amazon EC2实例

我最近已经在Bamboo
AWS插件中
实现了此功能;它是开源的,代码可在Bitbucket上找到,您可以找到一个完整的示例如何在EC2Task.java中启动/停止/重启实例(a,实际上应该是一个单独的类)。

幸运的是,这一点都不复杂,例如,可以像这样启动一个实例:

private String startInstance(final String instanceId, AmazonEC2 ec2, final BuildLogger buildLogger)        throws AmazonServiceException, AmazonClientException, InterruptedException{    StartInstancesRequest startRequest = new StartInstancesRequest().withInstanceIds(instanceId);    StartInstancesResult startResult = ec2.startInstances(startRequest);    List<InstanceStateChange> stateChangeList = startResult.getStartingInstances();    buildLogger.addBuildLogEntry("Starting instance '" + instanceId + "':");    // Wait for the instance to be started    return waitForTransitionCompletion(stateChangeList, "running", ec2, instanceId, buildLogger); }

BuildLogger是Bamboo特有的,而waitForTransitionCompletion()是特定于实现的帮助程序,用于报告过程/结果。该

AmazonEC2ec2
参数通过AmazonEC2接口将引用传递给AmazonEC2Client对象,该接口定义了所有相关方法(包括许多其他方法),尤其是:

  • StartInstances()
  • StopInstances()
  • RebootInstances()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号