全部产品
云市场

快速入门

更新时间:2019-06-19 20:16:35

快速入门

本节介绍如何快速使用OOS Java SDK完成常见操作,如创建模板、启动执行、查询执行等。

创建模板

以下代码用于创建模板:

  1. import com.aliyuncs.profile.DefaultProfile;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.exceptions.ServerException;
  6. import com.aliyuncs.oos.model.v20190601.*;
  7. public class Demo {
  8. public static void main(String[] args) {
  9. DefaultProfile profile = DefaultProfile.getProfile(
  10. "<your-region-id>", // 地域ID
  11. "<your-access-key-id>", // RAM账号的AccessKey ID
  12. "<your-access-key-secret>"); // RAM账号AccessKey Secret
  13. IAcsClient client = new DefaultAcsClient(profile);
  14. // 创建模板
  15. CreateTemplateRequest request = new CreateTemplateRequest();
  16. request.setTemplateName("MyTemplate");
  17. request.setContent("{\n"
  18. + " \"FormatVersion\": \"OOS-2019-06-01\",\n"
  19. + " \"Description\": \"Descirbe instances of given status\",\n"
  20. + " \"Parameters\": {\n"
  21. + " \"Status\": {\n"
  22. + " \"Type\": \"String\",\n"
  23. + " \"Description\": \"(Required) The status of the Ecs instance.\"\n"
  24. + " }\n"
  25. + " },\n"
  26. + " \"Tasks\": [\n"
  27. + " {\n"
  28. + " \"Properties\": {\n"
  29. + " \"Parameters\": { \"Status\": \"{{ Status }}\" },\n"
  30. + " \"API\": \"DescribeInstances\",\n"
  31. + " \"Service\": \"ECS\"\n"
  32. + " },\n"
  33. + " \"Name\": \"describeInstances\",\n"
  34. + " \"Action\": \"ACS::ExecuteAPI\"\n"
  35. + " }\n"
  36. + " ]\n"
  37. + "}");
  38. // 发起请求并处理应答或异常
  39. CreateTemplateResponse response;
  40. try {
  41. response = client.getAcsResponse(request);
  42. System.out.println(response.getTemplate().getTemplateName());
  43. } catch (ServerException e) {
  44. e.printStackTrace();
  45. } catch (ClientException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }

启动执行

以下代码用于启动执行:

  1. import com.aliyuncs.profile.DefaultProfile;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.exceptions.ServerException;
  6. import com.aliyuncs.oos.model.v20190601.*;
  7. public class Demo {
  8. public static void main(String[] args) {
  9. DefaultProfile profile = DefaultProfile.getProfile(
  10. "<your-region-id>", // 地域ID
  11. "<your-access-key-id>", // RAM账号的AccessKey ID
  12. "<your-access-key-secret>"); // RAM账号AccessKey Secret
  13. IAcsClient client = new DefaultAcsClient(profile);
  14. // 启动执行
  15. StartExecutionRequest request = new StartExecutionRequest();
  16. request.setTemplateName("MyTemplate");
  17. request.setParameters("{\"Status\": \"Running\"}");
  18. // 发起请求并处理应答或异常
  19. StartExecutionResponse response;
  20. try {
  21. response = client.getAcsResponse(request);
  22. System.out.println(response.getExecution().getExecutionId());
  23. } catch (ServerException e) {
  24. e.printStackTrace();
  25. } catch (ClientException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

查询执行

以下代码用于查询执行:

  1. import com.aliyuncs.profile.DefaultProfile;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.exceptions.ServerException;
  6. import com.aliyuncs.oos.model.v20190601.*;
  7. public class Demo {
  8. public static void main(String[] args) {
  9. DefaultProfile profile = DefaultProfile.getProfile(
  10. "<your-region-id>", // 地域ID
  11. "<your-access-key-id>", // RAM账号的AccessKey ID
  12. "<your-access-key-secret>"); // RAM账号AccessKey Secret
  13. IAcsClient client = new DefaultAcsClient(profile);
  14. // 创建API请求并设置参数
  15. ListExecutionsRequest request = new ListExecutionsRequest();
  16. request.setExecutionId("<ExecutionId>");
  17. // 发起请求并处理应答或异常
  18. ListExecutionsResponse response;
  19. try {
  20. response = client.getAcsResponse(request);
  21. for(ListExecutionsResponse.Execution execution: response.getExecutions()){
  22. System.out.println(execution.getExecutionId());
  23. System.out.println(execution.getStatus());
  24. }
  25. } catch (ServerException e) {
  26. e.printStackTrace();
  27. } catch (ClientException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }