Class AbstractMessageChannelBinder<C extends ConsumerProperties,P extends ProducerProperties,PP extends ProvisioningProvider<C,P>>
- java.lang.Object
-
- org.springframework.cloud.stream.binder.AbstractBinder<org.springframework.messaging.MessageChannel,C,P>
-
- org.springframework.cloud.stream.binder.AbstractMessageChannelBinder<C,P,PP>
-
- Type Parameters:
C
- the consumer properties typeP
- the producer properties typePP
- the provisioning producer properties type
- All Implemented Interfaces:
org.springframework.beans.factory.Aware
,org.springframework.beans.factory.InitializingBean
,Binder<org.springframework.messaging.MessageChannel,C,P>
,PollableConsumerBinder<org.springframework.messaging.MessageHandler,C>
,org.springframework.context.ApplicationContextAware
,org.springframework.context.ApplicationEventPublisherAware
public abstract class AbstractMessageChannelBinder<C extends ConsumerProperties,P extends ProducerProperties,PP extends ProvisioningProvider<C,P>> extends AbstractBinder<org.springframework.messaging.MessageChannel,C,P> implements PollableConsumerBinder<org.springframework.messaging.MessageHandler,C>, org.springframework.context.ApplicationEventPublisherAware
AbstractBinder
that serves as base class forMessageChannel
binders. Implementors must implement the following methods:- Since:
- 1.1
- Author:
- Marius Bogoevici, Ilayaperumal Gopinathan, Soby Chacko, Oleg Zhurakousky, Artem Bilan, Gary Russell
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractMessageChannelBinder.ErrorInfrastructure
protected static class
AbstractMessageChannelBinder.PolledConsumerResources
-
Field Summary
Fields Modifier and Type Field Description protected PP
provisioningProvider
ProvisioningProvider
delegated by the downstream binder implementations.-
Fields inherited from class org.springframework.cloud.stream.binder.AbstractBinder
logger
-
-
Constructor Summary
Constructors Constructor Description AbstractMessageChannelBinder(String[] headersToEmbed, PP provisioningProvider)
AbstractMessageChannelBinder(String[] headersToEmbed, PP provisioningProvider, ListenerContainerCustomizer<?> containerCustomizer, MessageSourceCustomizer<?> sourceCustomizer)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
afterUnbindConsumer(ConsumerDestination destination, String group, C consumerProperties)
Invoked after the unbinding of a consumer.protected void
afterUnbindProducer(ProducerDestination destination, P producerProperties)
Invoked after the unbinding of a producer.Binding<PollableSource<org.springframework.messaging.MessageHandler>>
bindPollableConsumer(String name, String group, PollableSource<org.springframework.messaging.MessageHandler> inboundBindTarget, C properties)
Configure a binding for a pollable message source.protected abstract org.springframework.integration.core.MessageProducer
createConsumerEndpoint(ConsumerDestination destination, String group, C properties)
CreatesMessageProducer
that receives data from the consumer destination.protected AbstractMessageChannelBinder.PolledConsumerResources
createPolledConsumerResources(String name, String group, ConsumerDestination destination, C consumerProperties)
protected abstract org.springframework.messaging.MessageHandler
createProducerMessageHandler(ProducerDestination destination, P producerProperties, org.springframework.messaging.MessageChannel errorChannel)
Create aMessageHandler
with the ability to send data to the target middleware.protected org.springframework.messaging.MessageHandler
createProducerMessageHandler(ProducerDestination destination, P producerProperties, org.springframework.messaging.MessageChannel channel, org.springframework.messaging.MessageChannel errorChannel)
Create aMessageHandler
with the ability to send data to the target middleware.Binding<org.springframework.messaging.MessageChannel>
doBindConsumer(String name, String group, org.springframework.messaging.MessageChannel inputChannel, C properties)
Binds an inbound channel to a given destination.Binding<org.springframework.messaging.MessageChannel>
doBindProducer(String destination, org.springframework.messaging.MessageChannel outputChannel, P producerProperties)
Binds an outbound channel to a given destination.protected String
errorsBaseName(ConsumerDestination destination, String group, C consumerProperties)
protected String
errorsBaseName(ProducerDestination destination)
protected org.springframework.context.ApplicationEventPublisher
getApplicationEventPublisher()
protected <L> ListenerContainerCustomizer<L>
getContainerCustomizer()
protected org.springframework.messaging.MessageHandler
getDefaultErrorMessageHandler(org.springframework.cloud.stream.binder.LastSubscriberAwareChannel errorChannel, boolean defaultErrorChannelPresent)
Return the default error message handler, which throws the error message payload to the caller if there are no user handlers subscribed.protected String
getErrorBridgeName(ConsumerDestination destination, String group, C consumerProperties)
protected String
getErrorBridgeName(ProducerDestination destination)
protected org.springframework.messaging.MessageHandler
getErrorMessageHandler(ConsumerDestination destination, String group, C consumerProperties)
Binders can return a message handler to be subscribed to the error channel.protected String
getErrorMessageHandlerName(ConsumerDestination destination, String group, C consumerProperties)
protected org.springframework.integration.support.ErrorMessageStrategy
getErrorMessageStrategy()
Binders can return anErrorMessageStrategy
for building error messages; binder implementations typically might add extra headers to the error message.protected String
getErrorRecovererName(ConsumerDestination destination, String group, C consumerProperties)
protected <S> MessageSourceCustomizer<S>
getMessageSourceCustomizer()
protected org.springframework.messaging.MessageHandler
getPolledConsumerErrorMessageHandler(ConsumerDestination destination, String group, C consumerProperties)
Binders can return a message handler to be subscribed to the error channel.protected org.springframework.retry.RecoveryCallback<Object>
getPolledConsumerRecoveryCallback(AbstractMessageChannelBinder.ErrorInfrastructure errorInfrastructure, C properties)
Implementations can override the defaultErrorMessageSendingRecoverer
.protected void
onInit()
Subclasses may implement this method to perform any necessary initialization.protected void
postProcessOutputChannel(org.springframework.messaging.MessageChannel outputChannel, P producerProperties)
Allows subclasses to perform post processing on the channel - for example to add more interceptors.protected void
postProcessPollableSource(DefaultPollableMessageSource bindingTarget)
protected AbstractMessageChannelBinder.ErrorInfrastructure
registerErrorInfrastructure(ConsumerDestination destination, String group, C consumerProperties)
Build an errorChannelRecoverer that writes to a pub/sub channel for the destination when an exception is thrown to a consumer.protected AbstractMessageChannelBinder.ErrorInfrastructure
registerErrorInfrastructure(ConsumerDestination destination, String group, C consumerProperties, boolean polled)
Build an errorChannelRecoverer that writes to a pub/sub channel for the destination when an exception is thrown to a consumer.void
setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
void
setConsumerEndpointCustomizer(ConsumerEndpointCustomizer<? extends org.springframework.integration.core.MessageProducer> endpointCustomizer)
Configure an optionalConsumerEndpointCustomizer
for further configuration of consumerMessageProducer
instances created by the binder.void
setProducerMessageHandlerCustomizer(ProducerMessageHandlerCustomizer<? extends org.springframework.messaging.MessageHandler> handlerCustomizer)
Configure an optionalProducerMessageHandlerCustomizer
for further configuration of producerMessageHandler
instances created by the binder.protected boolean
useNativeEncoding(P producerProperties)
Whether the producer for the destination being created should be configured to use native encoding which may, or may not, be determined from the properties.-
Methods inherited from class org.springframework.cloud.stream.binder.AbstractBinder
afterPropertiesSet, applyPrefix, bindConsumer, bindProducer, buildRetryTemplate, constructDLQName, getApplicationContext, getBeanFactory, getEvaluationContext, groupedName, setApplicationContext
-
-
-
-
Field Detail
-
provisioningProvider
protected final PP extends ProvisioningProvider<C,P> provisioningProvider
ProvisioningProvider
delegated by the downstream binder implementations.
-
-
Constructor Detail
-
AbstractMessageChannelBinder
public AbstractMessageChannelBinder(String[] headersToEmbed, PP provisioningProvider)
-
AbstractMessageChannelBinder
public AbstractMessageChannelBinder(String[] headersToEmbed, PP provisioningProvider, @Nullable ListenerContainerCustomizer<?> containerCustomizer, @Nullable MessageSourceCustomizer<?> sourceCustomizer)
-
-
Method Detail
-
onInit
protected void onInit() throws Exception
Description copied from class:AbstractBinder
Subclasses may implement this method to perform any necessary initialization. It will be invoked fromAbstractBinder.afterPropertiesSet()
which is itselffinal
.- Overrides:
onInit
in classAbstractBinder<org.springframework.messaging.MessageChannel,C extends ConsumerProperties,P extends ProducerProperties>
- Throws:
Exception
- when init fails
-
getApplicationEventPublisher
protected org.springframework.context.ApplicationEventPublisher getApplicationEventPublisher()
-
setApplicationEventPublisher
public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
- Specified by:
setApplicationEventPublisher
in interfaceorg.springframework.context.ApplicationEventPublisherAware
-
setProducerMessageHandlerCustomizer
public void setProducerMessageHandlerCustomizer(@Nullable ProducerMessageHandlerCustomizer<? extends org.springframework.messaging.MessageHandler> handlerCustomizer)
Configure an optionalProducerMessageHandlerCustomizer
for further configuration of producerMessageHandler
instances created by the binder.- Parameters:
handlerCustomizer
- theProducerMessageHandlerCustomizer
to use.- Since:
- 3.0
-
setConsumerEndpointCustomizer
public void setConsumerEndpointCustomizer(@Nullable ConsumerEndpointCustomizer<? extends org.springframework.integration.core.MessageProducer> endpointCustomizer)
Configure an optionalConsumerEndpointCustomizer
for further configuration of consumerMessageProducer
instances created by the binder.- Parameters:
endpointCustomizer
- theConsumerEndpointCustomizer
to use.- Since:
- 3.0
-
getContainerCustomizer
protected <L> ListenerContainerCustomizer<L> getContainerCustomizer()
-
getMessageSourceCustomizer
protected <S> MessageSourceCustomizer<S> getMessageSourceCustomizer()
-
doBindProducer
public final Binding<org.springframework.messaging.MessageChannel> doBindProducer(String destination, org.springframework.messaging.MessageChannel outputChannel, P producerProperties) throws BinderException
Binds an outbound channel to a given destination. The implementation delegates toProvisioningProvider.provisionProducerDestination(String, ProducerProperties)
andcreateProducerMessageHandler(ProducerDestination, ProducerProperties, MessageChannel)
for handling the middleware specific logic. If the returned producer message handler is anInitializingBean
thenInitializingBean.afterPropertiesSet()
will be called on it. Similarly, if the returned producer message handler endpoint is aLifecycle
, thenLifecycle.start()
will be called on it.- Specified by:
doBindProducer
in classAbstractBinder<org.springframework.messaging.MessageChannel,C extends ConsumerProperties,P extends ProducerProperties>
- Parameters:
destination
- the name of the destinationoutputChannel
- the channel to be boundproducerProperties
- theProducerProperties
of the binding- Returns:
- the Binding for the channel
- Throws:
BinderException
- on internal errors during binding
-
useNativeEncoding
protected boolean useNativeEncoding(P producerProperties)
Whether the producer for the destination being created should be configured to use native encoding which may, or may not, be determined from the properties. For example, a transactional kafka binder uses a common producer for all destinations. The default implementation returnsProducerProperties.isUseNativeEncoding()
.- Parameters:
producerProperties
- the properties.- Returns:
- true to use native encoding.
-
postProcessOutputChannel
protected void postProcessOutputChannel(org.springframework.messaging.MessageChannel outputChannel, P producerProperties)
Allows subclasses to perform post processing on the channel - for example to add more interceptors.- Parameters:
outputChannel
- the channel.producerProperties
- the producer properties.
-
createProducerMessageHandler
protected org.springframework.messaging.MessageHandler createProducerMessageHandler(ProducerDestination destination, P producerProperties, org.springframework.messaging.MessageChannel channel, org.springframework.messaging.MessageChannel errorChannel) throws Exception
Create aMessageHandler
with the ability to send data to the target middleware. If the returned instance is also aLifecycle
, it will be stopped automatically by the binder.In order to be fully compliant, the
MessageHandler
of the binder must observe the following headers:BinderHeaders.PARTITION_HEADER
- indicates the target partition where the message must be sent
- Parameters:
destination
- the name of the target destination.producerProperties
- the producer properties.channel
- the channel to bind.errorChannel
- the error channel (if enabled, otherwise null). If not null, the binder must wire this channel into the producer endpoint so that errors are forwarded to it.- Returns:
- the message handler for sending data to the target middleware
- Throws:
Exception
- when producer messsage handler failed to be created
-
createProducerMessageHandler
protected abstract org.springframework.messaging.MessageHandler createProducerMessageHandler(ProducerDestination destination, P producerProperties, org.springframework.messaging.MessageChannel errorChannel) throws Exception
Create aMessageHandler
with the ability to send data to the target middleware. If the returned instance is also aLifecycle
, it will be stopped automatically by the binder.In order to be fully compliant, the
MessageHandler
of the binder must observe the following headers:BinderHeaders.PARTITION_HEADER
- indicates the target partition where the message must be sent
- Parameters:
destination
- the name of the target destinationproducerProperties
- the producer propertieserrorChannel
- the error channel (if enabled, otherwise null). If not null, the binder must wire this channel into the producer endpoint so that errors are forwarded to it.- Returns:
- the message handler for sending data to the target middleware
- Throws:
Exception
- upon failure to create the producer message handler
-
afterUnbindProducer
protected void afterUnbindProducer(ProducerDestination destination, P producerProperties)
Invoked after the unbinding of a producer. Subclasses may override this to provide their own logic for dealing with unbinding.- Parameters:
destination
- the bound destinationproducerProperties
- the producer properties
-
doBindConsumer
public final Binding<org.springframework.messaging.MessageChannel> doBindConsumer(String name, String group, org.springframework.messaging.MessageChannel inputChannel, C properties) throws BinderException
Binds an inbound channel to a given destination. The implementation delegates toProvisioningProvider.provisionConsumerDestination(String, String, ConsumerProperties)
andcreateConsumerEndpoint(ConsumerDestination, String, ConsumerProperties)
for handling middleware-specific logic. If the returned consumer endpoint is anInitializingBean
thenInitializingBean.afterPropertiesSet()
will be called on it. Similarly, if the returned consumer endpoint is aLifecycle
, thenLifecycle.start()
will be called on it.- Specified by:
doBindConsumer
in classAbstractBinder<org.springframework.messaging.MessageChannel,C extends ConsumerProperties,P extends ProducerProperties>
- Parameters:
name
- the name of the destinationgroup
- the consumer groupinputChannel
- the channel to be boundproperties
- theConsumerProperties
of the binding- Returns:
- the Binding for the channel
- Throws:
BinderException
- on internal errors during binding
-
bindPollableConsumer
public Binding<PollableSource<org.springframework.messaging.MessageHandler>> bindPollableConsumer(String name, String group, PollableSource<org.springframework.messaging.MessageHandler> inboundBindTarget, C properties)
Description copied from interface:PollableConsumerBinder
Configure a binding for a pollable message source.- Specified by:
bindPollableConsumer
in interfacePollableConsumerBinder<C extends ConsumerProperties,P extends ProducerProperties>
- Parameters:
name
- the binding name.group
- the consumer group.inboundBindTarget
- the binding target.properties
- the consumer properties.- Returns:
- the binding.
-
postProcessPollableSource
protected void postProcessPollableSource(DefaultPollableMessageSource bindingTarget)
-
getPolledConsumerRecoveryCallback
protected org.springframework.retry.RecoveryCallback<Object> getPolledConsumerRecoveryCallback(AbstractMessageChannelBinder.ErrorInfrastructure errorInfrastructure, C properties)
Implementations can override the defaultErrorMessageSendingRecoverer
.- Parameters:
errorInfrastructure
- the infrastructure.properties
- the consumer properties.- Returns:
- the recoverer.
-
createPolledConsumerResources
protected AbstractMessageChannelBinder.PolledConsumerResources createPolledConsumerResources(String name, String group, ConsumerDestination destination, C consumerProperties)
-
createConsumerEndpoint
protected abstract org.springframework.integration.core.MessageProducer createConsumerEndpoint(ConsumerDestination destination, String group, C properties) throws Exception
CreatesMessageProducer
that receives data from the consumer destination. will be started and stopped by the binder.- Parameters:
group
- the consumer groupdestination
- reference to the consumer destinationproperties
- the consumer properties- Returns:
- the consumer endpoint.
- Throws:
Exception
- when consumer endpoint creation failed.
-
afterUnbindConsumer
protected void afterUnbindConsumer(ConsumerDestination destination, String group, C consumerProperties)
Invoked after the unbinding of a consumer. The binder implementation can override this method to provide their own logic (e.g. for cleaning up destinations).- Parameters:
destination
- the consumer destinationgroup
- the consumer groupconsumerProperties
- the consumer properties
-
registerErrorInfrastructure
protected final AbstractMessageChannelBinder.ErrorInfrastructure registerErrorInfrastructure(ConsumerDestination destination, String group, C consumerProperties)
Build an errorChannelRecoverer that writes to a pub/sub channel for the destination when an exception is thrown to a consumer.- Parameters:
destination
- the destination.group
- the group.consumerProperties
- the properties.- Returns:
- the ErrorInfrastructure which is a holder for the error channel, the recoverer and the message handler that is subscribed to the channel.
-
registerErrorInfrastructure
protected final AbstractMessageChannelBinder.ErrorInfrastructure registerErrorInfrastructure(ConsumerDestination destination, String group, C consumerProperties, boolean polled)
Build an errorChannelRecoverer that writes to a pub/sub channel for the destination when an exception is thrown to a consumer.- Parameters:
destination
- the destination.group
- the group.consumerProperties
- the properties.polled
- true if this is for a polled consumer.- Returns:
- the ErrorInfrastructure which is a holder for the error channel, the recoverer and the message handler that is subscribed to the channel.
-
getErrorMessageHandler
protected org.springframework.messaging.MessageHandler getErrorMessageHandler(ConsumerDestination destination, String group, C consumerProperties)
Binders can return a message handler to be subscribed to the error channel. Examples might be if the user wishes to (re)publish messages to a DLQ.- Parameters:
destination
- the destination.group
- the group.consumerProperties
- the properties.- Returns:
- the handler (may be null, which is the default, causing the exception to be rethrown).
-
getPolledConsumerErrorMessageHandler
protected org.springframework.messaging.MessageHandler getPolledConsumerErrorMessageHandler(ConsumerDestination destination, String group, C consumerProperties)
Binders can return a message handler to be subscribed to the error channel. Examples might be if the user wishes to (re)publish messages to a DLQ.- Parameters:
destination
- the destination.group
- the group.consumerProperties
- the properties.- Returns:
- the handler (may be null, which is the default, causing the exception to be rethrown).
-
getDefaultErrorMessageHandler
protected org.springframework.messaging.MessageHandler getDefaultErrorMessageHandler(org.springframework.cloud.stream.binder.LastSubscriberAwareChannel errorChannel, boolean defaultErrorChannelPresent)
Return the default error message handler, which throws the error message payload to the caller if there are no user handlers subscribed. The handler is ordered so it runs after any user-defined handlers that are subscribed.- Parameters:
errorChannel
- the error channel.defaultErrorChannelPresent
- true if the context has a default 'errorChannel'.- Returns:
- the handler.
-
getErrorMessageStrategy
protected org.springframework.integration.support.ErrorMessageStrategy getErrorMessageStrategy()
Binders can return anErrorMessageStrategy
for building error messages; binder implementations typically might add extra headers to the error message.- Returns:
- the implementation - may be null.
-
getErrorRecovererName
protected String getErrorRecovererName(ConsumerDestination destination, String group, C consumerProperties)
-
getErrorMessageHandlerName
protected String getErrorMessageHandlerName(ConsumerDestination destination, String group, C consumerProperties)
-
getErrorBridgeName
protected String getErrorBridgeName(ConsumerDestination destination, String group, C consumerProperties)
-
errorsBaseName
protected String errorsBaseName(ConsumerDestination destination, String group, C consumerProperties)
-
getErrorBridgeName
protected String getErrorBridgeName(ProducerDestination destination)
-
errorsBaseName
protected String errorsBaseName(ProducerDestination destination)
-
-