Class CompositeBackPressureHandler
- All Implemented Interfaces:
BackPressureHandler,BatchAwareBackPressureHandler,IdentifiableContainerComponent
BackPressureHandler implementation that delegates the back-pressure handling to a list of
BackPressureHandlers.
This class is used to combine multiple back-pressure handlers into a single one. It allows for more complex back-pressure handling strategies by combining different implementations.
The order in which the back-pressure handlers are registered in the CompositeBackPressureHandler is important
as it will affect the blocking and limiting behaviour of the back-pressure handling.
When request(int amount) is called, the first back-pressure handler in the list is called with
amount as the requested amount of permits. The returned amount of permits (which is less than or equal to the
initial amount) is then passed to the next back-pressure handler in the list. This process of reducing the amount to
request for the next handlers in the chain is called "limiting". This process continues until all back-pressure
handlers have been called or 0 permits has been returned.
Once the final amount of available permits have been computed, unused acquired permits on back-pressure handlers (due to later limiting happening in the chain) are released.
If no permits were obtained, the request(int) method will wait up to noPermitsReturnedWaitTimeout
for a release of permits before returning.
This handler builds on the original SemaphoreBackPressureHandler, separating specific responsibilities into a more modular form and enabling composition with other handlers as part of an extensible backpressure strategy.
- Since:
- 4.0.0
- Author:
- Loïc Rouchon, Tomaz Fernandes
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface io.awspring.cloud.sqs.listener.backpressure.BackPressureHandler
BackPressureHandler.ReleaseReason -
Method Summary
Modifier and TypeMethodDescriptionbuilder()booleanAttempts to acquire all permits up to the specified timeout.getId()Get the component id.voidrelease(int amount, BackPressureHandler.ReleaseReason reason) Releases the specified amount of permits for processed messages.intrequest(int amount) Requests a number of permits.intRequest a batch of permits.voidSet the component id.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.awspring.cloud.sqs.listener.backpressure.BackPressureHandler
releaseMethods inherited from interface io.awspring.cloud.sqs.listener.backpressure.BatchAwareBackPressureHandler
getBatchSize, releaseBatch
-
Method Details
-
setId
Description copied from interface:IdentifiableContainerComponentSet the component id.- Specified by:
setIdin interfaceIdentifiableContainerComponent- Parameters:
id- the id.
-
getId
Description copied from interface:IdentifiableContainerComponentGet the component id.- Specified by:
getIdin interfaceIdentifiableContainerComponent- Returns:
- the id.
-
requestBatch
Description copied from interface:BatchAwareBackPressureHandlerRequest a batch of permits.- Specified by:
requestBatchin interfaceBatchAwareBackPressureHandler- Returns:
- the number of permits acquired.
- Throws:
InterruptedException- if the Thread is interrupted while waiting for permits.
-
request
Description copied from interface:BackPressureHandlerRequests a number of permits. Each obtained permit allows theMessageSourceto retrieve one message.- Specified by:
requestin interfaceBackPressureHandler- Parameters:
amount- the amount of permits to request.- Returns:
- the amount of permits obtained.
- Throws:
InterruptedException- if the Thread is interrupted while waiting for permits.
-
release
Description copied from interface:BackPressureHandlerReleases the specified amount of permits for processed messages. Each message that has been processed should release one permit, whether processing was successful or not.This method can be called in the following use cases:
BackPressureHandler.ReleaseReason.LIMITED: all/some permits were not used because another BackPressureHandler has a lower permits limit and the difference in permits needs to be returned.BackPressureHandler.ReleaseReason.NONE_FETCHED: none of the permits were actually used because no messages were retrieved from SQS. Permits need to be returned.BackPressureHandler.ReleaseReason.PARTIAL_FETCH: some of the permits were used (some messages were retrieved from SQS). The unused ones need to be returned. The amount to be returned might be 0, in which case it means all the permits will be used as the same number of messages were fetched from SQS.BackPressureHandler.ReleaseReason.PROCESSED: a message processing finished, successfully or not.
- Specified by:
releasein interfaceBackPressureHandler- Specified by:
releasein interfaceBatchAwareBackPressureHandler- Parameters:
amount- the amount of permits to release.reason- the reason why the permits were released.
-
drain
Description copied from interface:BackPressureHandlerAttempts to acquire all permits up to the specified timeout. If successful, means all permits were returned and thus no activity is left in theMessageSource.- Specified by:
drainin interfaceBackPressureHandler- Parameters:
timeout- the maximum amount of time to wait for all permits to be released.- Returns:
- whether all permits were acquired.
-
builder
-