Skip to content

Commit 97f8ff8

Browse files
chore(spring-kafka-test): aditionate producer,consumer, compose configs, global handler exception,logs and lombok in the project
1 parent 68bf3a3 commit 97f8ff8

File tree

10 files changed

+169
-1
lines changed

10 files changed

+169
-1
lines changed

spring-kafka-example/compose.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ services:
99
environment:
1010
SERVER_PORT: "80"
1111
SPRING_PROFILES_ACTIVE: "default"
12+
KAFKA_SERVER_PORT: kafka:9092
13+
depends_on:
14+
- kafka
1215

1316
kafka:
17+
image: apache/kafka:4.0.0
1418
container_name: kafka
15-
image: confluentinc/cp-kafka:8.0.0
1619
ports:
1720
- "9092:9092"
1821
environment:
@@ -26,3 +29,5 @@ services:
2629
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
2730
KAFKA_LOG_DIRS: /tmp/kraft-combined-logs
2831
CLUSTER_ID: qGyb4Z0XQpeoKgUXYfCCLw
32+
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
33+
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'

spring-kafka-example/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<java.version>21</java.version>
2323
<spring.boot.version>3.5.4</spring.boot.version>
2424
<spring.kafka.version>3.3.8</spring.kafka.version>
25+
<lombok.version>1.18.38</lombok.version>
2526
</properties>
2627

2728
<dependencies>
@@ -38,6 +39,13 @@
3839
<version>${spring.kafka.version}</version>
3940
</dependency>
4041

42+
<dependency>
43+
<groupId>org.projectlombok</groupId>
44+
<artifactId>lombok</artifactId>
45+
<optional>true</optional>
46+
<version>${lombok.version}</version>
47+
</dependency>
48+
4149
<dependency>
4250
<groupId>org.springframework.boot</groupId>
4351
<artifactId>spring-boot-starter-test</artifactId>
@@ -55,15 +63,38 @@
5563
</dependencies>
5664

5765
<build>
66+
5867
<plugins>
5968

69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<configuration>
73+
<annotationProcessorPaths>
74+
<path>
75+
<groupId>org.projectlombok</groupId>
76+
<artifactId>lombok</artifactId>
77+
</path>
78+
</annotationProcessorPaths>
79+
</configuration>
80+
</plugin>
81+
6082
<plugin>
6183
<groupId>org.springframework.boot</groupId>
6284
<artifactId>spring-boot-maven-plugin</artifactId>
6385
<version>${spring.boot.version}</version>
86+
<configuration>
87+
<excludes>
88+
<exclude>
89+
<groupId>org.projectlombok</groupId>
90+
<artifactId>lombok</artifactId>
91+
</exclude>
92+
</excludes>
93+
</configuration>
6494
</plugin>
6595

6696
</plugins>
97+
6798
</build>
6899

69100
</project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.io.example.consumer;
2+
3+
@SuppressWarnings("unused")
4+
public interface KafkaConsumerService {
5+
void consume(String mensagem);
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.io.example.consumer;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.kafka.annotation.KafkaListener;
5+
import org.springframework.stereotype.Service;
6+
7+
@Slf4j
8+
@Service
9+
@SuppressWarnings("unused")
10+
public class KafkaConsumerServiceImpl implements KafkaConsumerService {
11+
12+
@KafkaListener(topics = "${spring.kafka.topics.topic-1}" , groupId = "${spring.kafka.consumer.group-id}")
13+
public void consume(String message) {
14+
log.info("Message receive: {} ", message);
15+
}
16+
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.io.example.controller.dto.request;
2+
3+
public record MessageRequestDtoRequest(String message) {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.io.example.controller.dto.response;
2+
3+
public record GetDtoResponse(String message) {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.io.example.exception.handler;
2+
3+
import com.io.example.exception.responseBody.Error;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
11+
@Slf4j
12+
@ControllerAdvice
13+
@SuppressWarnings("unused")
14+
public class GlobalExceptionHandler {
15+
16+
@ExceptionHandler(RuntimeException.class)
17+
public ResponseEntity<ResponseEntity<Error>> handleRuntimeException(RuntimeException ex, HttpServletRequest s) {
18+
log.error("HandleRuntimeException= {}", ex.getMessage());
19+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
20+
.body(Error.response(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, s));
21+
}
22+
23+
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.io.example.exception.responseBody;
2+
3+
import jakarta.servlet.ServletRequest;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.web.server.ServerWebExchange;
12+
13+
import java.time.Instant;
14+
import java.time.ZoneId;
15+
import java.time.format.DateTimeFormatter;
16+
17+
@Data
18+
@Builder
19+
@AllArgsConstructor
20+
@NoArgsConstructor
21+
public class Error {
22+
private String timestamp;
23+
private String path;
24+
private Integer status;
25+
private String error;
26+
27+
public static ResponseEntity<Error> response(String message, HttpStatus status, HttpServletRequest uri){
28+
return ResponseEntity
29+
.status(status)
30+
.body(Error.builder()
31+
.timestamp(getInstantNow())
32+
.path(uri.getRequestURI())
33+
.status(status.value())
34+
.error(message)
35+
.build());
36+
}
37+
38+
private static String getInstantNow() {
39+
return Instant.now()
40+
.atZone(ZoneId.systemDefault())
41+
.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"));
42+
43+
}
44+
45+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.io.example.producer;
2+
3+
@FunctionalInterface
4+
public interface KafkaProducerService {
5+
void sendMessage(String mensagem);
6+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.io.example.producer;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.kafka.core.KafkaTemplate;
7+
import org.springframework.stereotype.Service;
8+
9+
@Slf4j
10+
@Service
11+
@RequiredArgsConstructor
12+
@SuppressWarnings("unused")
13+
public class KafkaProducerServiceImpl implements KafkaProducerService {
14+
15+
@SuppressWarnings("unused")
16+
@Value("${spring.kafka.topics.topic-1}")
17+
private String topic;
18+
19+
private final KafkaTemplate<String, String> kafkaTemplate;
20+
21+
@Override
22+
public void sendMessage(String message) {
23+
log.info("Preparing to send message to Kafka topic: {}", topic);
24+
kafkaTemplate.send(topic, message);
25+
log.info("Message successfully dispatched: {}", message);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)