Advertisement
fkeles

asynchronousClient.java

Oct 10th, 2023
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | Source Code | 0 0
  1.     private static ArrayList<String> asynchronousClient(int numberOfInstances) throws Exception {
  2.         long startTime = System.nanoTime();
  3.         System.out.println("-----------------------------------------------------------------------");
  4.         ArrayList<String> instanceIds = new ArrayList<>();
  5.         // Futures for async handlers
  6.         List<Future<LaunchInstanceResponse>> futures = new CopyOnWriteArrayList<>();
  7.  
  8.         AsyncHandler<LaunchInstanceRequest, LaunchInstanceResponse> handler = new AsyncHandler<LaunchInstanceRequest, LaunchInstanceResponse>() {
  9.             @Override
  10.             public void onSuccess(LaunchInstanceRequest request, LaunchInstanceResponse response) {
  11.                 String requestId = response.getOpcRequestId();
  12.                 Instance instance = response.getInstance();
  13.                 // System.out.println(requestId);
  14.                 // System.out.println(requestId + " - " + instance.getDisplayName() + " - " +
  15.                 // instance.getId());
  16.             }
  17.  
  18.             @Override
  19.             public void onError(LaunchInstanceRequest request, Throwable error) {
  20.                 error.printStackTrace();
  21.             }
  22.         };
  23.  
  24.         // Custom client config
  25.         ClientConfiguration clientConfig = ClientConfiguration.builder()
  26.                 .connectionTimeoutMillis(3000)
  27.                 .readTimeoutMillis(60000)
  28.                 .build();
  29.         // Auth provider
  30.         AuthenticationDetailsProvider authenticationDetailsProvider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
  31.  
  32.         // Compute client
  33.         ComputeAsyncClient computeClient = ComputeAsyncClient.builder()
  34.                 .configuration(clientConfig)
  35.                 .build(authenticationDetailsProvider);
  36.         computeClient.setRegion(Region.valueOf(System.getenv("REGION")));
  37.  
  38.         // Default values
  39.         String availabilityDomain = System.getenv("AD");
  40.         String compartmentId = System.getenv("COMPARTMENT_ID");
  41.         String imageId = System.getenv("IMAGE_ID");
  42.         String subnetId = System.getenv("SUBNET_ID");
  43.         String shape = System.getenv("SHAPE");
  44.         String instanceName = System.getenv("INSTANCE_NAME_PREFIX");
  45.  
  46.         for (int i = 0; i < numberOfInstances; i++) {
  47.             long startTime1 = System.nanoTime();
  48.             // Set instance details
  49.             LaunchInstanceDetails launchInstanceDetails = LaunchInstanceDetails.builder()
  50.                     .availabilityDomain(availabilityDomain)
  51.                     .compartmentId(compartmentId)
  52.                     .displayName(instanceName + (i + 1))
  53.                     .imageId(imageId)
  54.                     .subnetId(subnetId)
  55.                     .shape(shape)
  56.                     .build();
  57.  
  58.             // Create request
  59.             LaunchInstanceRequest launchInstanceRequest = LaunchInstanceRequest.builder()
  60.                     .launchInstanceDetails(launchInstanceDetails)
  61.                     .build();
  62.  
  63.             // Send asynch request
  64.             Future<LaunchInstanceResponse> future = computeClient.launchInstance(launchInstanceRequest, handler);
  65.             futures.add(future);
  66.  
  67.             long elapsedTime1 = System.nanoTime() - startTime1;
  68.             System.out.println("work requested in " + (elapsedTime1 / 1000000) + " ms");
  69.         }
  70.  
  71.         // wait for the threads to complete
  72.         int completed = 0;
  73.         while (completed < numberOfInstances) {
  74.             for (Future<LaunchInstanceResponse> f : futures) {
  75.                 if (f.isDone()) {
  76.                     LaunchInstanceResponse response = (LaunchInstanceResponse) f.get();
  77.                     String requestId = response.getOpcRequestId();
  78.                     Instance instance = response.getInstance();
  79.                     // System.out.println(requestId);
  80.                     System.out.println(requestId + " - " + instance.getDisplayName() + " - " + instance.getId());
  81.                     instanceIds.add(instance.getId());
  82.                     completed++;
  83.                     futures.remove(f);
  84.                 }
  85.             }
  86.         }
  87.  
  88.         long elapsedTime = System.nanoTime() - startTime;
  89.         System.out.println("Total execution time in seconds: " + TimeUnit.MILLISECONDS.toSeconds(elapsedTime / 1000000));
  90.         System.out.println("-----------------------------------------------------------------------");
  91.         return instanceIds;
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement