Class ReactorResourceFactory

java.lang.Object
org.springframework.http.client.ReactorResourceFactory
All Implemented Interfaces:
Aware, DisposableBean, InitializingBean, ApplicationContextAware, Lifecycle, Phased, SmartLifecycle
Direct Known Subclasses:
ReactorResourceFactory

public class ReactorResourceFactory extends Object implements ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle
Factory to manage Reactor Netty resources, i.e. LoopResources for event loop threads, and ConnectionProvider for the connection pool, within the lifecycle of a Spring ApplicationContext.

This factory implements SmartLifecycle and is expected typically to be declared as a Spring-managed bean.

Notice that after a SmartLifecycle stop/restart, new instances of the configured LoopResources and ConnectionProvider are created, so any references to those should be updated.

Since:
6.1
Author:
Rossen Stoyanchev, Brian Clozel, Sebastien Deleuze, Juergen Hoeller
  • Field Summary

    Fields inherited from interface org.springframework.context.SmartLifecycle

    DEFAULT_PHASE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addGlobalResourcesConsumer(Consumer<reactor.netty.http.HttpResources> consumer)
    Add a Consumer for configuring the global Reactor Netty resources on startup.
    void
    Starts the resources if initialized outside an ApplicationContext.
    void
    Stops the resources if initialized outside an ApplicationContext.
    reactor.netty.resources.ConnectionProvider
    Return the configured ConnectionProvider.
    reactor.netty.resources.LoopResources
    Return the configured LoopResources.
    int
    Return the phase that this lifecycle object is supposed to run in.
    boolean
    Check whether this component is currently running.
    boolean
    Whether this factory exposes the global HttpResources holder.
    void
    Setting an ApplicationContext is optional: If set, Reactor resources will be initialized in the lifecycle start phase and closed in the lifecycle stop phase.
    void
    setConnectionProvider(reactor.netty.resources.ConnectionProvider connectionProvider)
    Use this when you want to provide an externally managed ConnectionProvider instance.
    void
    setConnectionProviderSupplier(Supplier<reactor.netty.resources.ConnectionProvider> supplier)
    Use this when you don't want to participate in global resources and you want to customize the creation of the managed ConnectionProvider.
    void
    setLoopResources(reactor.netty.resources.LoopResources loopResources)
    Use this option when you want to provide an externally managed LoopResources instance.
    void
    setLoopResourcesSupplier(Supplier<reactor.netty.resources.LoopResources> supplier)
    Use this when you don't want to participate in global resources and you want to customize the creation of the managed LoopResources.
    void
    setShutdownQuietPeriod(Duration shutdownQuietPeriod)
    Configure the amount of time we'll wait before shutting down resources.
    void
    setShutdownTimeout(Duration shutdownTimeout)
    Configure the maximum amount of time to wait until the disposal of the underlying resources regardless if a task was submitted during the shutdownQuietPeriod.
    void
    setUseGlobalResources(boolean useGlobalResources)
    Whether to use global Reactor Netty resources via HttpResources.
    void
    Start this component.
    void
    Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.context.SmartLifecycle

    isAutoStartup, stop
  • Constructor Details

    • ReactorResourceFactory

      public ReactorResourceFactory()
  • Method Details

    • setUseGlobalResources

      public void setUseGlobalResources(boolean useGlobalResources)
      Whether to use global Reactor Netty resources via HttpResources.

      Default is "true" in which case this factory initializes and stops the global Reactor Netty resources within Spring's ApplicationContext lifecycle. If set to "false" the factory manages its resources independent of the global ones.

      Parameters:
      useGlobalResources - whether to expose and manage the global resources
      See Also:
    • isUseGlobalResources

      public boolean isUseGlobalResources()
      Whether this factory exposes the global HttpResources holder.
    • addGlobalResourcesConsumer

      public void addGlobalResourcesConsumer(Consumer<reactor.netty.http.HttpResources> consumer)
      Add a Consumer for configuring the global Reactor Netty resources on startup. When this option is used, setUseGlobalResources(boolean) is also enabled.
      Parameters:
      consumer - the consumer to apply
      See Also:
    • setConnectionProviderSupplier

      public void setConnectionProviderSupplier(Supplier<reactor.netty.resources.ConnectionProvider> supplier)
      Use this when you don't want to participate in global resources and you want to customize the creation of the managed ConnectionProvider.

      By default, ConnectionProvider.elastic("http") is used.

      Note that this supplier is ignored if isUseGlobalResources() is true or once the ConnectionProvider is set.

      Parameters:
      supplier - the supplier to use
    • setConnectionProvider

      public void setConnectionProvider(reactor.netty.resources.ConnectionProvider connectionProvider)
      Use this when you want to provide an externally managed ConnectionProvider instance.
      Parameters:
      connectionProvider - the connection provider to use as is
    • getConnectionProvider

      public reactor.netty.resources.ConnectionProvider getConnectionProvider()
      Return the configured ConnectionProvider.

      Lazily tries to start the resources on demand if not initialized yet.

      See Also:
    • setLoopResourcesSupplier

      public void setLoopResourcesSupplier(Supplier<reactor.netty.resources.LoopResources> supplier)
      Use this when you don't want to participate in global resources and you want to customize the creation of the managed LoopResources.

      By default, LoopResources.create("webflux-http") is used.

      Note that this supplier is ignored if isUseGlobalResources() is true or once the LoopResources is set.

      Parameters:
      supplier - the supplier to use
    • setLoopResources

      public void setLoopResources(reactor.netty.resources.LoopResources loopResources)
      Use this option when you want to provide an externally managed LoopResources instance.
      Parameters:
      loopResources - the loop resources to use as is
    • getLoopResources

      public reactor.netty.resources.LoopResources getLoopResources()
      Return the configured LoopResources.

      Lazily tries to start the resources on demand if not initialized yet.

      See Also:
    • setShutdownQuietPeriod

      public void setShutdownQuietPeriod(Duration shutdownQuietPeriod)
      Configure the amount of time we'll wait before shutting down resources. If a task is submitted during the shutdownQuietPeriod, it is guaranteed to be accepted and the shutdownQuietPeriod will start over.

      By default, this is set to LoopResources.DEFAULT_SHUTDOWN_QUIET_PERIOD which is 2 seconds but can also be overridden with the system property ReactorNetty.SHUTDOWN_QUIET_PERIOD.

      See Also:
    • setShutdownTimeout

      public void setShutdownTimeout(Duration shutdownTimeout)
      Configure the maximum amount of time to wait until the disposal of the underlying resources regardless if a task was submitted during the shutdownQuietPeriod.

      By default, this is set to LoopResources.DEFAULT_SHUTDOWN_TIMEOUT which is 15 seconds but can also be overridden with the system property ReactorNetty.SHUTDOWN_TIMEOUT.

      See Also:
    • setApplicationContext

      public void setApplicationContext(ApplicationContext applicationContext)
      Setting an ApplicationContext is optional: If set, Reactor resources will be initialized in the lifecycle start phase and closed in the lifecycle stop phase. If not set, it will happen in afterPropertiesSet() and destroy(), respectively.
      Specified by:
      setApplicationContext in interface ApplicationContextAware
      Parameters:
      applicationContext - the ApplicationContext object to be used by this object
      See Also:
    • afterPropertiesSet

      public void afterPropertiesSet()
      Starts the resources if initialized outside an ApplicationContext. This is for backwards compatibility; the preferred way is to rely on the ApplicationContext's lifecycle management.
      Specified by:
      afterPropertiesSet in interface InitializingBean
      See Also:
    • destroy

      public void destroy()
      Stops the resources if initialized outside an ApplicationContext. This is for backwards compatibility; the preferred way is to rely on the ApplicationContext's lifecycle management.
      Specified by:
      destroy in interface DisposableBean
      See Also:
    • start

      public void start()
      Description copied from interface: Lifecycle
      Start this component.

      Should not throw an exception if the component is already running.

      In the case of a container, this will propagate the start signal to all components that apply.

      Specified by:
      start in interface Lifecycle
      See Also:
    • stop

      public void stop()
      Description copied from interface: Lifecycle
      Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing SmartLifecycle and its stop(Runnable) variant when asynchronous stop behavior is necessary.

      Note that this stop notification is not guaranteed to come before destruction: On regular shutdown, Lifecycle beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.

      Should not throw an exception if the component is not running (not started yet).

      In the case of a container, this will propagate the stop signal to all components that apply.

      Specified by:
      stop in interface Lifecycle
      See Also:
    • isRunning

      public boolean isRunning()
      Description copied from interface: Lifecycle
      Check whether this component is currently running.

      In the case of a container, this will return true only if all components that apply are currently running.

      Specified by:
      isRunning in interface Lifecycle
      Returns:
      whether the component is currently running
    • getPhase

      public int getPhase()
      Description copied from interface: SmartLifecycle
      Return the phase that this lifecycle object is supposed to run in.

      The default implementation returns SmartLifecycle.DEFAULT_PHASE in order to let stop() callbacks execute before regular Lifecycle implementations.

      Specified by:
      getPhase in interface Phased
      Specified by:
      getPhase in interface SmartLifecycle
      See Also: