Lior Elayev
Lior Elayev

Reputation: 1

How to configure the timeout and retries on NetworkManager client

I want to control the timeout, connection retries number and the delay duration between the retries when the connection to the Azure NetworkManager failed.

Here is how I connect to Azure:

private void connect() {
HttpClient httpClient = HttpClient.createDefault();
            TokenCredential tokenCredential = new ClientSecretCredentialBuilder()
                    .clientSecret(azureCredentials.getDecryptedDeviceLoginPassword())
                    .clientId(azureCredentials.getDeviceLoginUsername())
                    .tenantId(tenantId)
                    .httpClient(httpClient)
                    .build();

AzureProfile azureProfile = new AzureProfile(...)
NetworkManager.Configurable configurable =      NetworkManager.configure().withLogLevel(HttpLogDetailLevel.BODY).withHttpClient(httpClient);
NetworkManager networkManager = configurable.authenticate(tokenCredential, azureProfile);
networkManager.resourceManager().providers().getByName("Microsoft.Compute");
}

When the connection failed there are 3 connection attempts and also delay between each retry.

how can I configure custom timeout, retries and delay between the attempts?

I tried to configure RetryPolicy as the following:

RetryStrategy retryStrategy = getRetryStrategy();
RetryPolicy retryPolicy = new RetryPolicy(retryStrategy);


     private RetryStrategy getRetryStrategy() {
            return new RetryStrategy() {
                @Override
                public int getMaxRetries() {
                    return 1;
                }
    
                @Override
                public Duration calculateRetryDelay(int retryAttempts) {
                    return Duration.ofSeconds(2);
                }
            };
        }

and use it on the networkManager configureation but I still have long delay between the connection attempts.

Upvotes: 0

Views: 333

Answers (1)

Suresh Chikkam
Suresh Chikkam

Reputation: 3473

  • Use the FixedDelay or ExponentialBackoff provided by Azure SDK for Java instead of a custom RetryStrategy. These built-in strategies handle retries and delay durations more effectively.

Sample.java:

public static void main(String[] args) {
        // Create an instance of HttpClient with customized options  
         HttpClient httpClient = new NettyAsyncHttpClientBuilder()  
                .proxy(new ProxyOptions(ProxyOptions.Type.HTTP, "proxyHost", 8888))  
                .responseTimeout(Duration.ofSeconds(30))  
                .retryPolicy(new RetryPolicy(3, Duration.ofSeconds(5)))  
                .build();
        // Build the Azure credentials
        TokenCredential tokenCredential = new ClientSecretCredentialBuilder()
                .tenantId(TENANT_ID)
                .clientId(CLIENT_ID)
                .clientSecret(CLIENT_SECRET)
                .build();

        // Build the Azure profile
        AzureProfile azureProfile = new AzureProfile.Builder()
                .subscriptionId(SUBSCRIPTION_ID)
                .build();

        // Configure the retry policy
        RetryStrategy retryStrategy = new RetryStrategy()
                .withRetryStrategyType(RetryStrategyType.FIXED)
                .withMaxRetryAttempts(3)
                .withDelay(Duration.ofSeconds(2));
        RetryPolicy retryPolicy = new RetryPolicy(retryStrategy);

        // Configure the NetworkManager
        NetworkManager.Configurable networkManagerConfigurable = NetworkManager.configure()
                .withLogLevel(HttpLogDetailLevel.BODY)
                .withHttpClient(httpClient)
                .withRetryPolicy(retryPolicy);

        // Authenticate and connect to NetworkManager
        NetworkManager networkManager = networkManagerConfigurable.authenticate(tokenCredential, azureProfile);

enter image description here

  • NettyAsyncHttpClientBuilder is used to create HttpClient with custom options, including the retry policy. Please check this document as I followed.

Project's build configuration:

<dependencies>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-identity</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-core</artifactId>
        <version>1.19.0</version>
    </dependency>
    <dependency>
        <groupId>com.azure.resourcemanager</groupId>
        <artifactId>azure-resourcemanager-network</artifactId>
        <version>2.6.0</version>
    </dependency>
    <!-- Add other required dependencies -->
</dependencies>

Upvotes: 0

Related Questions