|
| 1 | +/* |
| 2 | + * Copyright 2022 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.integration.kafka.inbound; |
| 18 | + |
| 19 | +import org.apache.kafka.clients.consumer.Consumer; |
| 20 | + |
| 21 | +import org.springframework.kafka.KafkaException; |
| 22 | +import org.springframework.kafka.support.Acknowledgment; |
| 23 | +import org.springframework.retry.RecoveryCallback; |
| 24 | +import org.springframework.retry.support.RetryTemplate; |
| 25 | + |
| 26 | +/** |
| 27 | + * Implementations of this interface will generally support a retry template for retrying |
| 28 | + * incoming deliveries and this supports adding common attributes to the retry context. |
| 29 | + * |
| 30 | + * @author Gary Russell |
| 31 | + * @since 6.0 |
| 32 | + * |
| 33 | + */ |
| 34 | +public interface KafkaInboundEndpoint { |
| 35 | + |
| 36 | + /** |
| 37 | + * {@link org.springframework.retry.RetryContext} attribute key for an acknowledgment |
| 38 | + * if the listener is capable of acknowledging. |
| 39 | + */ |
| 40 | + String CONTEXT_ACKNOWLEDGMENT = "acknowledgment"; |
| 41 | + |
| 42 | + /** |
| 43 | + * {@link org.springframework.retry.RetryContext} attribute key for the consumer if |
| 44 | + * the listener is consumer-aware. |
| 45 | + */ |
| 46 | + String CONTEXT_CONSUMER = "consumer"; |
| 47 | + |
| 48 | + /** |
| 49 | + * {@link org.springframework.retry.RetryContext} attribute key for the record. |
| 50 | + */ |
| 51 | + String CONTEXT_RECORD = "record"; |
| 52 | + |
| 53 | + /** |
| 54 | + * Execute the runnable with the retry template and recovery callback. |
| 55 | + * @param template the template. |
| 56 | + * @param callback the callback. |
| 57 | + * @param record the record (or records). |
| 58 | + * @param acknowledgment the acknowledgment. |
| 59 | + * @param consumer the consumer. |
| 60 | + * @param runnable the runnable. |
| 61 | + */ |
| 62 | + default void doWithRetry(RetryTemplate template, RecoveryCallback<?> callback, Object data, |
| 63 | + Acknowledgment acknowledgment, Consumer<?, ?> consumer, Runnable runnable) { |
| 64 | + |
| 65 | + try { |
| 66 | + template.execute(context -> { |
| 67 | + context.setAttribute(CONTEXT_RECORD, data); |
| 68 | + context.setAttribute(CONTEXT_ACKNOWLEDGMENT, acknowledgment); |
| 69 | + context.setAttribute(CONTEXT_CONSUMER, consumer); |
| 70 | + runnable.run(); |
| 71 | + return null; |
| 72 | + }, callback); |
| 73 | + } |
| 74 | + catch (Exception ex) { |
| 75 | + throw new KafkaException("Failed to execute runnable", ex); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments