diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java index a50faee9cf16a..3e995fb06a087 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java +++ b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java @@ -13,6 +13,7 @@ import com.microsoft.rest.credentials.TokenCredentials; import java.io.IOException; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** @@ -103,7 +104,8 @@ public void refreshToken() throws IOException { private void acquireAccessToken() throws IOException { String authorityUrl = this.getEnvironment().getAuthenticationEndpoint() + this.getDomain(); - AuthenticationContext context = new AuthenticationContext(authorityUrl, this.getEnvironment().isValidateAuthority(), Executors.newSingleThreadExecutor()); + ExecutorService executor = Executors.newSingleThreadExecutor(); + AuthenticationContext context = new AuthenticationContext(authorityUrl, this.getEnvironment().isValidateAuthority(), executor); try { authenticationResult = context.acquireToken( this.getEnvironment().getTokenAudience(), @@ -111,6 +113,8 @@ private void acquireAccessToken() throws IOException { null).get(); } catch (Exception e) { throw new IOException(e.getMessage(), e); + } finally { + executor.shutdown(); } } } diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/UserTokenCredentials.java b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/UserTokenCredentials.java index 995f32fabc4a1..af02c07507b74 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/UserTokenCredentials.java +++ b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/UserTokenCredentials.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.util.Date; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** @@ -146,7 +147,8 @@ private void acquireAccessToken() throws IOException { private void acquireAccessTokenFromRefreshToken() throws IOException { String authorityUrl = this.getEnvironment().getAuthenticationEndpoint() + this.getDomain(); - AuthenticationContext context = new AuthenticationContext(authorityUrl, this.getEnvironment().isValidateAuthority(), Executors.newSingleThreadExecutor()); + ExecutorService executor = Executors.newSingleThreadExecutor(); + AuthenticationContext context = new AuthenticationContext(authorityUrl, this.getEnvironment().isValidateAuthority(), executor); try { authenticationResult = context.acquireTokenByRefreshToken( authenticationResult.getRefreshToken(), @@ -154,6 +156,8 @@ private void acquireAccessTokenFromRefreshToken() throws IOException { null, null).get(); } catch (Exception e) { throw new IOException(e.getMessage(), e); + } finally { + executor.shutdown(); } } }