Skip to content

Creating Service Clients

Jonathan Giles edited this page Jul 30, 2019 · 3 revisions

All Azure Java client libraries follow the same API design pattern of offering a Java builder class that is responsible for creating an instance of a client. This separates the definition and instantiation of the client from its operation, allowing the client to be immutable and thus simpler to use. On top of this, all client libraries follow a few important patterns:

  • Client libraries that support both synchronous and asynchronous APIs must offer these in separate classes. This means that in these cases there would be, for example, a KeyVaultClient for sync APIs and a KeyVaultAsyncClient for async APIs. In cases where the client library only offers one type, the same naming pattern is used.

  • There is a single builder class that takes responsibility for building both the sync and async APIs. The builder will be named similarly to the sync client class, with Builder included. For example, KeyVaultClientBuilder. This builder will have buildClient() and buildAsyncClient() methods to create client instances, as appropriate.

Because of these conventions, users of the Java client libraries should feel comfortable that all classes ending in Client will be immutable and provide operations to interact with an Azure service. All classes that end in ClientBuilder will provide operations to configure and create an instance of a particular client type.

Example

The code to create a synchronous Key Vault KeyClient would be similar to the following:

KeyClient client = new KeyClientBuilder()
        .endpoint(<your-vault-url>)
        .credential(new DefaultAzureCredential())
        .buildClient();

Similarly, to create an asynchronous Key Vault KeyAsyncClient, do the following:

KeyAsyncClient client = new KeyClientBuilder()
        .endpoint(<your-vault-url>)
        .credential(new DefaultAzureCredential())
        .buildAsyncClient();
Clone this wiki locally