|
| 1 | +/* |
| 2 | + * Copyright 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; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.apache.kafka.clients.consumer.Consumer; |
| 23 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 24 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 25 | + |
| 26 | +import org.springframework.kafka.support.TopicPartitionOffset; |
| 27 | +import org.springframework.util.Assert; |
| 28 | + |
| 29 | +/** |
| 30 | + * Adapts a legacy {@link ErrorHandler} or {@link BatchErrorHandler}. |
| 31 | + * |
| 32 | + * @author Gary Russell |
| 33 | + * @since 2.7.4 |
| 34 | + * |
| 35 | + */ |
| 36 | +public class ErrorHandlerAdapter implements CommonErrorHandler { |
| 37 | + |
| 38 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 39 | + private static final ConsumerRecords EMPTY_BATCH = new ConsumerRecords(Collections.emptyMap()); |
| 40 | + |
| 41 | + private final ErrorHandler errorHandler; |
| 42 | + |
| 43 | + private final BatchErrorHandler batchErrorHandler; |
| 44 | + |
| 45 | + /** |
| 46 | + * Adapt an {@link ErrorHandler}. |
| 47 | + * @param errorHandler the handler. |
| 48 | + */ |
| 49 | + public ErrorHandlerAdapter(ErrorHandler errorHandler) { |
| 50 | + Assert.notNull(errorHandler, "'errorHandler' cannot be null"); |
| 51 | + this.errorHandler = errorHandler; |
| 52 | + this.batchErrorHandler = null; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Adapt a {@link BatchErrorHandler}. |
| 57 | + * @param batchErrorHandler the handler. |
| 58 | + */ |
| 59 | + public ErrorHandlerAdapter(BatchErrorHandler batchErrorHandler) { |
| 60 | + Assert.notNull(batchErrorHandler, "'batchErrorHandler' cannot be null"); |
| 61 | + this.errorHandler = null; |
| 62 | + this.batchErrorHandler = batchErrorHandler; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean isBatch() { |
| 67 | + return this.batchErrorHandler != null; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean remainingRecords() { |
| 72 | + return this.errorHandler instanceof RemainingRecordsErrorHandler; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean deliveryAttemptHeader() { |
| 77 | + return this.errorHandler instanceof DeliveryAttemptAware; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void clearThreadState() { |
| 82 | + if (this.errorHandler != null) { |
| 83 | + this.errorHandler.clearThreadState(); |
| 84 | + } |
| 85 | + else { |
| 86 | + this.batchErrorHandler.clearThreadState(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean isAckAfterHandle() { |
| 92 | + if (this.errorHandler != null) { |
| 93 | + return this.errorHandler.isAckAfterHandle(); |
| 94 | + } |
| 95 | + else { |
| 96 | + return this.batchErrorHandler.isAckAfterHandle(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void setAckAfterHandle(boolean ack) { |
| 102 | + if (this.errorHandler != null) { |
| 103 | + this.errorHandler.setAckAfterHandle(ack); |
| 104 | + } |
| 105 | + else { |
| 106 | + this.batchErrorHandler.setAckAfterHandle(ack); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int deliveryAttempt(TopicPartitionOffset topicPartitionOffset) { |
| 112 | + return ((DeliveryAttemptAware) this.errorHandler).deliveryAttempt(topicPartitionOffset); |
| 113 | + } |
| 114 | + |
| 115 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 116 | + @Override |
| 117 | + public void handleConsumerException(Exception thrownException, Consumer<?, ?> consumer, |
| 118 | + MessageListenerContainer container) { |
| 119 | + |
| 120 | + if (this.errorHandler != null) { |
| 121 | + this.errorHandler.handle(thrownException, null, consumer, container); |
| 122 | + } |
| 123 | + else { |
| 124 | + this.batchErrorHandler.handle(thrownException, EMPTY_BATCH, consumer, container); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void handle(Exception thrownException, ConsumerRecord<?, ?> record, Consumer<?, ?> consumer, |
| 130 | + MessageListenerContainer container) { |
| 131 | + |
| 132 | + this.errorHandler.handle(thrownException, record, consumer); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void handle(Exception thrownException, List<ConsumerRecord<?, ?>> records, Consumer<?, ?> consumer, |
| 137 | + MessageListenerContainer container) { |
| 138 | + |
| 139 | + this.errorHandler.handle(thrownException, records, consumer, container); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void handle(Exception thrownException, ConsumerRecords<?, ?> data, Consumer<?, ?> consumer, |
| 144 | + MessageListenerContainer container, Runnable invokeListener) { |
| 145 | + |
| 146 | + this.batchErrorHandler.handle(thrownException, data, consumer, container, invokeListener); |
| 147 | + } |
| 148 | + |
| 149 | +} |
| 150 | + |
0 commit comments