0

I've the following code that tries to delete a resource group asynchronously with Azure Java SDK APIs. I expects the code to execute without waiting for the completion of delete request. But the control flow get blocked in the async function invocation and I get the control back only after executing the delete request.

import java.io.File;
import java.io.IOException;
import java.time.Instant;
import com.microsoft.azure.CloudException;
import com.microsoft.azure.management.Azure;
import com.microsoft.rest.LogLevel;
import com.microsoft.rest.ServiceCallback;

public class TestAsyncDeleteMain {

  public static void main(final String[] args) throws CloudException, IOException {
    final File credFile =
        new File(ClassLoader.getSystemClassLoader().getResource("myacc.azureauth").getFile());
    final Azure azureClient = Azure.configure().withLogLevel(LogLevel.BASIC).authenticate(credFile)
        .withDefaultSubscription();
    final String resourceGrpName = "myTestRg";

    System.out.println("Delete RG Async start - Timestamp :" + Instant.now());
    invokeAsyncDelete(azureClient, resourceGrpName);
    System.out.println("Invoked the Async method - Timestamp :" + Instant.now());
  }


  private static void invokeAsyncDelete(final Azure azureClient, final String resourceGrpName) {
    azureClient.resourceGroups().deleteByNameAsync(resourceGrpName, new ServiceCallback<Void>() {

      @Override
      public void success(final Void result) {
        System.out.println("Successfully completed async delete resource group - Timestamp:" + Instant.now());

      }

      @Override
      public void failure(final Throwable t) {
        System.err.println("Failed to remove resource group - Timestamp: " + Instant.now());
        t.printStackTrace();
      }
    });
  }

}

The actual console logs is as follows

Delete RG Async start - Timestamp :2019-07-25T12:46:05.723Z
Successfully completed async delete resource group - Timestamp:2019-07-25T12:49:05.155Z
Invoked the Async method - Timestamp :2019-07-25T12:49:05.156Z

But the output logs I expects in the following order as the delete operation takes time and I assumes the success callback get fired after successful execution of the delete request

Delete RG Async start - Timestamp :<the timestamp>
Invoked the Async method - Timestamp :<the timestamp>
Successfully completed async delete resource group - Timestamp:<the timestamp>

Is this is the right way to invoke the azure's azync functions? Or my understanding on the async functions is wrong?

I have the following azure dependencies in my pom.xml

<dependencies>
   <dependency>
      <groupId>com.microsoft.azure</groupId>
      <artifactId>azure</artifactId>
      <version>1.23.0</version>
   </dependency>
   <dependency>
      <groupId>com.microsoft.azure</groupId>
      <artifactId>azure-mgmt-compute</artifactId>
      <version>1.23.0</version>
   </dependency>
</dependencies>
Master Po
  • 1,497
  • 1
  • 23
  • 42

1 Answers1

0

Java natively has no asynchronous methods.

The Azure SDK for Java is using a third-party library RxJava, which works a little different from the other program language (js or .NET).

As the docs of RxJava: It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.

A sample which may meet your requirement:

    String resourceGrpName = "JackTest";
    System.out.println("Delete RG Async start - Timestamp :" + Instant.now());
    azureClient.resourceGroups().deleteByNameAsync(resourceGrpName).doOnSubscribe( s -> {
        System.out.println("Invoked the Async method - Timestamp :" + Instant.now());
    }).doOnError(t -> {
        System.err.println("Failed to remove resource group - Timestamp: " + Instant.now());
        t.printStackTrace();
    }).doOnCompleted(()->{
        System.out.println("Successfully completed async delete resource group - Timestamp:" + Instant.now());
    }).toObservable().subscribe();

OutPut:

Delete RG Async start - Timestamp :2019-07-31T03:44:01.243Z
Invoked the Async method - Timestamp :2019-07-31T03:44:01.348Z
Successfully completed async delete resource group - Timestamp:2019-07-31T03:44:52.977Z

There are also some official samples which can show you how to use Azure SDK for Java asynchronously.

Jack Jia
  • 5,268
  • 1
  • 12
  • 14