Lazily start resources on demand (if necessary outside of lifecycle)
See gh-32945
This commit is contained in:
parent
c3a0eaa95e
commit
c0bef2c693
|
|
@ -61,12 +61,12 @@ public class ReactorResourceFactory
|
||||||
private Supplier<ConnectionProvider> connectionProviderSupplier = () -> ConnectionProvider.create("webflux", 500);
|
private Supplier<ConnectionProvider> connectionProviderSupplier = () -> ConnectionProvider.create("webflux", 500);
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private ConnectionProvider connectionProvider;
|
private volatile ConnectionProvider connectionProvider;
|
||||||
|
|
||||||
private Supplier<LoopResources> loopResourcesSupplier = () -> LoopResources.create("webflux-http");
|
private Supplier<LoopResources> loopResourcesSupplier = () -> LoopResources.create("webflux-http");
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private LoopResources loopResources;
|
private volatile LoopResources loopResources;
|
||||||
|
|
||||||
private boolean manageConnectionProvider = false;
|
private boolean manageConnectionProvider = false;
|
||||||
|
|
||||||
|
|
@ -141,16 +141,22 @@ public class ReactorResourceFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the configured {@link ConnectionProvider}.
|
* Return the configured {@link ConnectionProvider}.
|
||||||
|
* <p>Lazily tries to start the resources on demand if not initialized yet.
|
||||||
|
* @see #start()
|
||||||
*/
|
*/
|
||||||
public ConnectionProvider getConnectionProvider() {
|
public ConnectionProvider getConnectionProvider() {
|
||||||
Assert.state(this.connectionProvider != null, "ConnectionProvider not initialized yet");
|
if (this.connectionProvider == null) {
|
||||||
return this.connectionProvider;
|
start();
|
||||||
|
}
|
||||||
|
ConnectionProvider connectionProvider = this.connectionProvider;
|
||||||
|
Assert.state(connectionProvider != null, "ConnectionProvider not initialized");
|
||||||
|
return connectionProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use this when you don't want to participate in global resources and
|
* Use this when you don't want to participate in global resources and
|
||||||
* you want to customize the creation of the managed {@code LoopResources}.
|
* you want to customize the creation of the managed {@code LoopResources}.
|
||||||
* <p>By default, {@code LoopResources.create("reactor-http")} is used.
|
* <p>By default, {@code LoopResources.create("webflux-http")} is used.
|
||||||
* <p>Note that this option is ignored if {@code userGlobalResources=false} or
|
* <p>Note that this option is ignored if {@code userGlobalResources=false} or
|
||||||
* {@link #setLoopResources(LoopResources)} is set.
|
* {@link #setLoopResources(LoopResources)} is set.
|
||||||
* @param supplier the supplier to use
|
* @param supplier the supplier to use
|
||||||
|
|
@ -170,10 +176,16 @@ public class ReactorResourceFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the configured {@link LoopResources}.
|
* Return the configured {@link LoopResources}.
|
||||||
|
* <p>Lazily tries to start the resources on demand if not initialized yet.
|
||||||
|
* @see #start()
|
||||||
*/
|
*/
|
||||||
public LoopResources getLoopResources() {
|
public LoopResources getLoopResources() {
|
||||||
Assert.state(this.loopResources != null, "LoopResources not initialized yet");
|
if (this.loopResources == null) {
|
||||||
return this.loopResources;
|
start();
|
||||||
|
}
|
||||||
|
LoopResources loopResources = this.loopResources;
|
||||||
|
Assert.state(loopResources != null, "LoopResources not initialized");
|
||||||
|
return loopResources;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -220,6 +232,12 @@ public class ReactorResourceFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the resources if initialized outside an ApplicationContext.
|
||||||
|
* This is for backwards compatibility; the preferred way is to rely on
|
||||||
|
* the ApplicationContext's {@link SmartLifecycle lifecycle management}.
|
||||||
|
* @see #start()
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
if (this.applicationContext == null) {
|
if (this.applicationContext == null) {
|
||||||
|
|
@ -227,6 +245,12 @@ public class ReactorResourceFactory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the resources if initialized outside an ApplicationContext.
|
||||||
|
* This is for backwards compatibility; the preferred way is to rely on
|
||||||
|
* the ApplicationContext's {@link SmartLifecycle lifecycle management}.
|
||||||
|
* @see #stop()
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
if (this.applicationContext == null) {
|
if (this.applicationContext == null) {
|
||||||
|
|
|
||||||
|
|
@ -261,4 +261,22 @@ class ReactorResourceFactoryTests {
|
||||||
assertThat(resourceFactory.isRunning()).isFalse();
|
assertThat(resourceFactory.isRunning()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void lazilyStartOnConnectionProviderAccess() {
|
||||||
|
assertThat(this.resourceFactory.isRunning()).isFalse();
|
||||||
|
this.resourceFactory.getConnectionProvider();
|
||||||
|
assertThat(this.resourceFactory.isRunning()).isTrue();
|
||||||
|
this.resourceFactory.stop();
|
||||||
|
assertThat(this.resourceFactory.isRunning()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void lazilyStartOnLoopResourcesAccess() {
|
||||||
|
assertThat(this.resourceFactory.isRunning()).isFalse();
|
||||||
|
this.resourceFactory.getLoopResources();
|
||||||
|
assertThat(this.resourceFactory.isRunning()).isTrue();
|
||||||
|
this.resourceFactory.stop();
|
||||||
|
assertThat(this.resourceFactory.isRunning()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue