Class MessagingMessageListenerAdapter<K,V>
java.lang.Object
org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter<K,V>
- Type Parameters:
K
- the key type.V
- the value type.
- All Implemented Interfaces:
ConsumerSeekAware
- Direct Known Subclasses:
BatchMessagingMessageListenerAdapter
,RecordMessagingMessageListenerAdapter
public abstract class MessagingMessageListenerAdapter<K,V>
extends Object
implements ConsumerSeekAware
An abstract
MessageListener
adapter
providing the necessary infrastructure to extract the payload of a
Message
.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
Root object for reply expression evaluation.Nested classes/interfaces inherited from interface org.springframework.kafka.listener.ConsumerSeekAware
ConsumerSeekAware.ConsumerSeekCallback
-
Field Summary
Modifier and TypeFieldDescriptionprotected final org.springframework.core.log.LogAccessor
protected static final org.springframework.messaging.Message<KafkaNull>
Message used when no conversion is needed. -
Constructor Summary
ModifierConstructorDescriptionprotected
MessagingMessageListenerAdapter
(Object bean, Method method) Create an instance with the provided bean and method. -
Method Summary
Modifier and TypeMethodDescriptionprotected final String
createMessagingErrorMessage
(String description, Object payload) protected Type
determineInferredType
(Method method) Subclasses can override this method to use a different mechanism to determine the target type of the payload conversion.protected final RecordMessageConverter
Return theMessagingMessageConverter
for this listener, being able to convertMessage
.protected ReplyHeadersConfigurer
Return the reply configurer.protected Type
getType()
Returns the inferred type for conversion or, if null, thefallbackType
.protected void
handleResult
(Object resultArg, Object request, Object source) Handle the given result object returned from the listener method, sending a response message to the SendTo topic.protected final Object
invokeHandler
(Object data, Acknowledgment acknowledgment, org.springframework.messaging.Message<?> message, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer) Invoke the handler, wrapping any exception to aListenerExecutionFailedException
with a dedicated error message.protected boolean
boolean
boolean
protected boolean
protected boolean
When true,Iterable
return results will be split into discrete records.void
onIdleContainer
(Map<org.apache.kafka.common.TopicPartition, Long> assignments, ConsumerSeekAware.ConsumerSeekCallback callback) If the container is configured to emit idle container events, this method is called when the container idle event is emitted - allowing a seek operation.void
onPartitionsAssigned
(Map<org.apache.kafka.common.TopicPartition, Long> assignments, ConsumerSeekAware.ConsumerSeekCallback callback) When using group management, called when partition assignments change.void
onPartitionsRevoked
(Collection<org.apache.kafka.common.TopicPartition> partitions) When using group management, called when partition assignments are revoked.void
Register the callback to use when seeking at some arbitrary time.protected void
sendResponse
(Object result, String topic, Object source, boolean returnTypeMessage) Send the result to the topic.void
setBeanResolver
(org.springframework.expression.BeanResolver beanResolver) Set a bean resolver for runtime SpEL expressions.void
setCorrelationHeaderName
(String correlationHeaderName) Set a custom header name for the correlation id.void
setFallbackType
(Class<?> fallbackType) Set a fallback type to use when using a type-aware message converter and this adapter cannot determine the inferred type from the method.void
setHandlerMethod
(HandlerAdapter handlerMethod) Set theHandlerAdapter
to use to invoke the method processing an incomingConsumerRecord
.void
setMessageConverter
(RecordMessageConverter messageConverter) Set the MessageConverter.void
setMessagingConverter
(org.springframework.messaging.converter.SmartMessageConverter messageConverter) Set theSmartMessageConverter
to use with the defaultMessagingMessageConverter
.void
setReplyHeadersConfigurer
(ReplyHeadersConfigurer replyHeadersConfigurer) Set a configurer which will be invoked when creating a reply message.void
setReplyTemplate
(KafkaTemplate<?, ?> replyTemplate) Set the template to use to send any result from the method invocation.void
setReplyTopic
(String replyTopicParam) Set the topic to which to send any result from the method invocation.void
setSplitIterables
(boolean splitIterables) Set to false to disable splittingIterable
reply values into separate records.protected org.springframework.messaging.Message<?>
toMessagingMessage
(org.apache.kafka.clients.consumer.ConsumerRecord<K, V> cRecord, Acknowledgment acknowledgment, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.kafka.listener.ConsumerSeekAware
onFirstPoll, unregisterSeekCallback
-
Field Details
-
NULL_MESSAGE
Message used when no conversion is needed. -
logger
protected final org.springframework.core.log.LogAccessor logger
-
-
Constructor Details
-
MessagingMessageListenerAdapter
Create an instance with the provided bean and method.- Parameters:
bean
- the bean.method
- the method.
-
-
Method Details
-
setCorrelationHeaderName
Set a custom header name for the correlation id. DefaultKafkaHeaders.CORRELATION_ID
. This header will be echoed back in any reply message.- Parameters:
correlationHeaderName
- the header name.- Since:
- 3.0
-
setMessageConverter
Set the MessageConverter.- Parameters:
messageConverter
- the converter.
-
getMessageConverter
Return theMessagingMessageConverter
for this listener, being able to convertMessage
.- Returns:
- the
MessagingMessageConverter
for this listener, being able to convertMessage
.
-
setMessagingConverter
public void setMessagingConverter(org.springframework.messaging.converter.SmartMessageConverter messageConverter) Set theSmartMessageConverter
to use with the defaultMessagingMessageConverter
. Not allowed when a custommessageConverter
is provided.- Parameters:
messageConverter
- the converter.- Since:
- 2.7.1
-
getType
Returns the inferred type for conversion or, if null, thefallbackType
.- Returns:
- the type.
-
setFallbackType
Set a fallback type to use when using a type-aware message converter and this adapter cannot determine the inferred type from the method. An example of a type-aware message converter is theStringJsonMessageConverter
. Defaults toObject
.- Parameters:
fallbackType
- the type.
-
setHandlerMethod
Set theHandlerAdapter
to use to invoke the method processing an incomingConsumerRecord
.- Parameters:
handlerMethod
-HandlerAdapter
instance.
-
isConsumerRecordList
protected boolean isConsumerRecordList() -
isConsumerRecords
public boolean isConsumerRecords() -
isConversionNeeded
public boolean isConversionNeeded() -
setReplyTopic
Set the topic to which to send any result from the method invocation. May be a SpEL expression!{...}
evaluated at runtime.- Parameters:
replyTopicParam
- the topic or expression.- Since:
- 2.0
-
setReplyTemplate
Set the template to use to send any result from the method invocation.- Parameters:
replyTemplate
- the template.- Since:
- 2.0
-
setBeanResolver
public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver) Set a bean resolver for runtime SpEL expressions. Also configures the evaluation context with a standard type converter and map accessor.- Parameters:
beanResolver
- the resolver.- Since:
- 2.0
-
isMessageList
protected boolean isMessageList() -
getReplyHeadersConfigurer
Return the reply configurer.- Returns:
- the configurer.
- Since:
- 2.2
- See Also:
-
setReplyHeadersConfigurer
Set a configurer which will be invoked when creating a reply message.- Parameters:
replyHeadersConfigurer
- the configurer.- Since:
- 2.2
-
isSplitIterables
protected boolean isSplitIterables()When true,Iterable
return results will be split into discrete records.- Returns:
- true to split.
- Since:
- 2.3.5
-
setSplitIterables
public void setSplitIterables(boolean splitIterables) Set to false to disable splittingIterable
reply values into separate records.- Parameters:
splitIterables
- false to disable; default true.- Since:
- 2.3.5
-
registerSeekCallback
Description copied from interface:ConsumerSeekAware
Register the callback to use when seeking at some arbitrary time. When used with aConcurrentMessageListenerContainer
or the same listener instance in multiple containers listeners should store the callback in aThreadLocal
.- Specified by:
registerSeekCallback
in interfaceConsumerSeekAware
- Parameters:
callback
- the callback.
-
onPartitionsAssigned
public void onPartitionsAssigned(Map<org.apache.kafka.common.TopicPartition, Long> assignments, ConsumerSeekAware.ConsumerSeekCallback callback) Description copied from interface:ConsumerSeekAware
When using group management, called when partition assignments change.- Specified by:
onPartitionsAssigned
in interfaceConsumerSeekAware
- Parameters:
assignments
- the new assignments and their current offsets.callback
- the callback to perform an initial seek after assignment.
-
onPartitionsRevoked
Description copied from interface:ConsumerSeekAware
When using group management, called when partition assignments are revoked. Listeners should discard any callback saved fromConsumerSeekAware.registerSeekCallback(ConsumerSeekCallback)
on this thread.- Specified by:
onPartitionsRevoked
in interfaceConsumerSeekAware
- Parameters:
partitions
- the partitions that have been revoked.
-
onIdleContainer
public void onIdleContainer(Map<org.apache.kafka.common.TopicPartition, Long> assignments, ConsumerSeekAware.ConsumerSeekCallback callback) Description copied from interface:ConsumerSeekAware
If the container is configured to emit idle container events, this method is called when the container idle event is emitted - allowing a seek operation.- Specified by:
onIdleContainer
in interfaceConsumerSeekAware
- Parameters:
assignments
- the new assignments and their current offsets.callback
- the callback to perform a seek.
-
toMessagingMessage
protected org.springframework.messaging.Message<?> toMessagingMessage(org.apache.kafka.clients.consumer.ConsumerRecord<K, V> cRecord, @Nullable Acknowledgment acknowledgment, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer) -
invokeHandler
protected final Object invokeHandler(Object data, @Nullable Acknowledgment acknowledgment, org.springframework.messaging.Message<?> message, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer) Invoke the handler, wrapping any exception to aListenerExecutionFailedException
with a dedicated error message.- Parameters:
data
- the data to process during invocation.acknowledgment
- the acknowledgment to use if any.message
- the message to process.consumer
- the consumer.- Returns:
- the result of invocation.
-
handleResult
Handle the given result object returned from the listener method, sending a response message to the SendTo topic.- Parameters:
resultArg
- the result object to handle (nevernull
)request
- the original request messagesource
- the source data for the method invocation - e.g.o.s.messaging.Message<?>
; may be null
-
sendResponse
protected void sendResponse(Object result, String topic, @Nullable Object source, boolean returnTypeMessage) Send the result to the topic.- Parameters:
result
- the result.topic
- the topic.source
- the source (input).returnTypeMessage
- true if we are returning message(s).- Since:
- 2.1.3
-
createMessagingErrorMessage
-
determineInferredType
Subclasses can override this method to use a different mechanism to determine the target type of the payload conversion.- Parameters:
method
- the method.- Returns:
- the type.
-