Azure-Samples / compute-java-manage-vm-async

Getting Started with Compute - Manage Virtual Machines using Java asynchronously
https://docs.microsoft.com/java/azure
MIT License
2 stars 1 forks source link

Async methods are not actually Async. They seem to be blocking #2

Open rajdeeprath opened 6 years ago

rajdeeprath commented 6 years ago

PostDeleteChecker checker = new PostDeleteChecker(azure, vmResourceGroup, vmName);
azure.resourceGroups().deleteByNameAsync(vmResourceGroup.name(), checker.deleteServiceCallbackHandler);

ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(checker);
executorService.shutdown();
System.out.println("here");

class PostDeleteChecker implements Runnable
{
Azure azure;
ResourceGroup resourceGroup;
String vmName;

public PostDeleteChecker(Azure azure, ResourceGroup vmResourceGroup, String vmName) {
    this.azure = azure;
    this.resourceGroup = vmResourceGroup;
    this.vmName = vmName;
}

@Override
public void run() {         
    logger.info("Waiting for operation completion...");
}

private ServiceCallback<Void> deleteServiceCallbackHandler = new ServiceCallback<Void>(){

    @Override
    public void failure(Throwable arg0) 
    {
        logger.error("Delete operation Error! " + arg0.getMessage());               
    }

    @Override
    public void success(Void arg0) 
    {
        logger.info("Delete operation Success!");
    }

};

public ServiceCallback<Void> getDeleteServiceCallbackHandler() {
    return deleteServiceCallbackHandler;
}

}   

If you run the above code, neither System.out.println("here"); nor logger.info("Waiting for operation completion..."); gets executed until the resource group has been deleted completed. Then how is this asynchronous in any way?

rajdeeprath commented 6 years ago

Apologies but i cant get the code formatting to work properly. Also most searches on deleteAsync on resource group or vm sends me to a 404 page.,

rajdeeprath commented 6 years ago

Sorry, but the example is not very clear on this. I even tried this: https://github.com/Azure/azure-sdk-for-java/wiki/Asynchronous-calls , but got no good result. If i don't pass a ServiceCallobject or a Completablethen the operation is not performed at all.