-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/kafka #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feature/kafka #40
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97f8ff8
chore(spring-kafka-test): aditionate producer,consumer, compose confi…
igorcampos-dev e8fefad
chore(spring-kafka-test): aditionate producer,consumer, compose confi…
igorcampos-dev 7f92186
chore(spring-kafka-test): general improvements in the project
igorcampos-dev d793db2
chore(spring-kafka-test): add mapper
igorcampos-dev 7d92821
chore(spring-kafka-test): remove test
igorcampos-dev b34ad1f
chore(spring-kafka-test): geral adjustments
igorcampos-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #!/bin/bash | ||
|
|
||
| echo "Waiting for Kafka on port $KAFKA_PORT..." | ||
| while ! nc -z $KAFKA_HOST $KAFKA_PORT; do | ||
| sleep 2 | ||
| done | ||
|
igorcampos-dev marked this conversation as resolved.
|
||
|
|
||
| /opt/kafka/bin/kafka-topics.sh \ | ||
| --create \ | ||
| --topic "$KAFKA_TOPIC_CREATE" \ | ||
| --bootstrap-server $KAFKA_HOST:$KAFKA_PORT \ | ||
| --partitions 1 \ | ||
| --replication-factor 1 | ||
|
igorcampos-dev marked this conversation as resolved.
|
||
|
|
||
| echo "Topic $KAFKA_TOPIC_CREATE created successfully!" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
spring-kafka-example/src/main/java/com/io/example/config/JacksonConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.io.example.config; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.SerializationFeature; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class JacksonConfig { | ||
|
|
||
| @Bean | ||
| public ObjectMapper objectMapper() { | ||
| return new ObjectMapper() | ||
| .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) | ||
| .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
| .setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
spring-kafka-example/src/main/java/com/io/example/consumer/KafkaConsumerService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.io.example.consumer; | ||
|
|
||
| @SuppressWarnings("unused") | ||
|
igorcampos-dev marked this conversation as resolved.
|
||
| public interface KafkaConsumerService { | ||
| void consume(String message); | ||
|
igorcampos-dev marked this conversation as resolved.
|
||
| } | ||
24 changes: 24 additions & 0 deletions
24
spring-kafka-example/src/main/java/com/io/example/consumer/KafkaConsumerServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.io.example.consumer; | ||
|
|
||
| import com.io.example.controller.dto.request.MessageRequestDtoRequest; | ||
| import com.io.example.mapper.JsonMapper; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| @SuppressWarnings("unused") | ||
| public class KafkaConsumerServiceImpl implements KafkaConsumerService { | ||
|
igorcampos-dev marked this conversation as resolved.
|
||
|
|
||
| private final JsonMapper mapper; | ||
|
|
||
| @KafkaListener(topics = "${spring.kafka.topics.topic-1}" , groupId = "${spring.kafka.consumer.group-id}") | ||
| public void consume(String message) { | ||
| MessageRequestDtoRequest messageRequestDtoRequest = mapper.toObject(message, MessageRequestDtoRequest.class); | ||
| log.info("Message receive: {} ", messageRequestDtoRequest.toString()); | ||
| } | ||
|
igorcampos-dev marked this conversation as resolved.
|
||
|
|
||
| } | ||
25 changes: 25 additions & 0 deletions
25
spring-kafka-example/src/main/java/com/io/example/controller/ExampleController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.io.example.controller; | ||
|
|
||
| import com.io.example.controller.dto.request.MessageRequestDtoRequest; | ||
| import com.io.example.controller.dto.response.SimpleTopicDtoResponse; | ||
| import com.io.example.producer.KafkaProducerService; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/v1/example") | ||
| public class ExampleController { | ||
|
|
||
| private final KafkaProducerService kafkaProducerService; | ||
|
|
||
| @PostMapping("/send/simple-topic") | ||
| public ResponseEntity<SimpleTopicDtoResponse> sendMessageToSimpleTopic( @Valid @RequestBody MessageRequestDtoRequest dto){ | ||
| this.kafkaProducerService.sendMessage(dto); | ||
| return ResponseEntity.status(HttpStatus.OK).body(new SimpleTopicDtoResponse("Message sent successfully!")); | ||
| } | ||
|
|
||
| } |
10 changes: 10 additions & 0 deletions
10
...example/src/main/java/com/io/example/controller/dto/request/MessageRequestDtoRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.io.example.controller.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record MessageRequestDtoRequest( | ||
| @NotNull(message = "message cannot be null.") | ||
| @NotBlank(message = "message cannot be blank.") | ||
| String message | ||
| ) {} |
3 changes: 3 additions & 0 deletions
3
...-example/src/main/java/com/io/example/controller/dto/response/SimpleTopicDtoResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.io.example.controller.dto.response; | ||
|
|
||
| public record SimpleTopicDtoResponse( String message) {} |
40 changes: 40 additions & 0 deletions
40
...-kafka-example/src/main/java/com/io/example/exception/handler/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.io.example.exception.handler; | ||
|
|
||
| import com.io.example.exception.responseBody.Error; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.validation.FieldError; | ||
| import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
|
||
| @Slf4j | ||
| @ControllerAdvice | ||
| @SuppressWarnings("unused") | ||
|
igorcampos-dev marked this conversation as resolved.
|
||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(RuntimeException.class) | ||
| public ResponseEntity<Error> handleRuntimeException(RuntimeException ex, HttpServletRequest s) { | ||
| log.error("HandleRuntimeException= {}", ex.getMessage()); | ||
| return Error.response(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, s); | ||
| } | ||
|
|
||
| @ExceptionHandler(MethodArgumentNotValidException.class) | ||
| public ResponseEntity<Error> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest s) { | ||
| log.error("HandleMethodArgumentNotValidException= {}", ex.getMessage()); | ||
| return Error.response(this.getFirstErrorMessage(ex), HttpStatus.BAD_REQUEST, s); | ||
|
|
||
| } | ||
|
|
||
| private String getFirstErrorMessage(MethodArgumentNotValidException e) { | ||
| return e.getBindingResult() | ||
| .getFieldErrors() | ||
| .stream() | ||
| .findFirst() | ||
| .map(FieldError::getDefaultMessage) | ||
| .orElse(e.getMessage()); | ||
| } | ||
|
|
||
| } | ||
42 changes: 42 additions & 0 deletions
42
spring-kafka-example/src/main/java/com/io/example/exception/responseBody/Error.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.io.example.exception.responseBody; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import java.time.Instant; | ||
| import java.time.ZoneId; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| @Data | ||
| @Builder | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class Error { | ||
| private String timestamp; | ||
| private String path; | ||
| private Integer status; | ||
| private String error; | ||
|
|
||
| public static ResponseEntity<Error> response(String message, HttpStatus status, HttpServletRequest request){ | ||
| return ResponseEntity | ||
| .status(status) | ||
| .body(Error.builder() | ||
| .timestamp(getInstantNow()) | ||
| .path(request.getRequestURI()) | ||
| .status(status.value()) | ||
| .error(message) | ||
| .build()); | ||
| } | ||
|
|
||
| private static String getInstantNow() { | ||
| return Instant.now() | ||
| .atZone(ZoneId.systemDefault()) | ||
| .format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")); | ||
|
|
||
| } | ||
|
igorcampos-dev marked this conversation as resolved.
|
||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.