-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add KafkaProducer post-processor to enable extensions from other libr…
…aries (#90) * add KafkaProducer post-processor * refactor * add autoconfiguration in case we don't have a PP for the Producer * add snapshot version * use @Autowired required false
- Loading branch information
Showing
10 changed files
with
177 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.31.1 | ||
version=0.32.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(); | ||
} | ||
} | ||
} | ||
} | ||
} |