0% found this document useful (0 votes)
36 views

ACME's Message Dispatcher

A software engineer was tasked with creating a message dispatcher library in Java or another JVM language to efficiently dispatch messages from producer objects to correct consumer objects across multiple teams' applications at an ACME Corporation. The library should handle receiving and dispatching messages to registered consumers according to topic rules without external dependencies.

Uploaded by

Nikhil Kr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

ACME's Message Dispatcher

A software engineer was tasked with creating a message dispatcher library in Java or another JVM language to efficiently dispatch messages from producer objects to correct consumer objects across multiple teams' applications at an ACME Corporation. The library should handle receiving and dispatching messages to registered consumers according to topic rules without external dependencies.

Uploaded by

Nikhil Kr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ACME's Message Dispatcher

The ACME Corporation develops an ample range of commercial software, from desktop
applications, mobile apps, web applications to microservices.

The company's engineering teams use Java or other JVM languages. Multiple teams face a
similar problem: they have internal objects that generate multiple messages and other objects
interested in receiving them. They don’t know how to efficiently dispatch the messages from the
producers to the correct consumers.

You are a top skilled software engineer in ACME company and were selected to tackle that
issue. Your objective is to create a library in Java or other JVM language like Kotlin or Scala for
the other team developers to use in their projects.

Because of the myriad of different applications, your library should not use external
dependencies, only components already provided in your language’s standard library. It should
handle exclusively the matter of receiving a bunch of messages (multiple times and constantly),
and dispatching them to the correct consumers.

If you are more comfortable with non-JVM languages, feel free to prepare for this exercise in
some node.js or Go or Rust or your other favorite language. Be prepared to explain the code to
the interviewer who may not speak the language as well as you do!

Just keep in mind that the objective is to implement a library, not a full-fetched application. In
other words, don’t worry about HTTP / gRPC / etc. endpoints, RESTful decorators, database
interactions, etc.

What the library should provide:

● A Message class

The message object should be immutable and have two properties:


- Topics: a list of strings
- Content: a string

● A Message Dispatcher class

The message dispatcher should offer the following thread-safe operations:

A. Register a consumer (optionally in a specific topic).


If a topic is not provided during the registration, the consumer should receive all
the messages.
B. Define a named link between topics.
It should support creating named bi-directional links between two different topics.
It should be possible to create multiple links between the same pair of topics with
different names.

C. Receive messages and dispatch them to the correct registered consumers.

The dispatch should observe the following rules:


● A consumer should receive the message if it is registered in one of the
message's topics.
● A consumer should receive the message if it is registered to a topic
directly linked to one of the message's topics.
● A consumer should receive the message if it is registered to receive all
messages.
● A consumer should receive the same message object only once, even
when it matches multiple rules.

Example:

There is a link between the topics: "Blue" ←→ color of ←→ "Ball"


- A consumer C1 is registered to the topic "Blue"
- A consumer C2 is registered to the topic "Ball"
- A consumer C3 is registered to the topic "Square"
- A consumer C4 is registered to all messages
- A consumer C5 is registered to topics "Red" and "Blue"

When processing one message with topics: ["Red", "Ball"], the message
dispatcher will dispatch the message to:
- consumer C1 because it is registered to a topic linked to the "Ball" topic.
- consumer C2 because it is registered to the topic "Ball".
- consumer C4 because it is registered to all messages.
- consumer C5 because it is registered to the topic "Red" and it is
registered to a topic linked to the "Ball" topic

D. Deregister a previously registered consumer.

You might also like