List of usage examples for com.amazonaws.services.ec2.model MonitorInstancesRequest setInstanceIds
public void setInstanceIds(java.util.Collection<String> instanceIds)
The IDs of the instances.
From source file:org.gridgain.grid.util.ec2.GridEc2Helper.java
License:GNU General Public License
/** * @param imgId EC2 image ID./*from w ww .ja va2s . c o m*/ * @param keyPair Key pair name. * @param secGrps Security groups. * @param instType EC2 Instance type. * @param userData Instances user data. * @param count Instance count. * @param enableMon Enable CloudWatch monitoring. * @return List of started instances' IDs. * @throws GridException Thrown in case of any exception. */ private List<String> startInstances(String imgId, String keyPair, Collection<String> secGrps, String instType, String userData, int count, boolean enableMon) throws GridException { RunInstancesRequest req = new RunInstancesRequest(); req.setImageId(imgId); req.setInstanceType(instType); req.setKeyName(keyPair); req.setMaxCount(count); req.setMinCount(count); req.setMonitoring(enableMon); if (!F.isEmpty(secGrps)) req.setSecurityGroups(secGrps); if (userData != null) req.setUserData(new String(Base64.encodeBase64(userData.getBytes()))); RunInstancesResult res; try { res = ec2.runInstances(req); } catch (AmazonClientException ex) { throw new GridException("Failed to perform EC2 run instances request: " + ex.getMessage(), ex); } List<Instance> running = res.getReservation().getInstances(); if (running == null) throw new GridException("Received unexpected EC2 response (instances have not been started)."); List<String> ids = new ArrayList<String>(); for (Instance inst : running) ids.add(inst.getInstanceId()); if (enableMon) { MonitorInstancesRequest mReq = new MonitorInstancesRequest(); mReq.setInstanceIds(ids); try { ec2.monitorInstances(mReq); } catch (AmazonClientException ex) { throw new GridException("Failed to start instances monitoring.", ex); } } return ids; }