AWS - EC2 instance取得时类型异常

按照官方样例取得EC2 instance实例时,出现了异常

代码

while(!done) {
            DescribeInstancesResponse response = ec2.describeInstances(request);

            for(Reservation reservation : response.reservations()) { 
                for(Instance instance : reservation.instances()) { //异常
                    System.out.printf(
                        "Found reservation with id %s, " +
                        "AMI %s, " +
                        "type %s, " +
                        "state %s " +
                        "and monitoring state %s",
                        instance.instanceId(),
                        instance.imageId(),
                        instance.instanceType(),
                        instance.state().name(),
                        instance.monitoring().state());
                    System.out.println("");
                }
            }

            if(response.nextToken() == null) {
                done = true;
            }
        }

异常

在上面的for循环,提示出现异常

Type mismatch: cannot convert from element type Instance to CloseableThreadContext.Instance

解决

参照包出现错误,Instance 默认参照的包是

org.apache.logging.log4j.CloseableThreadContext.Instance

log4j内提示需要使用可关闭安全的线程
aws-java-sdk中已经提供了自动关闭的安全线程,修改参照

software.amazon.awssdk.services.ec2.model.Instance;

解决。

猜你喜欢

转载自blog.csdn.net/oblily/article/details/87907277