44package com .microsoft .durabletask .client .azuremanaged ;
55
66import com .azure .core .credential .TokenCredential ;
7- import com .azure .core .credential .TokenRequestContext ;
87import com .microsoft .durabletask .DurableTaskClient ;
98import com .microsoft .durabletask .DurableTaskGrpcClientBuilder ;
10- import com .microsoft .durabletask .shared .azuremanaged .AccessTokenCache ;
11-
129import io .grpc .Channel ;
13- import io .grpc .ClientCall ;
14- import io .grpc .ClientInterceptor ;
15- import io .grpc .ForwardingClientCall ;
16- import io .grpc .ManagedChannelBuilder ;
17- import io .grpc .Metadata ;
18- import io .grpc .MethodDescriptor ;
19- import io .grpc .CallOptions ;
20-
2110import java .util .Objects ;
11+ import javax .annotation .Nullable ;
2212
2313/**
2414 * Extension methods for creating DurableTaskClient instances that connect to Azure-managed Durable Task Scheduler.
15+ * This class provides various methods to create and configure clients using either connection strings or explicit parameters.
2516 */
26- public class DurableTaskSchedulerClientExtensions {
17+ public static class DurableTaskSchedulerClientExtensions {
18+ /**
19+ * Creates a DurableTaskClient using a connection string.
20+ *
21+ * @param connectionString The connection string for Azure-managed Durable Task Scheduler.
22+ * @return A new DurableTaskClient instance.
23+ */
24+ public static DurableTaskClient createClient (String connectionString ) {
25+ return createClient (connectionString , null );
26+ }
27+
2728 /**
28- * Creates a DurableTaskClient that connects to Azure-managed Durable Task Scheduler using a connection string.
29+ * Creates a DurableTaskClient using a connection string and token credential .
2930 *
30- * @param connectionString The connection string for connecting to Azure-managed Durable Task Scheduler.
31- * @param tokenCredential The token credential for connecting to Azure-managed Durable Task Scheduler .
31+ * @param connectionString The connection string for Azure-managed Durable Task Scheduler.
32+ * @param tokenCredential The token credential for authentication, or null to use connection string credentials .
3233 * @return A new DurableTaskClient instance.
34+ * @throws NullPointerException if connectionString is null
3335 */
3436 public static DurableTaskClient createClient (String connectionString , @ Nullable TokenCredential tokenCredential ) {
3537 Objects .requireNonNull (connectionString , "connectionString must not be null" );
36- DurableTaskSchedulerClientOptions options = DurableTaskSchedulerClientOptions .fromConnectionString (connectionString , tokenCredential );
37- Channel grpcChannel = options .createGrpcChannel ();
38-
39- return new DurableTaskGrpcClientBuilder ()
40- .grpcChannel (grpcChannel )
41- .build ();
38+ return createClientFromOptions (
39+ DurableTaskSchedulerClientOptions .fromConnectionString (connectionString , tokenCredential ));
4240 }
4341
44- public static DurableTaskClient createClient (String endpoint , String taskHubName , @ Nullable TokenCredential tokenCredential ) {
45- DurableTaskSchedulerClientOptions options = new DurableTaskSchedulerClientOptions ()
46- .setEndpoint (endpoint )
42+ /**
43+ * Creates a DurableTaskClient using explicit endpoint and task hub parameters.
44+ *
45+ * @param endpoint The endpoint address for Azure-managed Durable Task Scheduler.
46+ * @param taskHubName The name of the task hub to connect to.
47+ * @param tokenCredential The token credential for authentication, or null for anonymous access.
48+ * @return A new DurableTaskClient instance.
49+ * @throws NullPointerException if endpoint or taskHubName is null
50+ */
51+ public static DurableTaskClient createClient (
52+ String endpoint ,
53+ String taskHubName ,
54+ @ Nullable TokenCredential tokenCredential ) {
55+ Objects .requireNonNull (endpoint , "endpoint must not be null" );
56+ Objects .requireNonNull (taskHubName , "taskHubName must not be null" );
57+
58+ return createClientFromOptions (new DurableTaskSchedulerClientOptions ()
59+ .setEndpointAddress (endpoint )
4760 .setTaskHubName (taskHubName )
48- .setTokenCredential (tokenCredential );
49-
50- Channel grpcChannel = options .createGrpcChannel ();
61+ .setCredential (tokenCredential ));
62+ }
5163
52- return new DurableTaskGrpcClientBuilder ()
53- .grpcChannel (grpcChannel )
54- .build ();
64+ /**
65+ * Configures a DurableTaskGrpcClientBuilder to use Azure-managed Durable Task Scheduler with a connection string.
66+ *
67+ * @param builder The builder to configure.
68+ * @param connectionString The connection string for Azure-managed Durable Task Scheduler.
69+ * @param tokenCredential The token credential for authentication, or null to use connection string credentials.
70+ * @throws NullPointerException if builder or connectionString is null
71+ */
72+ public static void useDurableTaskScheduler (
73+ DurableTaskGrpcClientBuilder builder ,
74+ String connectionString ,
75+ @ Nullable TokenCredential tokenCredential ) {
76+ Objects .requireNonNull (builder , "builder must not be null" );
77+ Objects .requireNonNull (connectionString , "connectionString must not be null" );
78+
79+ configureBuilder (builder ,
80+ DurableTaskSchedulerClientOptions .fromConnectionString (connectionString , tokenCredential ));
5581 }
5682
57- public static void UseDurableTaskScheduler (DurableTaskGrpcClientBuilder builder , String endpoint , String taskHubName , @ Nullable TokenCredential tokenCredential ) {
58- DurableTaskSchedulerClientOptions options = new DurableTaskSchedulerClientOptions ()
59- .setEndpoint (endpoint )
83+ /**
84+ * Configures a DurableTaskGrpcClientBuilder to use Azure-managed Durable Task Scheduler with explicit parameters.
85+ *
86+ * @param builder The builder to configure.
87+ * @param endpoint The endpoint address for Azure-managed Durable Task Scheduler.
88+ * @param taskHubName The name of the task hub to connect to.
89+ * @param tokenCredential The token credential for authentication, or null for anonymous access.
90+ * @throws NullPointerException if builder, endpoint, or taskHubName is null
91+ */
92+ public static void useDurableTaskScheduler (
93+ DurableTaskGrpcClientBuilder builder ,
94+ String endpoint ,
95+ String taskHubName ,
96+ @ Nullable TokenCredential tokenCredential ) {
97+ Objects .requireNonNull (builder , "builder must not be null" );
98+ Objects .requireNonNull (endpoint , "endpoint must not be null" );
99+ Objects .requireNonNull (taskHubName , "taskHubName must not be null" );
100+
101+ configureBuilder (builder , new DurableTaskSchedulerClientOptions ()
102+ .setEndpointAddress (endpoint )
60103 .setTaskHubName (taskHubName )
61- .setTokenCredential (tokenCredential );
62-
63- Channel grpcChannel = options .createGrpcChannel ();
64- builder .grpcChannel (grpcChannel );
65- }
66- public static void UseDurableTaskScheduler (DurableTaskGrpcClientBuilder builder , String connectionString , @ Nullable TokenCredential tokenCredential ) {
67- DurableTaskSchedulerClientOptions options = DurableTaskSchedulerClientOptions .fromConnectionString (connectionString , tokenCredential );
68- Channel grpcChannel = options .createGrpcChannel ();
69- builder .grpcChannel (grpcChannel );
104+ .setCredential (tokenCredential ));
70105 }
71106
72107 /**
73- * Creates a DurableTaskGrpcClientBuilder that connects to Azure-managed Durable Task Scheduler using a connection string.
108+ * Creates a DurableTaskGrpcClientBuilder configured for Azure-managed Durable Task Scheduler using a connection string.
74109 *
75- * @param connectionString The connection string for connecting to Azure-managed Durable Task Scheduler.
76- * @param tokenCredential The token credential for connecting to Azure-managed Durable Task Scheduler.
77- * @return A new DurableTaskGrpcClientBuilder instance.
110+ * @param connectionString The connection string for Azure-managed Durable Task Scheduler.
111+ * @param tokenCredential The token credential for authentication, or null to use connection string credentials.
112+ * @return A new configured DurableTaskGrpcClientBuilder instance.
113+ * @throws NullPointerException if connectionString is null
78114 */
79- public static DurableTaskGrpcClientBuilder createClientBuilder (String connectionString , @ Nullable TokenCredential tokenCredential ) {
115+ public static DurableTaskGrpcClientBuilder createClientBuilder (
116+ String connectionString ,
117+ @ Nullable TokenCredential tokenCredential ) {
80118 Objects .requireNonNull (connectionString , "connectionString must not be null" );
81- DurableTaskSchedulerClientOptions options = DurableTaskSchedulerClientOptions .fromConnectionString (connectionString , tokenCredential );
82- Channel grpcChannel = options .createGrpcChannel ();
83-
84- return new DurableTaskGrpcClientBuilder ()
85- .grpcChannel (grpcChannel );
119+ return createBuilderFromOptions (
120+ DurableTaskSchedulerClientOptions .fromConnectionString (connectionString , tokenCredential ));
86121 }
87122
88123 /**
89- * Creates a DurableTaskGrpcClientBuilder that connects to Azure-managed Durable Task Scheduler.
124+ * Creates a DurableTaskGrpcClientBuilder configured for Azure-managed Durable Task Scheduler using explicit parameters .
90125 *
91- * @param endpoint The endpoint for connecting to Azure-managed Durable Task Scheduler.
92- * @param taskHubName The task hub name for connecting to Azure-managed Durable Task Scheduler.
93- * @param tokenCredential The token credential for connecting to Azure-managed Durable Task Scheduler.
94- * @return A new DurableTaskGrpcClientBuilder instance.
126+ * @param endpoint The endpoint address for Azure-managed Durable Task Scheduler.
127+ * @param taskHubName The name of the task hub to connect to.
128+ * @param tokenCredential The token credential for authentication, or null for anonymous access.
129+ * @return A new configured DurableTaskGrpcClientBuilder instance.
130+ * @throws NullPointerException if endpoint or taskHubName is null
95131 */
96- public static DurableTaskGrpcClientBuilder createClientBuilder (String endpoint , String taskHubName , @ Nullable TokenCredential tokenCredential ) {
97- DurableTaskSchedulerClientOptions options = new DurableTaskSchedulerClientOptions ()
98- .setEndpoint (endpoint )
132+ public static DurableTaskGrpcClientBuilder createClientBuilder (
133+ String endpoint ,
134+ String taskHubName ,
135+ @ Nullable TokenCredential tokenCredential ) {
136+ Objects .requireNonNull (endpoint , "endpoint must not be null" );
137+ Objects .requireNonNull (taskHubName , "taskHubName must not be null" );
138+
139+ return createBuilderFromOptions (new DurableTaskSchedulerClientOptions ()
140+ .setEndpointAddress (endpoint )
99141 .setTaskHubName (taskHubName )
100- .setTokenCredential (tokenCredential );
142+ .setCredential (tokenCredential ));
143+ }
101144
145+ // Private helper methods to reduce code duplication
146+
147+ private static DurableTaskClient createClientFromOptions (DurableTaskSchedulerClientOptions options ) {
148+ return createBuilderFromOptions (options ).build ();
149+ }
150+
151+ private static DurableTaskGrpcClientBuilder createBuilderFromOptions (DurableTaskSchedulerClientOptions options ) {
102152 Channel grpcChannel = options .createGrpcChannel ();
153+ return new DurableTaskGrpcClientBuilder ().grpcChannel (grpcChannel );
154+ }
103155
104- return new DurableTaskGrpcClientBuilder ()
105- .grpcChannel (grpcChannel );
156+ private static void configureBuilder (DurableTaskGrpcClientBuilder builder , DurableTaskSchedulerClientOptions options ) {
157+ Channel grpcChannel = options .createGrpcChannel ();
158+ builder .grpcChannel (grpcChannel );
106159 }
107160}
0 commit comments