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>