#!/bin/bash
-IMAGE=juplo/endless-producer:1.0-SNAPSHOT
+IMAGE=juplo/endless-stream-spring-producer:1.0-SNAPSHOT
if [ "$1" = "cleanup" ]
then
command: sleep infinity
producer:
- image: juplo/endless-producer:1.0-SNAPSHOT
+ image: juplo/endless-stream-spring-producer:1.0-SNAPSHOT
ports:
- 8080:8080
environment:
server.port: 8080
- producer.bootstrap-server: kafka:9092
- producer.client-id: producer
- producer.topic: test
+ spring.kafka.bootstrap-servers: kafka:9092
+ spring.kafka.client-id: producer
+ spring.kafka.template.default-topic: test
producer.throttle-ms: 200
</parent>
<groupId>de.juplo.kafka</groupId>
- <artifactId>endless-producer</artifactId>
- <name>Endless Producer: a Simple Producer that endlessly writes numbers into a topic</name>
+ <artifactId>endless-stream-spring-producer</artifactId>
+ <name>Endless Producer: a Spring Producer that endlessly writes numbers into a topic</name>
<version>1.0-SNAPSHOT</version>
<dependencies>
<optional>true</optional>
</dependency>
<dependency>
- <groupId>org.apache.kafka</groupId>
- <artifactId>kafka-clients</artifactId>
+ <groupId>org.springframework.kafka</groupId>
+ <artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
package de.juplo.kafka;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
+import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.Assert;
import java.util.concurrent.Executors;
@EnableConfigurationProperties(ApplicationProperties.class)
public class Application
{
- @Autowired
- ApplicationProperties properties;
-
-
@Bean
- public EndlessProducer producer()
+ public EndlessProducer producer(
+ ApplicationProperties properties,
+ @Value("${spring.kafka.client-id:DEV}") String clientId,
+ KafkaTemplate<String, String> kafkaTemplate)
{
- Assert.hasText(properties.getBootstrapServer(), "producer.bootstrap-server must be set");
- Assert.hasText(properties.getClientId(), "producer.client-id must be set");
- Assert.hasText(properties.getTopic(), "producer.topic must be set");
EndlessProducer producer =
new EndlessProducer(
Executors.newFixedThreadPool(1),
- properties.getBootstrapServer(),
- properties.getClientId(),
- properties.getTopic(),
- properties.getAcks(),
- properties.getThrottleMs());
+ clientId,
+ properties.getThrottleMs(),
+ kafkaTemplate);
producer.start();
@Setter
public class ApplicationProperties
{
- private String bootstrapServer;
- private String clientId;
- private String topic;
- private String acks;
private int throttleMs;
}
package de.juplo.kafka;
import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
-import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.kafka.clients.producer.RecordMetadata;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.kafka.support.SendResult;
+import org.springframework.util.concurrent.ListenableFuture;
import javax.annotation.PreDestroy;
-import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
{
private final ExecutorService executor;
private final String id;
- private final String topic;
private final int throttleMs;
- private final KafkaProducer<String, String> producer;
+ private final KafkaTemplate<String, String> kafkaTemplate;
private boolean running = false;
private long i = 0;
public EndlessProducer(
ExecutorService executor,
- String bootstrapServer,
String clientId,
- String topic,
- String acks,
- int throttleMs)
+ int throttleMs,
+ KafkaTemplate<String, String> kafkaTemplate)
{
this.executor = executor;
this.id = clientId;
- this.topic = topic;
this.throttleMs = throttleMs;
-
- Properties props = new Properties();
- props.put("bootstrap.servers", bootstrapServer);
- props.put("client.id", clientId);
- props.put("acks", acks);
- props.put("key.serializer", StringSerializer.class.getName());
- props.put("value.serializer", StringSerializer.class.getName());
-
- this.producer = new KafkaProducer<>(props);
+ this.kafkaTemplate = kafkaTemplate;
}
@Override
{
final long time = System.currentTimeMillis();
- final ProducerRecord<String, String> record = new ProducerRecord<>(
- topic, // Topic
- key, // Key
- value // Value
- );
-
- producer.send(record, (metadata, e) ->
- {
- long now = System.currentTimeMillis();
- if (e == null)
+ ListenableFuture<SendResult<String, String>> listenableFuture = kafkaTemplate.sendDefault(key, value);
+ listenableFuture.addCallback(
+ result ->
{
+ long now = System.currentTimeMillis();
+ RecordMetadata metadata = result.getRecordMetadata();
+ ProducerRecord<String, String> record = result.getProducerRecord();
+
// HANDLE SUCCESS
produced++;
log.debug(
metadata.timestamp(),
now - time
);
- }
- else
+ },
+ e ->
{
+ long now = System.currentTimeMillis();
+
// HANDLE ERROR
log.error(
- "{} - ERROR key={} timestamp={} latency={}ms: {}",
+ "{} - ERROR key={} latency={}ms: {}",
id,
- record.key(),
- metadata == null ? -1 : metadata.timestamp(),
+ key,
now - time,
e.toString()
);
- }
- });
+ });
long now = System.currentTimeMillis();
log.trace(
"{} - Queued #{} key={} latency={}ms",
id,
value,
- record.key(),
+ key,
now - time
);
}
}
finally
{
- log.info("{} - Closing the KafkaProducer", id);
- producer.close();
log.info("{}: Produced {} messages in total, exiting!", id, produced);
}
}
producer:
- bootstrap-server: :9092
- client-id: DEV
- topic: test
- acks: 1
throttle-ms: 1000
management:
endpoint:
enabled: true
info:
kafka:
- bootstrap-server: ${producer.bootstrap-server}
- client-id: ${producer.client-id}
- topic: ${producer.topic}
- acks: ${producer.acks}
+ bootstrap-servers: ${spring.kafka.bootstrap-servers}
+ client-id: ${spring.kafka.client-id}
+ topic: ${spring.kafka.template.default-topic}
+ acks: ${spring.kafka.producer.acks}
throttle-ms: ${producer.throttle-ms}
+ batch-size: ${spring.kafka.producer.batch-size}
+ linger-ms: ${spring.kafka.producer.properties.linger.ms}
+ compression-type: ${spring.kafka.producer.compression-type}
+spring:
+ kafka:
+ bootstrap-servers: :9092
+ client-id: DEV
+ template:
+ default-topic: test
logging:
level:
root: INFO