</properties>
<dependencies>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-web</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-actuator</artifactId>
+ </dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- </dependency>
</dependencies>
<build>
<plugins>
<plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
- <id>copy-dependencies</id>
- <phase>package</phase>
<goals>
- <goal>copy-dependencies</goal>
+ <goal>build-info</goal>
</goals>
- <configuration>
- <outputDirectory>${project.build.directory}/libs</outputDirectory>
- </configuration>
</execution>
</executions>
</plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <addClasspath>true</addClasspath>
- <classpathPrefix>libs/</classpathPrefix>
- <mainClass>de.juplo.kafka.ExampleConsumer</mainClass>
- </manifest>
- </archive>
- </configuration>
- </plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
--- /dev/null
+package de.juplo.kafka;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+
+@SpringBootApplication
+public class Application
+{
+ public static void main(String args[])
+ {
+ SpringApplication.run(Application.class, args);
+ }
+}
--- /dev/null
+package de.juplo.kafka;
+
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+
+@Configuration
+@EnableConfigurationProperties(ApplicationProperties.class)
+public class ApplicationConfiguration
+{
+ @Bean
+ public ExampleConsumer exampleConsumer(ApplicationProperties properties)
+ {
+ return new ExampleConsumer(
+ properties.getBroker(),
+ properties.getTopic(),
+ properties.getGroupId(),
+ properties.getClientId());
+ }
+}
--- /dev/null
+package de.juplo.kafka;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+
+@ConfigurationProperties(prefix = "juplo.consumer")
+@Getter
+@Setter
+public class ApplicationProperties
+{
+ private String broker;
+ private String topic;
+ private String groupId;
+ private String clientId;
+}
--- /dev/null
+package de.juplo.kafka;
+
+import jakarta.annotation.PreDestroy;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import java.time.Duration;
+
+
+@Component
+@Slf4j
+public class ConsumerRunner
+{
+ private final ExampleConsumer exampleConsumer;
+ private final Thread worker;
+
+ public ConsumerRunner(ExampleConsumer exampleConsumer)
+ {
+ this.exampleConsumer = exampleConsumer;
+ this.worker = new Thread(exampleConsumer, "ConsumerRunner-" + exampleConsumer);
+ log.info("Starting consumer: {}", exampleConsumer);
+ this.worker.start();
+ }
+
+ @PreDestroy
+ public void close()
+ {
+ log.info("Stopping: {}", exampleConsumer);
+ exampleConsumer.consumer.wakeup();
+ try
+ {
+ worker.join(Duration.ofSeconds(30));
+ }
+ catch (InterruptedException e)
+ {
+ log.error("Fehler: {} - {}", exampleConsumer, e.toString());
+ }
+ log.info("Done! {}", exampleConsumer);
+ }
+}
package de.juplo.kafka;
+import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
@Slf4j
-public class ExampleConsumer
+@ToString(of = "id")
+public class ExampleConsumer implements Runnable
{
private final String id;
private final String topic;
- private final Consumer<String, String> consumer;
+ final Consumer<String, String> consumer;
- private volatile boolean running = false;
private long consumed = 0;
public ExampleConsumer(
}
+ @Override
public void run()
{
try
{
log.info("{} - Subscribing to topic {}", id, topic);
consumer.subscribe(Arrays.asList(topic));
- running = true;
while (true)
{
log.info("{} - Closing the KafkaConsumer", id);
consumer.close();
log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
- running = false;
}
}
consumed++;
log.info("{} - partition={}-{}, offset={}: {}={}", id, topic, partition, offset, key, value);
}
-
-
- public static void main(String[] args) throws Exception
- {
- if (args.length != 4)
- {
- log.error("Four arguments required!");
- log.error("args[0]: Broker-Address");
- log.error("args[1]: Topic");
- log.error("args[2]: Group-ID");
- log.error("args[3]: Unique Client-ID");
- System.exit(1);
- return;
- }
-
-
- log.info(
- "Running ExampleConsumer: broker={}, topic={}, group-id={}, client-id={}",
- args[0],
- args[1],
- args[2],
- args[3]);
-
- ExampleConsumer instance = new ExampleConsumer(args[0], args[1], args[2], args[3]);
-
- Runtime.getRuntime().addShutdownHook(new Thread(() ->
- {
- instance.consumer.wakeup();
-
- while (instance.running)
- {
- log.info("Waiting for main-thread...");
- try
- {
- Thread.sleep(1000);
- }
- catch (InterruptedException e) {}
- }
- log.info("Shutdown completed.");
- }));
-
- instance.run();
- }
}
--- /dev/null
+juplo:
+ consumer:
+ broker: localhost:9092
+ topic: test
+ group-id: my-group
+ client-id: IntelliJ
+