|
| 1 | +/* |
| 2 | + * Copyright 2016-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.listener.adapter; |
| 18 | + |
| 19 | +import org.apache.kafka.clients.consumer.Consumer; |
| 20 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 21 | + |
| 22 | +import org.springframework.kafka.listener.AcknowledgingConsumerAwareMessageListener; |
| 23 | +import org.springframework.kafka.listener.MessageListener; |
| 24 | +import org.springframework.kafka.support.Acknowledgment; |
| 25 | +import org.springframework.lang.Nullable; |
| 26 | +import org.springframework.retry.RecoveryCallback; |
| 27 | +import org.springframework.retry.RetryState; |
| 28 | +import org.springframework.retry.support.DefaultRetryState; |
| 29 | +import org.springframework.retry.support.RetryTemplate; |
| 30 | +import org.springframework.util.Assert; |
| 31 | + |
| 32 | +/** |
| 33 | + * A retrying message listener adapter for {@link MessageListener}s. |
| 34 | + * |
| 35 | + * @param <K> the key type. |
| 36 | + * @param <V> the value type. |
| 37 | + * |
| 38 | + * @author Gary Russell |
| 39 | + * @deprecated since 2.8 - use a suitably configured error handler instead. |
| 40 | + * |
| 41 | + */ |
| 42 | +@Deprecated |
| 43 | +public class RetryingMessageListenerAdapter<K, V> |
| 44 | + extends AbstractRetryingMessageListenerAdapter<K, V, MessageListener<K, V>> |
| 45 | + implements AcknowledgingConsumerAwareMessageListener<K, V> { |
| 46 | + |
| 47 | + /** |
| 48 | + * {@link org.springframework.retry.RetryContext} attribute key for an acknowledgment |
| 49 | + * if the listener is capable of acknowledging. |
| 50 | + */ |
| 51 | + public static final String CONTEXT_ACKNOWLEDGMENT = "acknowledgment"; |
| 52 | + |
| 53 | + /** |
| 54 | + * {@link org.springframework.retry.RetryContext} attribute key for the consumer if |
| 55 | + * the listener is consumer-aware. |
| 56 | + */ |
| 57 | + public static final String CONTEXT_CONSUMER = "consumer"; |
| 58 | + |
| 59 | + /** |
| 60 | + * {@link org.springframework.retry.RetryContext} attribute key for the record. |
| 61 | + */ |
| 62 | + public static final String CONTEXT_RECORD = "record"; |
| 63 | + |
| 64 | + private boolean stateful; |
| 65 | + |
| 66 | + /** |
| 67 | + * Construct an instance with the provided template and delegate. The exception will |
| 68 | + * be thrown to the container after retries are exhausted. |
| 69 | + * @param messageListener the delegate listener. |
| 70 | + * @param retryTemplate the template. |
| 71 | + */ |
| 72 | + public RetryingMessageListenerAdapter(MessageListener<K, V> messageListener, RetryTemplate retryTemplate) { |
| 73 | + this(messageListener, retryTemplate, null); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Construct an instance with the provided template, callback and delegate. |
| 78 | + * @param messageListener the delegate listener. |
| 79 | + * @param retryTemplate the template. |
| 80 | + * @param recoveryCallback the recovery callback; if null, the exception will be |
| 81 | + * thrown to the container after retries are exhausted. |
| 82 | + */ |
| 83 | + public RetryingMessageListenerAdapter(MessageListener<K, V> messageListener, RetryTemplate retryTemplate, |
| 84 | + @Nullable RecoveryCallback<? extends Object> recoveryCallback) { |
| 85 | + |
| 86 | + this(messageListener, retryTemplate, recoveryCallback, false); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Construct an instance with the provided template, callback and delegate. When using |
| 91 | + * stateful retry, the retry context key is a concatenated String |
| 92 | + * {@code topic-partition-offset}. A |
| 93 | + * {@link org.springframework.kafka.listener.SeekToCurrentErrorHandler} is required in |
| 94 | + * the listener container because stateful retry will throw the exception to the |
| 95 | + * container for each delivery attempt. |
| 96 | + * @param messageListener the delegate listener. |
| 97 | + * @param retryTemplate the template. |
| 98 | + * @param recoveryCallback the recovery callback; if null, the exception will be |
| 99 | + * thrown to the container after retries are exhausted. |
| 100 | + * @param stateful true for stateful retry. |
| 101 | + * @since 2.1.3 |
| 102 | + */ |
| 103 | + public RetryingMessageListenerAdapter(MessageListener<K, V> messageListener, RetryTemplate retryTemplate, |
| 104 | + @Nullable RecoveryCallback<? extends Object> recoveryCallback, boolean stateful) { |
| 105 | + |
| 106 | + super(messageListener, retryTemplate, recoveryCallback); |
| 107 | + Assert.notNull(messageListener, "'messageListener' cannot be null"); |
| 108 | + this.stateful = stateful; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onMessage(final ConsumerRecord<K, V> record, @Nullable final Acknowledgment acknowledgment, |
| 113 | + final Consumer<?, ?> consumer) { |
| 114 | + |
| 115 | + RetryState retryState = null; |
| 116 | + if (this.stateful) { |
| 117 | + retryState = new DefaultRetryState(record.topic() + "-" + record.partition() + "-" + record.offset()); |
| 118 | + } |
| 119 | + getRetryTemplate().execute(context -> { |
| 120 | + context.setAttribute(CONTEXT_RECORD, record); |
| 121 | + switch (RetryingMessageListenerAdapter.this.delegateType) { |
| 122 | + case ACKNOWLEDGING_CONSUMER_AWARE: |
| 123 | + context.setAttribute(CONTEXT_ACKNOWLEDGMENT, acknowledgment); |
| 124 | + context.setAttribute(CONTEXT_CONSUMER, consumer); |
| 125 | + RetryingMessageListenerAdapter.this.delegate.onMessage(record, acknowledgment, consumer); |
| 126 | + break; |
| 127 | + case ACKNOWLEDGING: |
| 128 | + context.setAttribute(CONTEXT_ACKNOWLEDGMENT, acknowledgment); |
| 129 | + RetryingMessageListenerAdapter.this.delegate.onMessage(record, acknowledgment); |
| 130 | + break; |
| 131 | + case CONSUMER_AWARE: |
| 132 | + context.setAttribute(CONTEXT_CONSUMER, consumer); |
| 133 | + RetryingMessageListenerAdapter.this.delegate.onMessage(record, consumer); |
| 134 | + break; |
| 135 | + case SIMPLE: |
| 136 | + RetryingMessageListenerAdapter.this.delegate.onMessage(record); |
| 137 | + } |
| 138 | + return null; |
| 139 | + }, |
| 140 | + getRecoveryCallback(), retryState); |
| 141 | + } |
| 142 | + |
| 143 | + /* |
| 144 | + * Since the container uses the delegate's type to determine which method to call, we |
| 145 | + * must implement them all. |
| 146 | + */ |
| 147 | + |
| 148 | + @Override |
| 149 | + public void onMessage(ConsumerRecord<K, V> data) { |
| 150 | + onMessage(data, null, null); // NOSONAR |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void onMessage(ConsumerRecord<K, V> data, Acknowledgment acknowledgment) { |
| 155 | + onMessage(data, acknowledgment, null); // NOSONAR |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void onMessage(ConsumerRecord<K, V> data, Consumer<?, ?> consumer) { |
| 160 | + onMessage(data, null, consumer); |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments