@@ -61,12 +61,12 @@ public class ReactorResourceFactory
61
61
private Supplier <ConnectionProvider > connectionProviderSupplier = () -> ConnectionProvider .create ("webflux" , 500 );
62
62
63
63
@ Nullable
64
- private ConnectionProvider connectionProvider ;
64
+ private volatile ConnectionProvider connectionProvider ;
65
65
66
66
private Supplier <LoopResources > loopResourcesSupplier = () -> LoopResources .create ("webflux-http" );
67
67
68
68
@ Nullable
69
- private LoopResources loopResources ;
69
+ private volatile LoopResources loopResources ;
70
70
71
71
private boolean manageConnectionProvider = false ;
72
72
@@ -141,16 +141,22 @@ public void setConnectionProvider(ConnectionProvider connectionProvider) {
141
141
142
142
/**
143
143
* Return the configured {@link ConnectionProvider}.
144
+ * <p>Lazily tries to start the resources on demand if not initialized yet.
145
+ * @see #start()
144
146
*/
145
147
public ConnectionProvider getConnectionProvider () {
146
- Assert .state (this .connectionProvider != null , "ConnectionProvider not initialized yet" );
147
- return this .connectionProvider ;
148
+ if (this .connectionProvider == null ) {
149
+ start ();
150
+ }
151
+ ConnectionProvider connectionProvider = this .connectionProvider ;
152
+ Assert .state (connectionProvider != null , "ConnectionProvider not initialized" );
153
+ return connectionProvider ;
148
154
}
149
155
150
156
/**
151
157
* Use this when you don't want to participate in global resources and
152
158
* you want to customize the creation of the managed {@code LoopResources}.
153
- * <p>By default, {@code LoopResources.create("reactor -http")} is used.
159
+ * <p>By default, {@code LoopResources.create("webflux -http")} is used.
154
160
* <p>Note that this option is ignored if {@code userGlobalResources=false} or
155
161
* {@link #setLoopResources(LoopResources)} is set.
156
162
* @param supplier the supplier to use
@@ -170,10 +176,16 @@ public void setLoopResources(LoopResources loopResources) {
170
176
171
177
/**
172
178
* Return the configured {@link LoopResources}.
179
+ * <p>Lazily tries to start the resources on demand if not initialized yet.
180
+ * @see #start()
173
181
*/
174
182
public LoopResources getLoopResources () {
175
- Assert .state (this .loopResources != null , "LoopResources not initialized yet" );
176
- return this .loopResources ;
183
+ if (this .loopResources == null ) {
184
+ start ();
185
+ }
186
+ LoopResources loopResources = this .loopResources ;
187
+ Assert .state (loopResources != null , "LoopResources not initialized" );
188
+ return loopResources ;
177
189
}
178
190
179
191
/**
@@ -220,13 +232,25 @@ public void setApplicationContext(ApplicationContext applicationContext) {
220
232
}
221
233
222
234
235
+ /**
236
+ * Starts the resources if initialized outside an ApplicationContext.
237
+ * This is for backwards compatibility; the preferred way is to rely on
238
+ * the ApplicationContext's {@link SmartLifecycle lifecycle management}.
239
+ * @see #start()
240
+ */
223
241
@ Override
224
242
public void afterPropertiesSet () {
225
243
if (this .applicationContext == null ) {
226
244
start ();
227
245
}
228
246
}
229
247
248
+ /**
249
+ * Stops the resources if initialized outside an ApplicationContext.
250
+ * This is for backwards compatibility; the preferred way is to rely on
251
+ * the ApplicationContext's {@link SmartLifecycle lifecycle management}.
252
+ * @see #stop()
253
+ */
230
254
@ Override
231
255
public void destroy () {
232
256
if (this .applicationContext == null ) {
0 commit comments