-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add KafkaProducer post-processor to enable extensions from other libraries #90
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e376f5d
add KafkaProducer post-processor
sszp 17aca96
refactor
sszp 38c079f
add autoconfiguration in case we don't have a PP for the Producer
sszp 0c89930
add snapshot version
sszp b49bff7
use @Autowired required false
sszp cfde383
Merge branch 'master' into RDP-3392
sszp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.30.1 | ||
version=0.31.0-SNAPSHOT-0 |
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
7 changes: 7 additions & 0 deletions
7
...ter/src/main/java/com/transferwise/kafka/tkms/config/ITkmsKafkaProducerPostProcessor.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,7 @@ | ||
package com.transferwise.kafka.tkms.config; | ||
|
||
import java.util.function.Function; | ||
import org.apache.kafka.clients.producer.Producer; | ||
|
||
public interface ITkmsKafkaProducerPostProcessor extends Function<Producer<String, byte[]>, Producer<String, byte[]>> { | ||
} |
6 changes: 3 additions & 3 deletions
6
...-starter/src/main/java/com/transferwise/kafka/tkms/config/ITkmsKafkaProducerProvider.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
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
63 changes: 63 additions & 0 deletions
63
tw-tkms-starter/src/test/java/com/transferwise/kafka/tkms/MessagePostProcessingTest.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,63 @@ | ||
package com.transferwise.kafka.tkms; | ||
|
||
import static com.transferwise.kafka.tkms.test.TestKafkaProducerPostProcessor.TEST_MESSAGE; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.transferwise.common.baseutils.transactionsmanagement.ITransactionsHelper; | ||
import com.transferwise.kafka.tkms.api.ITransactionalKafkaMessageSender; | ||
import com.transferwise.kafka.tkms.api.TkmsMessage; | ||
import com.transferwise.kafka.tkms.test.BaseIntTest; | ||
import com.transferwise.kafka.tkms.test.ITkmsSentMessagesCollector; | ||
import com.transferwise.kafka.tkms.test.TestMessagesInterceptor; | ||
import com.transferwise.kafka.tkms.test.TestProperties; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.stream.StreamSupport; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
class MessagePostProcessingTest extends BaseIntTest { | ||
|
||
@Autowired | ||
private TestMessagesInterceptor testMessagesInterceptor; | ||
@Autowired | ||
private TransactionalKafkaMessageSender transactionalKafkaMessageSender; | ||
@Autowired | ||
private TestProperties testProperties; | ||
@Autowired | ||
private ITransactionsHelper transactionsHelper; | ||
|
||
@AfterEach | ||
void cleanupTest() { | ||
testMessagesInterceptor.setBeforeSendingToKafkaFunction(null); | ||
} | ||
|
||
@Test | ||
void messagesAreInstrumentedWithProducerPostProcessor() { | ||
byte[] someValue = TEST_MESSAGE; | ||
|
||
String topic = testProperties.getTestTopic(); | ||
|
||
transactionsHelper.withTransaction().run(() -> | ||
transactionalKafkaMessageSender.sendMessages(new ITransactionalKafkaMessageSender.SendMessagesRequest() | ||
.addTkmsMessage(new TkmsMessage().setTopic(topic).setKey("1").setValue(someValue)) | ||
.addTkmsMessage(new TkmsMessage().setTopic(topic).setKey("2").setValue(someValue)) | ||
)); | ||
|
||
await().until(() -> tkmsSentMessagesCollector.getSentMessages(topic).size() == 2); | ||
var messages = tkmsSentMessagesCollector.getSentMessages(topic); | ||
|
||
assertEquals(2, messages.size()); | ||
checkForHeader(messages.get(0), "wrapTest", "wrapped"); | ||
checkForHeader(messages.get(1), "wrapTest", "wrapped"); | ||
} | ||
|
||
private void checkForHeader(ITkmsSentMessagesCollector.SentMessage sentMessage, String key, String value) { | ||
assertTrue( | ||
StreamSupport.stream(sentMessage.getProducerRecord().headers().spliterator(), false) | ||
.anyMatch(h -> h.key().equals(key) && value.equals(new String(h.value(), StandardCharsets.UTF_8))) | ||
); | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
...tarter/src/test/java/com/transferwise/kafka/tkms/test/TestKafkaProducerPostProcessor.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,65 @@ | ||
package com.transferwise.kafka.tkms.test; | ||
|
||
import com.transferwise.kafka.tkms.config.ITkmsKafkaProducerPostProcessor; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import org.apache.kafka.clients.producer.Callback; | ||
import org.apache.kafka.clients.producer.Producer; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TestKafkaProducerPostProcessor implements ITkmsKafkaProducerPostProcessor { | ||
|
||
public static final byte[] TEST_MESSAGE = "Testing ProducerPostProcessing".getBytes(StandardCharsets.UTF_8); | ||
|
||
private ProxyInvocationHandler handler; | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Producer<String, byte[]> apply(Producer<String, byte[]> producer) { | ||
handler = new ProxyInvocationHandler(producer); | ||
return (Producer<String, byte[]>) | ||
Proxy.newProxyInstance( | ||
TestKafkaProducerPostProcessor.class.getClassLoader(), | ||
new Class<?>[] {Producer.class}, | ||
handler); | ||
} | ||
|
||
private static class ProxyInvocationHandler implements InvocationHandler { | ||
|
||
private final Producer<String, byte[]> producer; | ||
|
||
public ProxyInvocationHandler(Producer<String, byte[]> producer) { | ||
this.producer = producer; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
if ("send".equals(method.getName()) | ||
&& method.getParameterCount() >= 1 | ||
&& method.getParameterTypes()[0] == ProducerRecord.class) { | ||
ProducerRecord<String, byte[]> record = (ProducerRecord<String, byte[]>) args[0]; | ||
if (Arrays.equals(TEST_MESSAGE, record.value())) { | ||
record.headers().add("wrapTest", "wrapped".getBytes(StandardCharsets.UTF_8)); | ||
} | ||
Callback callback = | ||
method.getParameterCount() >= 2 | ||
&& method.getParameterTypes()[1] == Callback.class | ||
? (Callback) args[1] | ||
: null; | ||
return producer.send(record, callback); | ||
} else { | ||
try { | ||
return method.invoke(producer, args); | ||
} catch (InvocationTargetException exception) { | ||
throw exception.getCause(); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably would be better to declare the autowire usage as
@Autowired(required = false)
Then you don't need to explicitly provide an empty list bean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed that.