我有一个使用 Jclouds 创建 Amazon EC2 实例的 Java 类。此代码运行良好,并在独立运行时创建 EC2 实例。
但是当我运行我的应用程序时,它给了我一个错误,没有更改任何参数,也没有更改任何内容。
当我调试时,我意识到一旦我的应用程序创建了一个 Hibernate SessionFactory,用于创建 EC2 实例的 Java 代码就会给出这个错误。
问题可能出在某些会话变量中,也可能与缓存问题有关。
这是我调用 Jclouds 类的代码
private BrokerStorage() throws GridBrokerException{
super();
try{
logger.info("Initialising Broker Storage...");
//configures the hibernate system using the hibernate.cfg.xml file
//found in the classpath.
Configuration cfg = new Configuration();
cfg.configure(Constants.BROKER_HIBERNATE_CONFIG);
//build the session factory
long start = System.currentTimeMillis();
sf = cfg.buildSessionFactory();
AWSEC2ComputeService.main(new String[]{"asdf"});
long end = System.currentTimeMillis();
logger.info("Done: " + (end-start) + " ms.");
cstore = new CredentialStorage();
}catch (Exception e){
e.printStackTrace();
throw new GridBrokerException("Error creating storage instance.", e);
}
}
下面是创建 EC2 实例的代码
public class AWSEC2ComputeService implements CreateComputeService {
private ComputeService compute;
//private final String NAME = "xxx";
//private final File keyPairFile = new File(NAME + ".pem");
private final String provider = "aws-ec2";
private final String identity = "xxxxx";
private final String credential = "xxxx";
private final String locationId = Region.AP_SOUTHEAST_2;
private final String endpoint = "ec2.ap-southeast-2.amazonaws.com";
private final String NAME = "xxxx";
private final String ZONE = Region.AP_SOUTHEAST_2;
//my imageid
private final String imageId = "xxxx";
private final String hardwareId = "xxx";
private final String securityGroupName = "xxx";
private final String groupName = "xxx";
//private final String ZONE = "ap-southeast-2";
private final int VMnums = 1;
//MyownerID
private final String imageOwnerId = "xxxx";
private final String keyPairName = "xxxx";
private float bidPrice = 0.1f;
private Set<NodeMetadata> launchedNodes = new HashSet<NodeMetadata>();
public static void main(String[] args) {
AWSEC2ComputeService jcloudsNova = new AWSEC2ComputeService();
try {
jcloudsNova.createComputeService();
jcloudsNova.createInstances();
jcloudsNova.finalize();
jcloudsNova.close();
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
jcloudsNova.close();
}
}
@Override
public ComputeService getComputeService() {
return compute;
}
@Override
public List<String> createInstances(){
System.out.println("entering create instances");
Template template = null;
try {
// Create a template for the VM
template = compute.
templateBuilder().
locationId(locationId).
imageId(imageId).osFamily(OsFamily.UBUNTU).hypervisorMatches("paravirtual").
hardwareId(hardwareId).build();
/*template = compute.
templateBuilder().
locationId(locationId).
imageId(imageId).
hardwareId(hardwareId).build();*/
// Specify your own security group
TemplateOptions options = template.getOptions();
options.securityGroups(securityGroupName);
options.as(AWSEC2TemplateOptions.class).keyPair(NAME).spotPrice(bidPrice);
if(template != null){
System.out.println("template: " + template.toString());
}
Method method = options.getClass().getMethod("keyPair", String.class);
method.invoke(options, keyPairName);
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
}
final List<String> launchedNodesAddresses = new ArrayList<>();
try {
// Launch the instances...
Set<? extends NodeMetadata> launchedNodesMetadata = compute.createNodesInGroup(groupName, VMnums, template);
// Collect the addresses ...
for (NodeMetadata nodeMetadata : launchedNodesMetadata) {
System.out.println("Node id: " + nodeMetadata.getId() + " HostName " + nodeMetadata.getHostname() + " Pub Addrs "+nodeMetadata.getUri());
Set<String> privateAddresses = nodeMetadata.getPrivateAddresses();
for(String address : privateAddresses){
launchedNodesAddresses.add(address);
}
}
launchedNodes.addAll(launchedNodesMetadata);
} catch (RunNodesException e) {
throw new IllegalStateException("Nodes could not be created.", e);
}
return launchedNodesAddresses;
}
public Location getLocation(){
Location loca = null;
System.out.println(">Ec2: List Locations.");
ComputeService client = compute;
Set<? extends Location> listLocations = client.listAssignableLocations();
List<Location> locationList=new ArrayList<Location>();
for ( Location loc : listLocations) {
System.out.println("Locationsadsfs : " + loc.toString());
if(loc.getId().equalsIgnoreCase(locationId)){
System.out.println("Location : " + loc.toString());
loca = loc;
}
}
System.out.println(">EC2: Location List obtained successfully.");
return loca;
}
@Override
public ComputeService createComputeService() {
Properties imageOwnerIdFilter = new Properties();
imageOwnerIdFilter.setProperty(
"jclouds.ec2.ami-query", "owner-id=" +
imageOwnerId +
";state=available;image-type=machine;virtualization-type=paravirtual;");
compute = ContextBuilder.
newBuilder(provider).
//endpoint(endpoint).
credentials(identity, credential).
overrides(imageOwnerIdFilter).
buildView(ComputeServiceContext.class).getComputeService();
return compute;
}
@Override
public void destroyAllInstance() {
Iterator<NodeMetadata> iterator = launchedNodes.iterator();
while(iterator.hasNext()){
NodeMetadata nodeMetadata = iterator.next();
destroyInstance(nodeMetadata.getId());
}
}
public void destroyInstance(String nodeId){
compute.destroyNode(nodeId);
}
public void suspendInstance(String nodeId){
compute.suspendNode(nodeId);
}
public void rebootInstance(String nodeId){
compute.rebootNode(nodeId);
}
@Override
public void close() {
try {
Closeables.close(compute.getContext(), true);
} catch (IOException e) {
// TODO Auto-generated catch block
//must write logger statement
e.printStackTrace();
}
}
}
我得到的错误如下
java.lang.UnsupportedOperationException: Method not implemented.
at java.net.URLStreamHandler.openConnection(URLStreamHandler.java:97)
at java.net.URL.openConnection(URL.java:1020)
at org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.initConnection(JavaUrlHttpCommandExecutorService.java:208)
at org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.convert(JavaUrlHttpCommandExecutorService.java:153)
at org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.convert(JavaUrlHttpCommandExecutorService.java:72)
at org.jclouds.http.internal.BaseHttpCommandExecutorService.invoke(BaseHttpCommandExecutorService.java:95)
at org.jclouds.rest.internal.InvokeHttpMethod.invoke(InvokeHttpMethod.java:90)
at org.jclouds.rest.internal.InvokeHttpMethod.apply(InvokeHttpMethod.java:73)
at org.jclouds.rest.internal.InvokeHttpMethod.apply(InvokeHttpMethod.java:44)
at org.jclouds.reflect.FunctionalReflection$FunctionalInvocationHandler.handleInvocation(FunctionalReflection.java:117)
at com.google.common.reflect.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:87)
at com.sun.proxy.$Proxy54.describeRegions(Unknown Source)
at org.jclouds.ec2.suppliers.DescribeRegionsForRegionURIs.get(DescribeRegionsForRegionURIs.java:47)
at org.jclouds.ec2.suppliers.DescribeRegionsForRegionURIs.get(DescribeRegionsForRegionURIs.java:34)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier$SetAndThrowAuthorizationExceptionSupplierBackedLoader.load(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:73)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier$SetAndThrowAuthorizationExceptionSupplierBackedLoader.load(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:57)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.get(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:119)
at org.jclouds.location.suppliers.derived.RegionIdsFromRegionIdToURIKeySet.get(RegionIdsFromRegionIdToURIKeySet.java:49)
at org.jclouds.location.suppliers.derived.RegionIdsFromRegionIdToURIKeySet.get(RegionIdsFromRegionIdToURIKeySet.java:36)
at com.google.common.base.Suppliers$SupplierComposition.get(Suppliers.java:67)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier$SetAndThrowAuthorizationExceptionSupplierBackedLoader.load(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:73)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier$SetAndThrowAuthorizationExceptionSupplierBackedLoader.load(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:57)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.get(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:119)
at org.jclouds.location.suppliers.all.RegionToProviderOrJustProvider.get(RegionToProviderOrJustProvider.java:56)
at org.jclouds.location.suppliers.all.ZoneToRegionToProviderOrJustProvider.get(ZoneToRegionToProviderOrJustProvider.java:71)
at org.jclouds.location.suppliers.all.ZoneToRegionToProviderOrJustProvider.get(ZoneToRegionToProviderOrJustProvider.java:46)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier$SetAndThrowAuthorizationExceptionSupplierBackedLoader.load(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:73)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier$SetAndThrowAuthorizationExceptionSupplierBackedLoader.load(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:57)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
at org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.get(MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier.java:119)
at org.jclouds.compute.domain.internal.TemplateBuilderImpl.locationId(TemplateBuilderImpl.java:618)
at cloudResourceBroker.AWSEC2ComputeService.createInstances(AWSEC2ComputeService.java:100)
at cloudResourceBroker.AWSEC2ComputeService.main(AWSEC2ComputeService.java:68)
at org.gridbus.broker.common.BrokerStorage.<init>(BrokerStorage.java:67)
at org.gridbus.broker.common.BrokerStorage.initialise(BrokerStorage.java:86)
at org.gridbus.broker.common.Broker.initialise(Broker.java:122)
at org.gridbus.broker.common.Broker.<init>(Broker.java:86)
at org.gridbus.workflow.maria.BrokerInstantiation.InstantiateBroker(BrokerInstantiation.java:55)
at org.gridbus.workflow.maria.BrokerInstantiation.getBrokerInstance(BrokerInstantiation.java:74)
at org.gridbus.workflow.maria.BrokerInstantiation.runApplication(BrokerInstantiation.java:176)
at org.gridbus.workflow.maria.BrokerInstantiation.main(BrokerInstantiation.java:195)