diff --git a/docker-entrypoint/init.sql b/config/docker-entrypoint/init.sql
similarity index 100%
rename from docker-entrypoint/init.sql
rename to config/docker-entrypoint/init.sql
diff --git a/docker-compose.yaml b/docker-compose.yaml
index f971fba..5bd731b 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -10,7 +10,7 @@ services:
ports:
- "5432:5432"
volumes:
- - ./docker-entrypoint:/docker-entrypoint-initdb.d
+ - ./config/docker-entrypoint:/docker-entrypoint-initdb.d
zookeeper:
image: confluentinc/cp-zookeeper:latest
container_name: spring-batch-zookeeper-demo
diff --git a/pom.xml b/pom.xml
index a016418..79880da 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,6 +44,12 @@
spring-boot-starter-aop
+
+ org.springdoc
+ springdoc-openapi-starter-webmvc-ui
+ 2.5.0
+
+
org.postgresql
postgresql
diff --git a/src/main/java/com/ittovative/batchprocessing/BatchProcessingApplication.java b/src/main/java/com/ittovative/batchprocessing/BatchProcessingApplication.java
index 9ca7e31..0208dda 100644
--- a/src/main/java/com/ittovative/batchprocessing/BatchProcessingApplication.java
+++ b/src/main/java/com/ittovative/batchprocessing/BatchProcessingApplication.java
@@ -1,14 +1,43 @@
package com.ittovative.batchprocessing;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.CONTACT_EMAIL;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.CONTACT_NAME;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.CONTACT_URL;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.DESCRIPTION;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.DEVELOPMENT_SERVER_DESCRIPTION;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.DEVELOPMENT_SERVER_URL;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.TITLE;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.VERSION;
+
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import io.swagger.v3.oas.annotations.OpenAPIDefinition;
+import io.swagger.v3.oas.annotations.info.Contact;
+import io.swagger.v3.oas.annotations.info.Info;
+import io.swagger.v3.oas.annotations.servers.Server;
+
/**
* The type Batch processing application.
*/
@SpringBootApplication
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
+@OpenAPIDefinition(
+ info = @Info(
+ title = TITLE,
+ description = DESCRIPTION,
+ contact = @Contact(
+ email = CONTACT_EMAIL,
+ name = CONTACT_NAME,
+ url = CONTACT_URL
+ ),
+ version = VERSION
+ ),
+ servers = {
+ @Server(url = DEVELOPMENT_SERVER_URL, description = DEVELOPMENT_SERVER_DESCRIPTION)
+ }
+)
public class BatchProcessingApplication {
/**
@@ -20,16 +49,4 @@ public static void main(String... args) {
SpringApplication.run(BatchProcessingApplication.class, args);
}
- // uncomment to insert dummy orders to kafka at the app's startup
-
- /*@Bean
- public CommandLineRunner commandLineRunner(KafkaTemplate kafkaTemplate) {
- return args -> {
- for (int i = 0; i < 500; i++) {
- Order person = new Order(i,"order#"+i,"dummy description");
- kafkaTemplate.send("orders",person);
- }
- };
- }*/
-
}
diff --git a/src/main/java/com/ittovative/batchprocessing/config/APIResponseConstant.java b/src/main/java/com/ittovative/batchprocessing/config/APIResponseConstant.java
new file mode 100644
index 0000000..e80e128
--- /dev/null
+++ b/src/main/java/com/ittovative/batchprocessing/config/APIResponseConstant.java
@@ -0,0 +1,9 @@
+package com.ittovative.batchprocessing.config;
+
+public final class APIResponseConstant {
+ public static final String ADD_ORDER_SUCCESSFUL_MESSAGE = "Order added successfully!";
+ public static final String BATCH_SUCCESS_MESSAGE = "Batch ended successfully!";
+
+ private APIResponseConstant() {
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/ittovative/batchprocessing/config/BatchConfig.java b/src/main/java/com/ittovative/batchprocessing/config/BatchConfig.java
index 2705c64..e173736 100644
--- a/src/main/java/com/ittovative/batchprocessing/config/BatchConfig.java
+++ b/src/main/java/com/ittovative/batchprocessing/config/BatchConfig.java
@@ -1,7 +1,9 @@
package com.ittovative.batchprocessing.config;
+import static com.ittovative.batchprocessing.constant.AppConstant.KAFKA_TOPIC;
+
+import com.ittovative.batchprocessing.constant.AppConstant;
import com.ittovative.batchprocessing.model.Order;
-import com.ittovative.batchprocessing.util.AppConstants;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
@@ -89,7 +91,7 @@ public Step databaseProcessOrder(JobRepository jobRepository,
DataSource dataSource,
PagingQueryProvider pagingQueryProvider) {
return new StepBuilder("database-order-processing-step", jobRepository)
- .chunk(AppConstants.CHUNK_SIZE, platformTransactionManager)
+ .chunk(AppConstant.CHUNK_SIZE, platformTransactionManager)
.reader(jdbcOrderItemReader(dataSource, pagingQueryProvider))
.processor(itemProcessor())
.writer(flatFileItemWriter())
@@ -109,7 +111,7 @@ public Step kafkaProcessOrder(JobRepository jobRepository,
PlatformTransactionManager platformTransactionManager,
DefaultKafkaConsumerFactory defaultKafkaConsumerFactory) {
return new StepBuilder("kafka-order-processing-step", jobRepository)
- .chunk(AppConstants.CHUNK_SIZE, platformTransactionManager)
+ .chunk(AppConstant.CHUNK_SIZE, platformTransactionManager)
.reader(kafkaOrderItemReader(defaultKafkaConsumerFactory))
.processor(itemProcessor())
.writer(flatFileItemWriter())
@@ -131,7 +133,7 @@ public JdbcPagingItemReader jdbcOrderItemReader(DataSource dataSource,
.name("jdbc-item-reader")
.dataSource(dataSource)
.queryProvider(pagingQueryProvider)
- .pageSize(AppConstants.PAGE_SIZE)
+ .pageSize(AppConstant.PAGE_SIZE)
.rowMapper((resultSet, rowNum) -> {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
@@ -173,7 +175,7 @@ public KafkaItemReader kafkaOrderItemReader(
.name("orders-kafka-item-reader")
.partitions(0)
.saveState(true)
- .topic("orders")
+ .topic(KAFKA_TOPIC)
.consumerProperties(kafkaConsumerProperties)
.partitionOffsets(new HashMap<>())
.build();
@@ -187,8 +189,8 @@ public KafkaItemReader kafkaOrderItemReader(
@Bean
public ItemProcessor itemProcessor() {
return item -> {
- logger.info("Order: {" + item.getName().toLowerCase(Locale.ROOT) + "} is being processed!");
- Thread.sleep(AppConstants.THREAD_SLEEP_TIME_MS); // simulating real processing time
+ logger.info("Order: {" + item.name().toLowerCase(Locale.ROOT) + "} is being processed!");
+ Thread.sleep(AppConstant.THREAD_SLEEP_TIME_MS); // simulating real processing time
return item;
};
}
diff --git a/src/main/java/com/ittovative/batchprocessing/constant/AppConstant.java b/src/main/java/com/ittovative/batchprocessing/constant/AppConstant.java
new file mode 100644
index 0000000..c47655a
--- /dev/null
+++ b/src/main/java/com/ittovative/batchprocessing/constant/AppConstant.java
@@ -0,0 +1,12 @@
+package com.ittovative.batchprocessing.constant;
+
+public final class AppConstant {
+ public static final int THREAD_SLEEP_TIME_MS = 500;
+ public static final int CHUNK_SIZE = 5;
+ public static final int PAGE_SIZE = 5;
+
+ public static final String KAFKA_TOPIC = "dummy-orders";
+
+ private AppConstant() {
+ }
+}
diff --git a/src/main/java/com/ittovative/batchprocessing/constant/SwaggerConstant.java b/src/main/java/com/ittovative/batchprocessing/constant/SwaggerConstant.java
new file mode 100644
index 0000000..9aa73ed
--- /dev/null
+++ b/src/main/java/com/ittovative/batchprocessing/constant/SwaggerConstant.java
@@ -0,0 +1,56 @@
+package com.ittovative.batchprocessing.constant;
+
+public class SwaggerConstant {
+ public static final String TITLE = "Demo Batch Processing";
+ public static final String VERSION = "0.0.1";
+ public static final String DESCRIPTION = "Demo of using spring batch";
+ public static final String CONTACT_NAME = "Mohanad Khaled";
+ public static final String CONTACT_EMAIL = "mohanadkhaled87@gmail.com";
+ public static final String CONTACT_URL = "https://github.com/orgs/ittovate/";
+ public static final String DEVELOPMENT_SERVER_URL = "http://localhost:8080";
+ public static final String DEVELOPMENT_SERVER_DESCRIPTION = "Development Server";
+
+ //================================================= Add Order =================================================//
+
+ public static final String ADD_ORDER_KAFKA_SUMMARY = "Add new order to kafka";
+ public static final String ADD_ORDER_DB_SUMMARY = "Add new order to db";
+
+ public static final String ADD_ORDER_DESCRIPTION = "Add a new order with the " +
+ "specified name and description to the application for processing.";
+ public static final String ADD_ORDER_CREATED = "201";
+ public static final String ADD_ORDER_OK_RESPONSE_DESCRIPTION = "When order is sent successfully";
+ public static final String ADD_ORDER_REQUEST_EXAMPLE = """
+ {
+ "name": "string",
+ "description": "string"
+ }
+ """;
+ public static final String ADD_ORDER_OK_RESPONSE_EXAMPLE = """
+ {
+ "statusCode": 201,
+ "message": "Order sent successfully!",
+ "body": null
+ }
+
+ """;
+ //================================================= Start Batch =================================================//
+ private static final String START_BATCH_SUMMARY = "Start batch processing";
+
+ public static final String START_BATCH_SUMMARY_KAFKA = START_BATCH_SUMMARY + " from kafka";
+ public static final String START_BATCH_SUMMARY_DB = START_BATCH_SUMMARY + " from db";
+
+ public static final String START_BATCH_DESCRIPTION = "Starts batch processing from the beginning unless there was a " +
+ "previous failuer that was handled manually in spring batch's database , in this case it will restart after the " +
+ "last successful commit successful.";
+ public static final String START_BATCH_OK = "200";
+ public static final String START_BATCH_SUCCESFUL_DESCRIPTION = "When batch is processed successfully";
+
+ public static final String START_BATCH_OK_RESPONSE_EXAMPLE = """
+ {
+ "statusCode": 200,
+ "message": "Batch ended successfully!!",
+ "body": null
+ }
+
+ """;
+}
diff --git a/src/main/java/com/ittovative/batchprocessing/controller/OrderController.java b/src/main/java/com/ittovative/batchprocessing/controller/OrderController.java
index 135d010..91dc704 100644
--- a/src/main/java/com/ittovative/batchprocessing/controller/OrderController.java
+++ b/src/main/java/com/ittovative/batchprocessing/controller/OrderController.java
@@ -1,12 +1,31 @@
package com.ittovative.batchprocessing.controller;
-import com.ittovative.batchprocessing.dto.OrderDto;
+import static com.ittovative.batchprocessing.config.APIResponseConstant.ADD_ORDER_SUCCESSFUL_MESSAGE;
+import static com.ittovative.batchprocessing.config.APIResponseConstant.BATCH_SUCCESS_MESSAGE;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.ADD_ORDER_DB_SUMMARY;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.ADD_ORDER_DESCRIPTION;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.ADD_ORDER_OK_RESPONSE_EXAMPLE;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.ADD_ORDER_REQUEST_EXAMPLE;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.ADD_ORDER_KAFKA_SUMMARY;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.START_BATCH_DESCRIPTION;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.START_BATCH_OK;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.START_BATCH_OK_RESPONSE_EXAMPLE;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.START_BATCH_SUMMARY_DB;
+import static com.ittovative.batchprocessing.constant.SwaggerConstant.START_BATCH_SUMMARY_KAFKA;
+import static org.springframework.http.HttpStatus.CREATED;
+
import com.ittovative.batchprocessing.model.Order;
import com.ittovative.batchprocessing.service.OrderService;
-import com.ittovative.batchprocessing.util.ApiResponse;
-import com.ittovative.batchprocessing.util.BatchReadType;
+import com.ittovative.batchprocessing.util.APIResponse;
+import com.ittovative.batchprocessing.enums.BatchReadType;
+import com.ittovative.batchprocessing.util.ResponseUtil;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.media.Content;
+import io.swagger.v3.oas.annotations.media.ExampleObject;
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
+
import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -32,15 +51,22 @@ public OrderController(OrderService orderService) {
* @param order the order
* @return the response entity
*/
+ @Operation(summary = ADD_ORDER_KAFKA_SUMMARY, description = ADD_ORDER_DESCRIPTION,
+ requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true,
+ content = @Content(schema = @Schema(implementation = Order.class),
+ examples = @ExampleObject(ADD_ORDER_REQUEST_EXAMPLE))),
+ responses = @ApiResponse(
+ responseCode = "201",
+ content = @Content(
+ schema = @Schema(implementation = APIResponse.class),
+ examples = @ExampleObject(ADD_ORDER_OK_RESPONSE_EXAMPLE)
+ )
+ ))
@PostMapping("/kafka")
- public ResponseEntity> makeOrderKafka(@RequestBody OrderDto orderDto) {
+ public APIResponse makeOrderKafka(@RequestBody Order orderDto) {
orderService.sendOrderToKafka(orderDto);
- ApiResponse apiResponse = new ApiResponse<>(
- HttpStatus.OK.value(),
- "Order sent to kafka successfully!",
- null
- );
- return new ResponseEntity<>(apiResponse, HttpStatus.OK);
+ return ResponseUtil.createUnifiedResponse(CREATED.value(),
+ ADD_ORDER_SUCCESSFUL_MESSAGE,null);
}
/**
@@ -49,16 +75,21 @@ public ResponseEntity> makeOrderKafka(@RequestBody OrderDto
* @param order the order
* @return the response entity
*/
+ @Operation(summary = ADD_ORDER_DB_SUMMARY, description = ADD_ORDER_DESCRIPTION,
+ requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true,
+ content = @Content(schema = @Schema(implementation = Order.class),
+ examples = @ExampleObject(ADD_ORDER_REQUEST_EXAMPLE))), responses = @ApiResponse(
+ responseCode = "201",
+ content = @Content(
+ schema = @Schema(implementation = APIResponse.class),
+ examples = @ExampleObject(ADD_ORDER_OK_RESPONSE_EXAMPLE)
+ )
+ ))
@PostMapping("/db")
- public ResponseEntity> makeOrderDatabase(@RequestBody OrderDto order) {
- Order convertedOrder = new Order(order.name(),order.description());
- orderService.sendOrderToDatabase(convertedOrder);
- ApiResponse apiResponse = new ApiResponse<>(
- HttpStatus.OK.value(),
- "Order sent to database successfully!",
- null
- );
- return new ResponseEntity<>(apiResponse, HttpStatus.OK);
+ public APIResponse makeOrderDatabase(@RequestBody Order order) {
+ orderService.sendOrderToDatabase(order);
+ return ResponseUtil.createUnifiedResponse(CREATED.value(),ADD_ORDER_SUCCESSFUL_MESSAGE
+ ,null);
}
/**
@@ -67,15 +98,19 @@ public ResponseEntity> makeOrderDatabase(@RequestBody OrderD
* @return the response entity
* @throws Exception the exception
*/
+ @Operation(summary = START_BATCH_SUMMARY_DB + " from database", description = START_BATCH_DESCRIPTION,
+ responses = @ApiResponse(
+ responseCode = START_BATCH_OK,
+ content = @Content(
+ schema = @Schema(implementation = APIResponse.class),
+ examples = @ExampleObject(START_BATCH_OK_RESPONSE_EXAMPLE)
+ )
+ ))
@PostMapping("/batch-db")
- public ResponseEntity> batchProcessDB() throws Exception {
+ public APIResponse batchProcessDB() throws Exception {
orderService.batchProcess(BatchReadType.DATABASE);
- ApiResponse apiResponse = new ApiResponse<>(
- HttpStatus.OK.value(),
- "Batch processing from db started successfully!",
- null
- );
- return new ResponseEntity<>(apiResponse, HttpStatus.OK);
+ return ResponseUtil.createUnifiedResponse(HttpStatus.OK.value(),
+ BATCH_SUCCESS_MESSAGE,null);
}
/**
@@ -84,15 +119,19 @@ public ResponseEntity> batchProcessDB() throws Exception {
* @return the response entity
* @throws Exception the exception
*/
+ @Operation(summary = START_BATCH_SUMMARY_KAFKA, description = START_BATCH_DESCRIPTION,
+ responses = @ApiResponse(
+ responseCode = START_BATCH_OK,
+ content = @Content(
+ schema = @Schema(implementation = APIResponse.class),
+ examples = @ExampleObject(START_BATCH_OK_RESPONSE_EXAMPLE)
+ )
+ ))
@PostMapping("/batch-kafka")
- public ResponseEntity> batchProcessKafka() throws Exception {
+ public APIResponse batchProcessKafka() throws Exception {
orderService.batchProcess(BatchReadType.KAFKA);
- ApiResponse apiResponse = new ApiResponse<>(
- HttpStatus.OK.value(),
- "Batch processing from kafka started successfully!",
- null
- );
- return new ResponseEntity<>(apiResponse, HttpStatus.OK);
+ return ResponseUtil.createUnifiedResponse(HttpStatus.OK.value(),
+ BATCH_SUCCESS_MESSAGE,null);
}
}
diff --git a/src/main/java/com/ittovative/batchprocessing/dto/OrderDto.java b/src/main/java/com/ittovative/batchprocessing/dto/OrderDto.java
deleted file mode 100644
index 89c58ab..0000000
--- a/src/main/java/com/ittovative/batchprocessing/dto/OrderDto.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package com.ittovative.batchprocessing.dto;
-
-
-public record OrderDto(String name, String description) {
-}
diff --git a/src/main/java/com/ittovative/batchprocessing/util/BatchReadType.java b/src/main/java/com/ittovative/batchprocessing/enums/BatchReadType.java
similarity index 80%
rename from src/main/java/com/ittovative/batchprocessing/util/BatchReadType.java
rename to src/main/java/com/ittovative/batchprocessing/enums/BatchReadType.java
index ccb1e62..8b97b91 100644
--- a/src/main/java/com/ittovative/batchprocessing/util/BatchReadType.java
+++ b/src/main/java/com/ittovative/batchprocessing/enums/BatchReadType.java
@@ -1,4 +1,4 @@
-package com.ittovative.batchprocessing.util;
+package com.ittovative.batchprocessing.enums;
/**
* The enum Batch read type.
diff --git a/src/main/java/com/ittovative/batchprocessing/exception/GlobalExceptionHandler.java b/src/main/java/com/ittovative/batchprocessing/exception/GlobalExceptionHandler.java
index 5a8ace1..088e8a8 100644
--- a/src/main/java/com/ittovative/batchprocessing/exception/GlobalExceptionHandler.java
+++ b/src/main/java/com/ittovative/batchprocessing/exception/GlobalExceptionHandler.java
@@ -1,8 +1,9 @@
package com.ittovative.batchprocessing.exception;
-import com.ittovative.batchprocessing.util.ApiResponse;
+import com.ittovative.batchprocessing.util.APIResponse;
+import com.ittovative.batchprocessing.util.ResponseUtil;
+
import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -19,14 +20,10 @@ public class GlobalExceptionHandler {
* @return the response entity
*/
@ExceptionHandler(Exception.class)
- public ResponseEntity> handle(Exception exception) {
+ public APIResponse handle(Exception exception) {
exception.fillInStackTrace();
- ApiResponse apiResponse = new ApiResponse<>(
- HttpStatus.INTERNAL_SERVER_ERROR.value(),
- HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
- null
- );
- return new ResponseEntity<>(apiResponse, HttpStatus.INTERNAL_SERVER_ERROR);
+ return ResponseUtil.createUnifiedResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(),
+ HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),null);
}
}
diff --git a/src/main/java/com/ittovative/batchprocessing/model/Order.java b/src/main/java/com/ittovative/batchprocessing/model/Order.java
index 9d95a2e..eb75bbf 100644
--- a/src/main/java/com/ittovative/batchprocessing/model/Order.java
+++ b/src/main/java/com/ittovative/batchprocessing/model/Order.java
@@ -1,43 +1,5 @@
package com.ittovative.batchprocessing.model;
-public class Order {
- private int id;
- private String name;
- private String description;
-
- public Order(int id, String name, String description) {
- this.id = id;
- this.name = name;
- this.description = description;
- }
-
- public Order(String name, String description) {
- this.name = name;
- this.description = description;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-}
+public record Order(int id, String name, String description) {
+}
\ No newline at end of file
diff --git a/src/main/java/com/ittovative/batchprocessing/service/OrderService.java b/src/main/java/com/ittovative/batchprocessing/service/OrderService.java
index fab179e..7f9ed48 100644
--- a/src/main/java/com/ittovative/batchprocessing/service/OrderService.java
+++ b/src/main/java/com/ittovative/batchprocessing/service/OrderService.java
@@ -1,8 +1,9 @@
package com.ittovative.batchprocessing.service;
-import com.ittovative.batchprocessing.dto.OrderDto;
+import static com.ittovative.batchprocessing.constant.AppConstant.KAFKA_TOPIC;
+
import com.ittovative.batchprocessing.model.Order;
-import com.ittovative.batchprocessing.util.BatchReadType;
+import com.ittovative.batchprocessing.enums.BatchReadType;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
@@ -17,7 +18,7 @@
@Service
public class OrderService {
- private final KafkaTemplate kafkaTemplate;
+ private final KafkaTemplate kafkaTemplate;
private final JdbcTemplate jdbcTemplate;
private final Logger logger = Logger.getLogger(OrderService.class.getName());
private final ApplicationContext applicationContext;
@@ -31,7 +32,7 @@ public class OrderService {
* @param jobLauncher the job launcher
* @param jdbcTemplate the jdbc template
*/
- public OrderService(KafkaTemplate kafkaTemplate,
+ public OrderService(KafkaTemplate kafkaTemplate,
ApplicationContext applicationContext,
JobLauncher jobLauncher,
JdbcTemplate jdbcTemplate) {
@@ -46,9 +47,9 @@ public OrderService(KafkaTemplate kafkaTemplate,
*
* @param order the order
*/
- public void sendOrderToKafka(OrderDto orderDto) {
- logger.info("Sending order: " + orderDto);
- kafkaTemplate.send("orders", orderDto);
+ public void sendOrderToKafka(Order order) {
+ logger.info("Sending order: " + order);
+ kafkaTemplate.send(KAFKA_TOPIC, order);
logger.info("Order sent to kafka");
}
@@ -60,7 +61,7 @@ public void sendOrderToKafka(OrderDto orderDto) {
public void sendOrderToDatabase(Order order) {
logger.info("Sending order: " + order);
String sql = "INSERT INTO orders (name, description) VALUES (?, ?)";
- jdbcTemplate.update(sql, order.getName(), order.getDescription());
+ jdbcTemplate.update(sql, order.name(), order.description());
logger.info("Order saved into database");
}
diff --git a/src/main/java/com/ittovative/batchprocessing/util/APIResponse.java b/src/main/java/com/ittovative/batchprocessing/util/APIResponse.java
new file mode 100644
index 0000000..5aec293
--- /dev/null
+++ b/src/main/java/com/ittovative/batchprocessing/util/APIResponse.java
@@ -0,0 +1,9 @@
+package com.ittovative.batchprocessing.util;
+
+/**
+ * The type Api response.
+ *
+ * @param the type parameter
+ */
+public record APIResponse(int statusCode, String message, T body) {
+}
\ No newline at end of file
diff --git a/src/main/java/com/ittovative/batchprocessing/util/ApiResponse.java b/src/main/java/com/ittovative/batchprocessing/util/ApiResponse.java
deleted file mode 100644
index 15845bd..0000000
--- a/src/main/java/com/ittovative/batchprocessing/util/ApiResponse.java
+++ /dev/null
@@ -1,52 +0,0 @@
-package com.ittovative.batchprocessing.util;
-
-/**
- * The type Api response.
- *
- * @param the type parameter
- */
-public class ApiResponse {
- private final int statusCode;
- private final String message;
- private final T body;
-
- /**
- * Instantiates a new Api response.
- *
- * @param statusCode the status code
- * @param message the message
- * @param body the body
- */
- public ApiResponse(int statusCode, String message, T body) {
- this.statusCode = statusCode;
- this.message = message;
- this.body = body;
- }
-
- /**
- * Gets status code.
- *
- * @return the status code
- */
- public int getStatusCode() {
- return statusCode;
- }
-
- /**
- * Gets message.
- *
- * @return the message
- */
- public String getMessage() {
- return message;
- }
-
- /**
- * Gets body.
- *
- * @return the body
- */
- public T getBody() {
- return body;
- }
-}
diff --git a/src/main/java/com/ittovative/batchprocessing/util/AppConstants.java b/src/main/java/com/ittovative/batchprocessing/util/AppConstants.java
deleted file mode 100644
index b154378..0000000
--- a/src/main/java/com/ittovative/batchprocessing/util/AppConstants.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package com.ittovative.batchprocessing.util;
-
-public class AppConstants {
- public static final int THREAD_SLEEP_TIME_MS = 500;
- public static final int CHUNK_SIZE = 5;
- public static final int PAGE_SIZE = 5;
-}
diff --git a/src/main/java/com/ittovative/batchprocessing/util/ResponseUtil.java b/src/main/java/com/ittovative/batchprocessing/util/ResponseUtil.java
new file mode 100644
index 0000000..7d87b67
--- /dev/null
+++ b/src/main/java/com/ittovative/batchprocessing/util/ResponseUtil.java
@@ -0,0 +1,20 @@
+package com.ittovative.batchprocessing.util;
+
+public final class ResponseUtil {
+
+ /**
+ * Create unified response api response.
+ *
+ * @param the type parameter
+ * @param statusCode the status code
+ * @param message the message
+ * @param body the body
+ * @return the api response
+ */
+ public static APIResponse createUnifiedResponse(int statusCode, String message, T body) {
+ return new APIResponse<>(statusCode, message, body);
+ }
+
+ private ResponseUtil() {
+ }
+}