Writing to Kafka
In the last recipe, the Reader
has the invocation of the process
method. This method belongs to the Producer
class. In this recipe, the Writer
class is explained.
Getting ready
The execution of the previous recipes in this chapter is needed.
How to do it...
As we did with the Consumer
interface, the Producer
interface is needed to keep things flexible. The two producers in this chapter will implement the Producer
interface. This interface isolates all the common behavior of the producers.
Copy the following content to a file called src/main/java/doubloon/Producer.java
:
package doubloon; import java.util.Properties; public interface Producer { public void produce(String message); // 1 public static Properties createConfig(String servers) { // 2 Properties props = new Properties(); props.put("bootstrap.servers", servers); props.put("acks", "all"); props.put("retries", 0); props.put("batch.size", 1000); props.put("linger...