Reactor Disposable provides a mechanism for managing resources, subscriptions, or actions in a reactive application. There are three main types of Disposable: Disposable, Disposable.Composite, and Disposable.Swap.
Definition: Represents a single resource that can be disposed of when it's no longer needed, typically used for cleanup purposes.
Real-world usage:
-
In a messaging application, you might use
Disposable
to manage a subscription to a message queue. When a user subscribes to a topic or channel, you create a disposable subscription. When the user unsubscribes or logs out, you can calldispose()
on the subscription to release the resources and stop receiving messages. -
In a file processing system, you could use
Disposable
to manage file streams. When a file is opened for processing, you create a disposable stream. When the processing is complete or an error occurs, you can calldispose()
to close the stream and release system resources.
Definition: Represents a collection of disposables that can be managed as a single unit, allowing for the disposal of multiple resources at once.
Real-world usage:
-
In a financial trading application, you might have multiple subscriptions to market data feeds, trade execution services, and risk management systems. You could use a
Disposable.Composite
to manage all of these subscriptions together. When the trading system is shut down or no longer needed, you can dispose of the composite to release all the associated resources. -
In an IoT (Internet of Things) system, you might have multiple sensor readings or device connections that need to be managed. You could use a
Disposable.Composite
to manage all of these connections together. When the IoT device is turned off or removed from the network, you can dispose of the composite to release all the associated connections.
Definition: Allows for the dynamic replacement of one disposable with another, enabling seamless transitions between different resources or actions.
Real-world usage:
-
In a chat application, you might use
Disposable.Swap
to manage the connection to a chat server. When a user switches chat rooms or channels, you can replace the current chat connection with a new one usingDisposable.Swap
. This allows users to seamlessly switch between different chat rooms without interrupting their conversations. -
In a gaming server, you could use
Disposable.Swap
to manage player sessions. When a player logs in or joins a game, you create a disposable session. When the player logs out or leaves the game, you can replace the current session with a new one usingDisposable.Swap
. This allows for smooth transitions between player sessions without affecting the overall game state.
switchMap
: Automatically switches between Publishers, discarding previous ones, without user intervention for cancellation handling.Disposable.Swap
: Allows manual cancellation handling for seamless replacement of disposables, unlikeswitchMap
.
The UserService
class provided in this gist demonstrates the usage of Disposable.Swap
from Project Reactor. This class is a service for searching and log the users asynchronously and managing the lifecycle of search operations.
When a new search operation is initiated, the searchUsers(String query)
method is called with a search query. Internally, this method performs the search asynchronously using the provided UserRepository
. The result of the search is logged, and any errors encountered during the search are handled gracefully.
The Disposable.Swap
instance named disposableSwap
is used to manage the lifecycle of search operations. Each time a new search is initiated, the previous search operation's Disposable
is replaced with the new one atomically. This ensures that resources associated with the previous search are properly disposed of before starting a new search.
To clean up resources when the service is destroyed, the cleanUp()
method is annotated with @PreDestroy
. This method disposes of the Disposable.Swap
instance if it exists and is not already disposed of, ensuring proper cleanup of resources associated with search operations.