All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.kafka.listener.adapter;

import java.lang.reflect.Method;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;

import org.springframework.kafka.listener.AcknowledgingConsumerAwareMessageListener;
import org.springframework.kafka.listener.KafkaListenerErrorHandler;
import org.springframework.kafka.listener.ListenerExecutionFailedException;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;


/**
 * A {@link org.springframework.kafka.listener.MessageListener MessageListener}
 * adapter that invokes a configurable {@link HandlerAdapter}; used when the factory is
 * configured for the listener to receive individual messages.
 *
 * 

Wraps the incoming Kafka Message to Spring's {@link Message} abstraction. * *

The original {@link ConsumerRecord} and * the {@link Acknowledgment} are provided as additional arguments so that these can * be injected as method arguments if necessary. * * @param the key type. * @param the value type. * * @author Stephane Nicoll * @author Gary Russell * @author Artem Bilan * @author Venil Noronha */ public class RecordMessagingMessageListenerAdapter extends MessagingMessageListenerAdapter implements AcknowledgingConsumerAwareMessageListener { private KafkaListenerErrorHandler errorHandler; public RecordMessagingMessageListenerAdapter(Object bean, Method method) { this(bean, method, null); } public RecordMessagingMessageListenerAdapter(Object bean, Method method, KafkaListenerErrorHandler errorHandler) { super(bean, method); this.errorHandler = errorHandler; } /** * Kafka {@link AcknowledgingConsumerAwareMessageListener} entry point. *

Delegate the message to the target listener method, * with appropriate conversion of the message argument. * @param record the incoming Kafka {@link ConsumerRecord}. * @param acknowledgment the acknowledgment. * @param consumer the consumer. */ @Override public void onMessage(ConsumerRecord record, Acknowledgment acknowledgment, Consumer consumer) { Message message; if (isConversionNeeded()) { message = toMessagingMessage(record, acknowledgment, consumer); } else { message = NULL_MESSAGE; } if (logger.isDebugEnabled()) { logger.debug("Processing [" + message + "]"); } try { Object result = invokeHandler(record, acknowledgment, message, consumer); if (result != null) { handleResult(result, record, message); } } catch (ListenerExecutionFailedException e) { // NOSONAR ex flow control if (this.errorHandler != null) { try { if (message.equals(NULL_MESSAGE)) { message = new GenericMessage<>(record); } Object result = this.errorHandler.handleError(message, e, consumer); if (result != null) { handleResult(result, record, message); } } catch (Exception ex) { throw new ListenerExecutionFailedException(createMessagingErrorMessage(// NOSONAR stack trace loss "Listener error handler threw an exception for the incoming message", message.getPayload()), ex); } } else { throw e; } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy