-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1681: Add "after" methods to RecordInterceptor
Resolves #1681 Also add `BatchInterceptor`. * Fix LogAccessor Usage.
- Loading branch information
1 parent
8879427
commit f2f54ca
Showing
12 changed files
with
409 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
spring-kafka/src/main/java/org/springframework/kafka/listener/BatchInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2021 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; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
|
||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* An interceptor for batches of records. | ||
* | ||
* @param <K> the key type. | ||
* @param <V> the value type. | ||
* | ||
* @author Gary Russell | ||
* @since 2.7 | ||
* | ||
*/ | ||
@FunctionalInterface | ||
public interface BatchInterceptor<K, V> { | ||
|
||
/** | ||
* Perform some action on the records or return a different one. If null is returned | ||
* the records will be skipped. Invoked before the listener. | ||
* @param records the records. | ||
* @return the records or null. | ||
*/ | ||
@Nullable | ||
ConsumerRecords<K, V> intercept(ConsumerRecords<K, V> records); | ||
|
||
/** | ||
* Called after the listener exits normally. | ||
* @param records the records. | ||
*/ | ||
default void success(ConsumerRecords<K, V> records) { | ||
} | ||
|
||
/** | ||
* Called after the listener throws an exception. | ||
* @param records the records. | ||
* @param exception the exception. | ||
*/ | ||
default void failure(ConsumerRecords<K, V> records, Exception exception) { | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
spring-kafka/src/main/java/org/springframework/kafka/listener/CompositeBatchInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 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; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A {@link BatchInterceptor} that delegates to one or more {@link BatchInterceptor}s in | ||
* order. | ||
* | ||
* @param <K> the key type. | ||
* @param <V> the value type. | ||
* | ||
* @author Gary Russell | ||
* @since 2.7 | ||
* | ||
*/ | ||
public class CompositeBatchInterceptor<K, V> implements BatchInterceptor<K, V> { | ||
|
||
private final Collection<BatchInterceptor<K, V>> delegates = new ArrayList<>(); | ||
|
||
/** | ||
* Construct an instance with the provided delegates. | ||
* @param delegates the delegates. | ||
*/ | ||
@SafeVarargs | ||
@SuppressWarnings("varargs") | ||
public CompositeBatchInterceptor(BatchInterceptor<K, V>... delegates) { | ||
Assert.notNull(delegates, "'delegates' cannot be null"); | ||
Assert.noNullElements(delegates, "'delegates' cannot have null entries"); | ||
this.delegates.addAll(Arrays.asList(delegates)); | ||
} | ||
|
||
@Override | ||
public ConsumerRecords<K, V> intercept(ConsumerRecords<K, V> records) { | ||
ConsumerRecords<K, V> recordsToIntercept = records; | ||
for (BatchInterceptor<K, V> delegate : this.delegates) { | ||
recordsToIntercept = delegate.intercept(recordsToIntercept); | ||
} | ||
return recordsToIntercept; | ||
} | ||
|
||
@Override | ||
public void success(ConsumerRecords<K, V> records) { | ||
this.delegates.forEach(del -> del.success(records)); | ||
} | ||
|
||
@Override | ||
public void failure(ConsumerRecords<K, V> records, Exception exception) { | ||
this.delegates.forEach(del -> del.failure(records, exception)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.