Member-only story
Design and Implement a Java producer consumer (Part 1)
In this document, we design a simple Producer Consumer pattern in Java.
Problem statement
The Producer-Consumer pattern is used in many places in various software systems. This is a great way of decoupling separate tasks, and handle asynchronous work. Kafka is a very popular queue/stream technology that is used to push (a lot of) data from publishers to consumers efficiently.
Let’s assume we want to send messages from Earth to Mars. Because of the great distance between Earth and Mars (118 million miles), it takes a long time for data sent from the Earth to arrive to the receptor in Mars. The computer sending the messages from Earth is quite busy with other computation tasks, so it doesn’t have time to wait for Mars to send back a signal, so it just send the information and forgets about it.
The goal is to send the text from The Republic by Plato from Earth to Mars, so that Martians can learn about great Earth philosophers. We won’t be using any third party libraries, only core Java, and the delayed message will be modeled using a Queue.
Design
A few points considered in the design:
- We need a source file to download the data, we will be downloading the text from a URL.
- We will create a Parser to breakdown the source into words to send over small packages of data.
- We will create a Sender handling…