Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
final class KafkaPublisher implements Publisher {
private KafkaClient kafkaClient;
public KafkaPublisher(KafkaClient kafkaClient) {
this.kafkaClient = kafkaClient;
}
public void publish(Message m) {
kafka.publish(m.getType(), m);
}
}
final class LoopBackPublisher implements Publisher {
private Map<Topic, Subscriber[]> subscribers;
public Publisher(Map<Topic, Subscriber[]> subscribers) {
this.subscribers = subscribers;
}
public void publish(Message m) {
topic = new Topic(m.getType());
subscribers.getOrDefault(topic, {}).forEach(subscriber -> subscriber.handle(m));
}
}
final class PublishingHub implements Publisher {
private Publisher[] publishers;
public PublishingHub(Publisher... publishers) {
this.publishers = publishers;
}
public void publish(Message m) {
publishers.forEach(p -> p.publish(m));
}
}
// use case
publisher = new PublishingHub(loopBackPublisher, kafkaPublisher);
publisher.publish(new SomethingCoolHappened());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.