30
30
31
31
import org .springframework .aop .framework .ProxyFactory ;
32
32
import org .springframework .aop .framework .ReflectiveMethodInvocation ;
33
- import org .springframework .beans .factory .InitializingBean ;
34
- import org .springframework .context .EmbeddedValueResolverAware ;
35
33
import org .springframework .core .MethodIntrospector ;
36
34
import org .springframework .core .ReactiveAdapterRegistry ;
37
35
import org .springframework .core .annotation .AnnotatedElementUtils ;
44
42
* Factory for creating a client proxy given an RSocket service interface with
45
43
* {@link RSocketExchange @RSocketExchange} methods.
46
44
*
47
- * <p>This class is intended to be declared as a bean in Spring configuration.
45
+ * <p>To create an instance, use static methods to obtain a
46
+ * {@link Builder Builder}.
48
47
*
49
48
* @author Rossen Stoyanchev
50
49
* @since 6.0
51
50
*/
52
- public final class RSocketServiceProxyFactory implements InitializingBean , EmbeddedValueResolverAware {
51
+ public final class RSocketServiceProxyFactory {
53
52
54
- @ Nullable
55
- private final BuilderInitializedFactory builderInitializedFactory ;
53
+ private final RSocketRequester rsocketRequester ;
54
+
55
+ private final List <RSocketServiceArgumentResolver > argumentResolvers ;
56
56
57
57
@ Nullable
58
- private final BeanStyleFactory beanStyleFactory ;
58
+ private final StringValueResolver embeddedValueResolver ;
59
59
60
+ private final ReactiveAdapterRegistry reactiveAdapterRegistry ;
61
+
62
+ private final Duration blockTimeout ;
60
63
61
- /**
62
- * Create an instance with the underlying RSocketRequester to perform requests with.
63
- * @param rsocketRequester the requester to use
64
- * @deprecated in favor of using the Builder to initialize the
65
- * RSocketServiceProxyFactory instance.
66
- */
67
- @ Deprecated (since = "6.0.0-RC2" , forRemoval = true )
68
- public RSocketServiceProxyFactory (RSocketRequester rsocketRequester ) {
69
- this .beanStyleFactory = new BeanStyleFactory (rsocketRequester );
70
- this .builderInitializedFactory = null ;
71
- }
72
64
73
65
private RSocketServiceProxyFactory (
74
66
RSocketRequester rsocketRequester , List <RSocketServiceArgumentResolver > argumentResolvers ,
75
67
@ Nullable StringValueResolver embeddedValueResolver ,
76
68
ReactiveAdapterRegistry reactiveAdapterRegistry , Duration blockTimeout ) {
77
69
78
- this .beanStyleFactory = null ;
79
- this .builderInitializedFactory = new BuilderInitializedFactory (
80
- rsocketRequester , argumentResolvers , embeddedValueResolver , reactiveAdapterRegistry , blockTimeout );
81
- }
82
-
83
-
84
- /**
85
- * Register a custom argument resolver, invoked ahead of default resolvers.
86
- * @param resolver the resolver to add
87
- * @deprecated in favor of using the Builder to initialize the
88
- * RSocketServiceProxyFactory instance.
89
- */
90
- @ Deprecated (since = "6.0.0-RC2" , forRemoval = true )
91
- public void addCustomArgumentResolver (RSocketServiceArgumentResolver resolver ) {
92
- Assert .state (this .beanStyleFactory != null , "RSocketServiceProxyFactory was created through the builder" );
93
- this .beanStyleFactory .addCustomArgumentResolver (resolver );
94
- }
95
-
96
- /**
97
- * Set the custom argument resolvers to use, ahead of default resolvers.
98
- * @param resolvers the resolvers to use
99
- * @deprecated in favor of using the Builder to initialize the
100
- * RSocketServiceProxyFactory instance.
101
- */
102
- @ Deprecated (since = "6.0.0-RC2" , forRemoval = true )
103
- public void setCustomArgumentResolvers (List <RSocketServiceArgumentResolver > resolvers ) {
104
- Assert .state (this .beanStyleFactory != null , "RSocketServiceProxyFactory was created through the builder" );
105
- this .beanStyleFactory .setCustomArgumentResolvers (resolvers );
106
- }
107
-
108
- /**
109
- * Set the StringValueResolver to use for resolving placeholders and
110
- * expressions in {@link RSocketExchange#value()}.
111
- * @param resolver the resolver to use
112
- * @deprecated in favor of using the Builder to initialize the
113
- * RSocketServiceProxyFactory instance.
114
- */
115
- @ Deprecated (since = "6.0.0-RC2" , forRemoval = true )
116
- @ Override
117
- public void setEmbeddedValueResolver (StringValueResolver resolver ) {
118
- if (this .beanStyleFactory != null ) {
119
- this .beanStyleFactory .setEmbeddedValueResolver (resolver );
120
- }
121
- }
122
-
123
- /**
124
- * Set the {@link ReactiveAdapterRegistry} to use to support different
125
- * asynchronous types for RSocket service method return values.
126
- * <p>By default this is {@link ReactiveAdapterRegistry#getSharedInstance()}.
127
- * @deprecated in favor of using the Builder to initialize the
128
- * RSocketServiceProxyFactory instance.
129
- */
130
- @ Deprecated (since = "6.0.0-RC2" , forRemoval = true )
131
- public void setReactiveAdapterRegistry (ReactiveAdapterRegistry registry ) {
132
- Assert .state (this .beanStyleFactory != null , "RSocketServiceProxyFactory was created through the builder" );
133
- this .beanStyleFactory .setReactiveAdapterRegistry (registry );
134
- }
135
-
136
- /**
137
- * Configure how long to wait for a response for an RSocket service method
138
- * with a synchronous (blocking) method signature.
139
- * <p>By default this is 5 seconds.
140
- * @param blockTimeout the timeout value
141
- * @deprecated in favor of using the Builder to initialize the
142
- * RSocketServiceProxyFactory instance.
143
- */
144
- @ Deprecated (since = "6.0.0-RC2" , forRemoval = true )
145
- public void setBlockTimeout (Duration blockTimeout ) {
146
- Assert .state (this .beanStyleFactory != null , "RSocketServiceProxyFactory was created through the builder" );
147
- this .beanStyleFactory .setBlockTimeout (blockTimeout );
148
- }
149
-
150
-
151
- @ Override
152
- @ Deprecated
153
- public void afterPropertiesSet () throws Exception {
154
- if (this .beanStyleFactory != null ) {
155
- this .beanStyleFactory .afterPropertiesSet ();
156
- }
70
+ this .rsocketRequester = rsocketRequester ;
71
+ this .argumentResolvers = argumentResolvers ;
72
+ this .embeddedValueResolver = embeddedValueResolver ;
73
+ this .reactiveAdapterRegistry = reactiveAdapterRegistry ;
74
+ this .blockTimeout = blockTimeout ;
157
75
}
158
76
159
77
@@ -166,15 +84,26 @@ public void afterPropertiesSet() throws Exception {
166
84
* @return the created proxy
167
85
*/
168
86
public <S > S createClient (Class <S > serviceType ) {
169
- if (this .builderInitializedFactory != null ) {
170
- return this .builderInitializedFactory .createClient (serviceType );
171
- }
172
- else if (this .beanStyleFactory != null ) {
173
- return this .beanStyleFactory .createClient (serviceType );
174
- }
175
- else {
176
- throw new IllegalStateException ("Expected Builder initialized or Bean-style delegate" );
177
- }
87
+
88
+ List <RSocketServiceMethod > serviceMethods =
89
+ MethodIntrospector .selectMethods (serviceType , this ::isExchangeMethod ).stream ()
90
+ .map (method -> createRSocketServiceMethod (serviceType , method ))
91
+ .toList ();
92
+
93
+ return ProxyFactory .getProxy (serviceType , new ServiceMethodInterceptor (serviceMethods ));
94
+ }
95
+
96
+ private boolean isExchangeMethod (Method method ) {
97
+ return AnnotatedElementUtils .hasAnnotation (method , RSocketExchange .class );
98
+ }
99
+
100
+ private <S > RSocketServiceMethod createRSocketServiceMethod (Class <S > serviceType , Method method ) {
101
+ Assert .notNull (this .argumentResolvers ,
102
+ "No argument resolvers: afterPropertiesSet was not called" );
103
+
104
+ return new RSocketServiceMethod (
105
+ method , serviceType , this .argumentResolvers , this .rsocketRequester ,
106
+ this .embeddedValueResolver , this .reactiveAdapterRegistry , this .blockTimeout );
178
107
}
179
108
180
109
@@ -330,163 +259,4 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
330
259
}
331
260
}
332
261
333
-
334
- /**
335
- * Temporary class until bean-style initialization is removed.
336
- */
337
- private static final class BuilderInitializedFactory {
338
-
339
- private final RSocketRequester rsocketRequester ;
340
-
341
- private final List <RSocketServiceArgumentResolver > argumentResolvers ;
342
-
343
- @ Nullable
344
- private final StringValueResolver embeddedValueResolver ;
345
-
346
- private final ReactiveAdapterRegistry reactiveAdapterRegistry ;
347
-
348
- private final Duration blockTimeout ;
349
-
350
-
351
- public BuilderInitializedFactory (
352
- RSocketRequester rsocketRequester , List <RSocketServiceArgumentResolver > argumentResolvers ,
353
- @ Nullable StringValueResolver embeddedValueResolver ,
354
- ReactiveAdapterRegistry reactiveAdapterRegistry , Duration blockTimeout ) {
355
-
356
- this .rsocketRequester = rsocketRequester ;
357
- this .argumentResolvers = argumentResolvers ;
358
- this .embeddedValueResolver = embeddedValueResolver ;
359
- this .reactiveAdapterRegistry = reactiveAdapterRegistry ;
360
- this .blockTimeout = blockTimeout ;
361
- }
362
-
363
-
364
- public <S > S createClient (Class <S > serviceType ) {
365
-
366
- List <RSocketServiceMethod > serviceMethods =
367
- MethodIntrospector .selectMethods (serviceType , this ::isExchangeMethod ).stream ()
368
- .map (method -> createRSocketServiceMethod (serviceType , method ))
369
- .toList ();
370
-
371
- return ProxyFactory .getProxy (serviceType , new ServiceMethodInterceptor (serviceMethods ));
372
- }
373
-
374
- private boolean isExchangeMethod (Method method ) {
375
- return AnnotatedElementUtils .hasAnnotation (method , RSocketExchange .class );
376
- }
377
-
378
- private <S > RSocketServiceMethod createRSocketServiceMethod (Class <S > serviceType , Method method ) {
379
- Assert .notNull (this .argumentResolvers ,
380
- "No argument resolvers: afterPropertiesSet was not called" );
381
-
382
- return new RSocketServiceMethod (
383
- method , serviceType , this .argumentResolvers , this .rsocketRequester ,
384
- this .embeddedValueResolver , this .reactiveAdapterRegistry , this .blockTimeout );
385
- }
386
- }
387
-
388
-
389
- /**
390
- * Temporary class to support bean-style initialization during deprecation period.
391
- */
392
- private static final class BeanStyleFactory implements InitializingBean , EmbeddedValueResolverAware {
393
-
394
- private final RSocketRequester rsocketRequester ;
395
-
396
- @ Nullable
397
- private List <RSocketServiceArgumentResolver > customArgumentResolvers ;
398
-
399
- @ Nullable
400
- private List <RSocketServiceArgumentResolver > argumentResolvers ;
401
-
402
- @ Nullable
403
- private StringValueResolver embeddedValueResolver ;
404
-
405
- private ReactiveAdapterRegistry reactiveAdapterRegistry = ReactiveAdapterRegistry .getSharedInstance ();
406
-
407
- private Duration blockTimeout = Duration .ofSeconds (5 );
408
-
409
-
410
- public BeanStyleFactory (RSocketRequester rsocketRequester ) {
411
- Assert .notNull (rsocketRequester , "RSocketRequester is required" );
412
- this .rsocketRequester = rsocketRequester ;
413
- }
414
-
415
-
416
- public void addCustomArgumentResolver (RSocketServiceArgumentResolver resolver ) {
417
- if (this .customArgumentResolvers == null ) {
418
- this .customArgumentResolvers = new ArrayList <>();
419
- }
420
- this .customArgumentResolvers .add (resolver );
421
- }
422
-
423
- public void setCustomArgumentResolvers (List <RSocketServiceArgumentResolver > resolvers ) {
424
- this .customArgumentResolvers = new ArrayList <>(resolvers );
425
- }
426
-
427
- @ Override
428
- public void setEmbeddedValueResolver (StringValueResolver resolver ) {
429
- this .embeddedValueResolver = resolver ;
430
- }
431
-
432
- public void setReactiveAdapterRegistry (ReactiveAdapterRegistry registry ) {
433
- this .reactiveAdapterRegistry = registry ;
434
- }
435
-
436
- public void setBlockTimeout (Duration blockTimeout ) {
437
- this .blockTimeout = blockTimeout ;
438
- }
439
-
440
-
441
- @ Override
442
- public void afterPropertiesSet () {
443
- this .argumentResolvers = initArgumentResolvers ();
444
- }
445
-
446
- private List <RSocketServiceArgumentResolver > initArgumentResolvers () {
447
- List <RSocketServiceArgumentResolver > resolvers = new ArrayList <>();
448
-
449
- // Custom
450
- if (this .customArgumentResolvers != null ) {
451
- resolvers .addAll (this .customArgumentResolvers );
452
- }
453
-
454
- // Annotation-based
455
- resolvers .add (new PayloadArgumentResolver (this .reactiveAdapterRegistry , false ));
456
- resolvers .add (new DestinationVariableArgumentResolver ());
457
-
458
- // Type-based
459
- resolvers .add (new MetadataArgumentResolver ());
460
-
461
- // Fallback
462
- resolvers .add (new PayloadArgumentResolver (this .reactiveAdapterRegistry , true ));
463
-
464
- return resolvers ;
465
- }
466
-
467
-
468
- public <S > S createClient (Class <S > serviceType ) {
469
-
470
- List <RSocketServiceMethod > serviceMethods =
471
- MethodIntrospector .selectMethods (serviceType , this ::isExchangeMethod ).stream ()
472
- .map (method -> createRSocketServiceMethod (serviceType , method ))
473
- .toList ();
474
-
475
- return ProxyFactory .getProxy (serviceType , new ServiceMethodInterceptor (serviceMethods ));
476
- }
477
-
478
- private boolean isExchangeMethod (Method method ) {
479
- return AnnotatedElementUtils .hasAnnotation (method , RSocketExchange .class );
480
- }
481
-
482
- private <S > RSocketServiceMethod createRSocketServiceMethod (Class <S > serviceType , Method method ) {
483
- Assert .notNull (this .argumentResolvers ,
484
- "No argument resolvers: afterPropertiesSet was not called" );
485
-
486
- return new RSocketServiceMethod (
487
- method , serviceType , this .argumentResolvers , this .rsocketRequester ,
488
- this .embeddedValueResolver , this .reactiveAdapterRegistry , this .blockTimeout );
489
- }
490
- }
491
-
492
262
}
0 commit comments