mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-26 07:58:51 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
+10
-9
@@ -32,13 +32,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* The main entry point for the idempotent-consumer application.
|
||||
* This application demonstrates the use of the Idempotent Consumer
|
||||
* pattern which ensures that a message is processed exactly once
|
||||
* in scenarios where the same message can be delivered multiple times.
|
||||
* The main entry point for the idempotent-consumer application. This application demonstrates the
|
||||
* use of the Idempotent Consumer pattern which ensures that a message is processed exactly once in
|
||||
* scenarios where the same message can be delivered multiple times.
|
||||
*
|
||||
* @see <a href="https://en.wikipedia.org/wiki/Idempotence">Idempotence (Wikipedia)</a>
|
||||
* @see <a href="https://camel.apache.org/components/latest/eips/idempotentConsumer-eip.html">Idempotent Consumer Pattern (Apache Camel)</a>
|
||||
* @see <a
|
||||
* href="https://camel.apache.org/components/latest/eips/idempotentConsumer-eip.html">Idempotent
|
||||
* Consumer Pattern (Apache Camel)</a>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@Slf4j
|
||||
@@ -46,9 +47,9 @@ public class App {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* The starting point of the CommandLineRunner
|
||||
* where the main program is run.
|
||||
* The starting point of the CommandLineRunner where the main program is run.
|
||||
*
|
||||
* @param requestService idempotent request service
|
||||
* @param requestRepository request jpa repository
|
||||
@@ -59,7 +60,8 @@ public class App {
|
||||
Request req = requestService.create(UUID.randomUUID());
|
||||
requestService.create(req.getUuid());
|
||||
requestService.create(req.getUuid());
|
||||
LOGGER.info("Nb of requests : {}", requestRepository.count()); // 1, processRequest is idempotent
|
||||
LOGGER.info(
|
||||
"Nb of requests : {}", requestRepository.count()); // 1, processRequest is idempotent
|
||||
req = requestService.start(req.getUuid());
|
||||
try {
|
||||
req = requestService.start(req.getUuid());
|
||||
@@ -71,4 +73,3 @@ public class App {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -25,9 +25,9 @@
|
||||
package com.iluwatar.idempotentconsumer;
|
||||
|
||||
/**
|
||||
* This exception is thrown when an invalid transition is attempted in the Statemachine
|
||||
* for the request status. This can occur when attempting to move to a state that is not valid
|
||||
* from the current state.
|
||||
* This exception is thrown when an invalid transition is attempted in the Statemachine for the
|
||||
* request status. This can occur when attempting to move to a state that is not valid from the
|
||||
* current state.
|
||||
*/
|
||||
public class InvalidNextStateException extends RuntimeException {
|
||||
public InvalidNextStateException(String s) {
|
||||
|
||||
+3
-4
@@ -31,8 +31,8 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* The {@code Request} class represents a request with a unique UUID and a status.
|
||||
* The status of a request can be one of four values: PENDING, STARTED, COMPLETED, or INERROR.
|
||||
* The {@code Request} class represents a request with a unique UUID and a status. The status of a
|
||||
* request can be one of four values: PENDING, STARTED, COMPLETED, or INERROR.
|
||||
*/
|
||||
@Entity
|
||||
@NoArgsConstructor
|
||||
@@ -44,8 +44,7 @@ public class Request {
|
||||
COMPLETED
|
||||
}
|
||||
|
||||
@Id
|
||||
private UUID uuid;
|
||||
@Id private UUID uuid;
|
||||
private Status status;
|
||||
|
||||
public Request(UUID uuid) {
|
||||
|
||||
+2
-3
@@ -28,9 +28,8 @@ import java.util.UUID;
|
||||
|
||||
/**
|
||||
* This class extends the RuntimeException class to handle scenarios where a Request is not found.
|
||||
* It is intended to be used where you would like to have a custom exception that signals that a requested object or action
|
||||
* was not found in the system, based on the UUID of the request.
|
||||
*
|
||||
* It is intended to be used where you would like to have a custom exception that signals that a
|
||||
* requested object or action was not found in the system, based on the UUID of the request.
|
||||
*/
|
||||
public class RequestNotFoundException extends RuntimeException {
|
||||
RequestNotFoundException(UUID uuid) {
|
||||
|
||||
+6
-7
@@ -29,12 +29,11 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* This is a repository interface for the "Request" entity. It extends the JpaRepository interface from Spring Data JPA.
|
||||
* JpaRepository comes with many operations out of the box, including standard CRUD operations.
|
||||
* With JpaRepository, we are also able to leverage the power of Spring Data's query methods.
|
||||
* The UUID parameter in JpaRepository refers to the type of the ID in the "Request" entity.
|
||||
*
|
||||
* This is a repository interface for the "Request" entity. It extends the JpaRepository interface
|
||||
* from Spring Data JPA. JpaRepository comes with many operations out of the box, including standard
|
||||
* CRUD operations. With JpaRepository, we are also able to leverage the power of Spring Data's
|
||||
* query methods. The UUID parameter in JpaRepository refers to the type of the ID in the "Request"
|
||||
* entity.
|
||||
*/
|
||||
@Repository
|
||||
public interface RequestRepository extends JpaRepository<Request, UUID> {
|
||||
}
|
||||
public interface RequestRepository extends JpaRepository<Request, UUID> {}
|
||||
|
||||
+6
-7
@@ -29,24 +29,23 @@ import java.util.UUID;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* This service is responsible for handling request operations including
|
||||
* creation, start, and completion of requests.
|
||||
* This service is responsible for handling request operations including creation, start, and
|
||||
* completion of requests.
|
||||
*/
|
||||
@Service
|
||||
public class RequestService {
|
||||
RequestRepository requestRepository;
|
||||
RequestStateMachine requestStateMachine;
|
||||
|
||||
public RequestService(RequestRepository requestRepository,
|
||||
RequestStateMachine requestStateMachine) {
|
||||
public RequestService(
|
||||
RequestRepository requestRepository, RequestStateMachine requestStateMachine) {
|
||||
this.requestRepository = requestRepository;
|
||||
this.requestStateMachine = requestStateMachine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Request or returns an existing one by it's UUID.
|
||||
* This operation is idempotent: performing it once or several times
|
||||
* successively leads to an equivalent result.
|
||||
* Creates a new Request or returns an existing one by it's UUID. This operation is idempotent:
|
||||
* performing it once or several times successively leads to an equivalent result.
|
||||
*
|
||||
* @param uuid The unique identifier for the Request.
|
||||
* @return Return existing Request or save and return a new Request.
|
||||
|
||||
+6
-4
@@ -27,8 +27,8 @@ package com.iluwatar.idempotentconsumer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* This class represents a state machine for managing request transitions.
|
||||
* It supports transitions to the statuses: PENDING, STARTED, and COMPLETED.
|
||||
* This class represents a state machine for managing request transitions. It supports transitions
|
||||
* to the statuses: PENDING, STARTED, and COMPLETED.
|
||||
*/
|
||||
@Component
|
||||
public class RequestStateMachine {
|
||||
@@ -36,8 +36,10 @@ public class RequestStateMachine {
|
||||
/**
|
||||
* Provides the next possible state of the request based on the current and next status.
|
||||
*
|
||||
* @param req The actual request object. This object MUST NOT be null and SHOULD have a valid status.
|
||||
* @param nextStatus Represents the next status that the request could transition to. MUST NOT be null.
|
||||
* @param req The actual request object. This object MUST NOT be null and SHOULD have a valid
|
||||
* status.
|
||||
* @param nextStatus Represents the next status that the request could transition to. MUST NOT be
|
||||
* null.
|
||||
* @return A new Request object with updated status if the transition is valid.
|
||||
* @throws InvalidNextStateException If an invalid state transition is attempted.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user