1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -54,31 +54,40 @@ public class ReactorClientHttpConnector implements ClientHttpConnector, SmartLif
54
54
private static final Function <HttpClient , HttpClient > defaultInitializer = client -> client .compress (true );
55
55
56
56
57
- private HttpClient httpClient ;
58
-
59
57
@ Nullable
60
58
private final ReactorResourceFactory resourceFactory ;
61
59
62
60
@ Nullable
63
61
private final Function <HttpClient , HttpClient > mapper ;
64
62
65
- private volatile boolean running = true ;
63
+ @ Nullable
64
+ private volatile HttpClient httpClient ;
66
65
67
66
private final Object lifecycleMonitor = new Object ();
68
67
69
68
70
69
/**
71
70
* Default constructor. Initializes {@link HttpClient} via:
72
- * <pre class="code">
73
- * HttpClient.create().compress()
74
- * </pre>
71
+ * <pre class="code">HttpClient.create().compress(true)</pre>
75
72
*/
76
73
public ReactorClientHttpConnector () {
77
74
this .httpClient = defaultInitializer .apply (HttpClient .create ());
78
75
this .resourceFactory = null ;
79
76
this .mapper = null ;
80
77
}
81
78
79
+ /**
80
+ * Constructor with a pre-configured {@code HttpClient} instance.
81
+ * @param httpClient the client to use
82
+ * @since 5.1
83
+ */
84
+ public ReactorClientHttpConnector (HttpClient httpClient ) {
85
+ Assert .notNull (httpClient , "HttpClient is required" );
86
+ this .httpClient = httpClient ;
87
+ this .resourceFactory = null ;
88
+ this .mapper = null ;
89
+ }
90
+
82
91
/**
83
92
* Constructor with externally managed Reactor Netty resources, including
84
93
* {@link LoopResources} for event loop threads, and {@link ConnectionProvider}
@@ -98,50 +107,34 @@ public ReactorClientHttpConnector() {
98
107
* @since 5.1
99
108
*/
100
109
public ReactorClientHttpConnector (ReactorResourceFactory resourceFactory , Function <HttpClient , HttpClient > mapper ) {
101
- this .httpClient = createHttpClient (resourceFactory , mapper );
102
110
this .resourceFactory = resourceFactory ;
103
111
this .mapper = mapper ;
112
+ if (resourceFactory .isRunning ()) {
113
+ this .httpClient = createHttpClient (resourceFactory , mapper );
114
+ }
104
115
}
105
116
106
- private static HttpClient createHttpClient (ReactorResourceFactory resourceFactory , Function <HttpClient , HttpClient > mapper ) {
107
- ConnectionProvider provider = resourceFactory .getConnectionProvider ();
108
- Assert .notNull (provider , "No ConnectionProvider: is ReactorResourceFactory not initialized yet?" );
109
- return defaultInitializer .andThen (mapper ).andThen (applyLoopResources (resourceFactory ))
110
- .apply (HttpClient .create (provider ));
111
- }
112
-
113
- private static Function <HttpClient , HttpClient > applyLoopResources (ReactorResourceFactory factory ) {
114
- return httpClient -> {
115
- LoopResources resources = factory .getLoopResources ();
116
- Assert .notNull (resources , "No LoopResources: is ReactorResourceFactory not initialized yet?" );
117
- return httpClient .runOn (resources );
118
- };
119
- }
120
-
121
-
122
- /**
123
- * Constructor with a pre-configured {@code HttpClient} instance.
124
- * @param httpClient the client to use
125
- * @since 5.1
126
- */
127
- public ReactorClientHttpConnector (HttpClient httpClient ) {
128
- Assert .notNull (httpClient , "HttpClient is required" );
129
- this .httpClient = httpClient ;
130
- this .resourceFactory = null ;
131
- this .mapper = null ;
117
+ private static HttpClient createHttpClient (ReactorResourceFactory factory , Function <HttpClient , HttpClient > mapper ) {
118
+ return defaultInitializer .andThen (mapper ).andThen (httpClient -> httpClient .runOn (factory .getLoopResources ()))
119
+ .apply (HttpClient .create (factory .getConnectionProvider ()));
132
120
}
133
121
134
122
135
123
@ Override
136
124
public Mono <ClientHttpResponse > connect (HttpMethod method , URI uri ,
137
125
Function <? super ClientHttpRequest , Mono <Void >> requestCallback ) {
138
126
139
- AtomicReference <ReactorClientHttpResponse > responseRef = new AtomicReference <>();
127
+ HttpClient httpClient = this .httpClient ;
128
+ if (httpClient == null ) {
129
+ Assert .state (this .resourceFactory != null && this .mapper != null , "Illegal configuration" );
130
+ httpClient = createHttpClient (this .resourceFactory , this .mapper );
131
+ }
140
132
141
- HttpClient .RequestSender requestSender = this . httpClient
133
+ HttpClient .RequestSender requestSender = httpClient
142
134
.request (io .netty .handler .codec .http .HttpMethod .valueOf (method .name ()));
143
135
144
136
requestSender = setUri (requestSender , uri );
137
+ AtomicReference <ReactorClientHttpResponse > responseRef = new AtomicReference <>();
145
138
146
139
return requestSender
147
140
.send ((request , outbound ) -> requestCallback .apply (adaptRequest (method , uri , request , outbound )))
@@ -176,46 +169,34 @@ private ReactorClientHttpRequest adaptRequest(HttpMethod method, URI uri, HttpCl
176
169
return new ReactorClientHttpRequest (method , uri , request , nettyOutbound );
177
170
}
178
171
172
+
179
173
@ Override
180
174
public void start () {
181
- synchronized (this .lifecycleMonitor ) {
182
- if (! isRunning () ) {
183
- if (this .resourceFactory != null && this . mapper ! = null ) {
175
+ if (this .resourceFactory != null && this . mapper != null ) {
176
+ synchronized ( this . lifecycleMonitor ) {
177
+ if (this .httpClient = = null ) {
184
178
this .httpClient = createHttpClient (this .resourceFactory , this .mapper );
185
179
}
186
- else {
187
- logger .warn ("Restarting a ReactorClientHttpConnector bean is only supported with externally managed Reactor Netty resources" );
188
- }
189
- this .running = true ;
190
180
}
191
181
}
182
+ else {
183
+ logger .warn ("Restarting a ReactorClientHttpConnector bean is only supported " +
184
+ "with externally managed Reactor Netty resources" );
185
+ }
192
186
}
193
187
194
188
@ Override
195
189
public void stop () {
196
- synchronized (this .lifecycleMonitor ) {
197
- if ( isRunning () ) {
198
- this .running = false ;
190
+ if (this .resourceFactory != null && this . mapper != null ) {
191
+ synchronized ( this . lifecycleMonitor ) {
192
+ this .httpClient = null ;
199
193
}
200
194
}
201
195
}
202
196
203
- @ Override
204
- public final void stop (Runnable callback ) {
205
- synchronized (this .lifecycleMonitor ) {
206
- stop ();
207
- callback .run ();
208
- }
209
- }
210
-
211
197
@ Override
212
198
public boolean isRunning () {
213
- return this .running ;
214
- }
215
-
216
- @ Override
217
- public boolean isAutoStartup () {
218
- return false ;
199
+ return (this .httpClient != null );
219
200
}
220
201
221
202
@ Override
0 commit comments