-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andreas Mautsch
committed
Aug 4, 2024
1 parent
411d11b
commit f6ca34f
Showing
5 changed files
with
52 additions
and
10 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
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
47 changes: 47 additions & 0 deletions
47
src/test/java/org/goafabric/eventdispatcher/service/producer/TestConsumer.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,47 @@ | ||
package org.goafabric.eventdispatcher.service.producer; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import org.goafabric.event.EventData; | ||
import org.goafabric.eventdispatcher.consumer.LatchConsumer; | ||
import org.goafabric.eventdispatcher.service.extensions.TenantContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.MDC; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.kafka.support.KafkaHeaders; | ||
import org.springframework.messaging.handler.annotation.Header; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
@Component | ||
public class TestConsumer implements LatchConsumer { | ||
private final Logger log = LoggerFactory.getLogger(this.getClass()); | ||
|
||
static final String CONSUMER_NAME = "Calendar"; | ||
public static Long CONSUMER_COUNT = 0L; | ||
|
||
private final CountDownLatch latch = new CountDownLatch(1); | ||
|
||
|
||
@KafkaListener(groupId = CONSUMER_NAME, topics = {"test-topic"}) //only topics listed here will be autocreated | ||
public void processKafka(@Header(KafkaHeaders.RECEIVED_TOPIC) String topic, EventData eventData) { | ||
withTenantInfos(() -> process(topic, eventData)); | ||
} | ||
|
||
private void process(String topic, EventData eventData) { | ||
log.info("processing test event"); | ||
CONSUMER_COUNT++; | ||
latch.countDown(); | ||
} | ||
|
||
private static void withTenantInfos(Runnable runnable) { | ||
Span.fromContext(Context.current()).setAttribute("tenant.id", TenantContext.getTenantId()); | ||
MDC.put("tenantId", TenantContext.getTenantId()); | ||
try { runnable.run(); } finally { MDC.remove("tenantId"); } | ||
} | ||
|
||
@Override | ||
public CountDownLatch getLatch() { return latch; } | ||
} |