From 762d5f0f56f66972f94d1dc1c19a29842338462a Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Thu, 19 May 2022 10:14:05 +0100 Subject: [PATCH] Reformatted code --- lightrun/README.md | 9 +- lightrun/api-service/pom.xml | 80 ++++++------ .../apiservice/ApiServiceApplication.java | 6 +- .../apiservice/RequestIdGenerator.java | 14 +-- .../apiservice/RestTemplateConfig.java | 12 +- .../adapters/http/TaskResponse.java | 7 +- .../adapters/http/TasksController.java | 22 ++-- .../apiservice/adapters/tasks/Task.java | 7 +- .../adapters/tasks/TaskRepository.java | 6 +- .../adapters/users/UserRepository.java | 7 +- lightrun/pom.xml | 8 +- lightrun/tasks-service/pom.xml | 114 +++++++++--------- .../tasksservice/TasksServiceApplication.java | 6 +- .../adapters/http/TaskResponse.java | 7 +- .../adapters/http/TasksController.java | 20 ++- .../adapters/jms/JmsConsumer.java | 2 +- .../adapters/repository/TaskRecord.java | 6 +- .../service/DeletedUserService.java | 28 ++--- .../tasksservice/service/TasksService.java | 25 ++-- .../src/main/resources/application.properties | 4 - lightrun/users-service/README.md | 9 +- lightrun/users-service/pom.xml | 114 +++++++++--------- .../usersservice/UsersServiceApplication.java | 6 +- .../adapters/http/UserResponse.java | 3 +- .../adapters/http/UsersController.java | 13 +- .../usersservice/service/UsersService.java | 23 ++-- .../src/main/resources/application.properties | 3 - 27 files changed, 278 insertions(+), 283 deletions(-) diff --git a/lightrun/README.md b/lightrun/README.md index 732d9b03cd..18d4ccc12f 100644 --- a/lightrun/README.md +++ b/lightrun/README.md @@ -3,16 +3,21 @@ This application exists as an example for the Lightrun series of articles. ## Building + This application requires [Apache Maven](https://maven.apache.org/) and [Java 17+](https://www.oracle.com/java/technologies/downloads/). Building the code is done by executing: + ``` $ mvn install ``` + from the top level. ## Running + The application consists of three services: + * Tasks * Users * API @@ -23,7 +28,9 @@ The Tasks and Users services exist as microservices for managing one facet of da This does mean that the startup order is important. The JMS queue exists within the Tasks service and is connected to from the Users service. As such, the Tasks service must be started before the others. -Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for example: +Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for +example: + ``` $ java -jar ./target/tasks-service-0.0.1-SNAPSHOT.jar ``` diff --git a/lightrun/api-service/pom.xml b/lightrun/api-service/pom.xml index c72d66748c..3423c490f1 100644 --- a/lightrun/api-service/pom.xml +++ b/lightrun/api-service/pom.xml @@ -1,45 +1,45 @@ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.7 - - - com.baeldung - api-service - 0.0.1-SNAPSHOT - api-service - Aggregator Service for LightRun Article - - 17 - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + api-service + 0.0.1-SNAPSHOT + api-service + Aggregator Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java index 4d7e2f3ff7..a9b29cbd4b 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ApiServiceApplication { - public static void main(String[] args) { - SpringApplication.run(ApiServiceApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ApiServiceApplication.class, args); + } } diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java index 3ad137ca4d..f15738c1e6 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java @@ -1,20 +1,20 @@ package com.baeldung.apiservice; -import org.slf4j.MDC; -import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.ModelAndView; +import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.UUID; + +import org.slf4j.MDC; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; @Component public class RequestIdGenerator implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String requestId = UUID.randomUUID().toString(); + String requestId = UUID.randomUUID() + .toString(); MDC.put(RequestIdGenerator.class.getCanonicalName(), requestId); response.addHeader("X-Request-Id", requestId); diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java index 278e1520c4..1582ba5953 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java @@ -9,12 +9,12 @@ import org.springframework.web.client.RestTemplate; public class RestTemplateConfig { @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { - return builder - .additionalInterceptors((request, body, execution) -> { - request.getHeaders().add("X-Request-Id", RequestIdGenerator.getRequestId()); + return builder.additionalInterceptors((request, body, execution) -> { + request.getHeaders() + .add("X-Request-Id", RequestIdGenerator.getRequestId()); - return execution.execute(request, body); - }) - .build(); + return execution.execute(request, body); + }) + .build(); } } diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java index cec3105c04..875390fdcd 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java @@ -2,10 +2,5 @@ package com.baeldung.apiservice.adapters.http; import java.time.Instant; -public record TaskResponse(String id, - String title, - Instant created, - UserResponse createdBy, - UserResponse assignedTo, - String status) { +public record TaskResponse(String id, String title, Instant created, UserResponse createdBy, UserResponse assignedTo, String status) { } diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java index e11eaac35f..55b449f249 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java @@ -1,11 +1,17 @@ package com.baeldung.apiservice.adapters.http; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + import com.baeldung.apiservice.adapters.tasks.Task; import com.baeldung.apiservice.adapters.tasks.TaskRepository; import com.baeldung.apiservice.adapters.users.UserRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.*; @RequestMapping("/") @RestController @@ -27,15 +33,10 @@ public class TasksController { } private TaskResponse buildResponse(Task task) { - return new TaskResponse(task.id(), - task.title(), - task.created(), - getUser(task.createdBy()), - getUser(task.assignedTo()), - task.status()); + return new TaskResponse(task.id(), task.title(), task.created(), getUser(task.createdBy()), getUser(task.assignedTo()), task.status()); } - private UserResponse getUser(String userId) { + private UserResponse getUser(String userId) { if (userId == null) { return null; } @@ -47,6 +48,7 @@ public class TasksController { return new UserResponse(user.id(), user.name()); } + @ExceptionHandler(UnknownTaskException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public void handleUnknownTask() { diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java index a83192f188..188d3e951c 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java @@ -2,10 +2,5 @@ package com.baeldung.apiservice.adapters.tasks; import java.time.Instant; -public record Task(String id, - String title, - Instant created, - String createdBy, - String assignedTo, - String status) { +public record Task(String id, String title, Instant created, String createdBy, String assignedTo, String status) { } diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java index 49ffa51818..9260a125af 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java @@ -17,9 +17,9 @@ public class TaskRepository { public Task getTaskById(String id) { var uri = UriComponentsBuilder.fromUriString(tasksService) - .path(id) - .build() - .toUri(); + .path(id) + .build() + .toUri(); try { return restTemplate.getForObject(uri, Task.class); diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java index 5a0e4eb64f..d5625f6efc 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java @@ -1,6 +1,5 @@ package com.baeldung.apiservice.adapters.users; -import com.baeldung.apiservice.adapters.users.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Repository; @@ -18,9 +17,9 @@ public class UserRepository { public User getUserById(String id) { var uri = UriComponentsBuilder.fromUriString(usersService) - .path(id) - .build() - .toUri(); + .path(id) + .build() + .toUri(); try { return restTemplate.getForObject(uri, User.class); diff --git a/lightrun/pom.xml b/lightrun/pom.xml index cfe275848f..4d046347ab 100644 --- a/lightrun/pom.xml +++ b/lightrun/pom.xml @@ -1,13 +1,13 @@ - + 4.0.0 com.baelduung lightrun-demo 0.0.1-SNAPSHOT pom - lightrun - Services for LightRun Article + lightrun + Services for LightRun Article tasks-service diff --git a/lightrun/tasks-service/pom.xml b/lightrun/tasks-service/pom.xml index e56a3a9edf..441b4d3713 100644 --- a/lightrun/tasks-service/pom.xml +++ b/lightrun/tasks-service/pom.xml @@ -1,67 +1,67 @@ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.7 - - - com.baeldung - tasks-service - 0.0.1-SNAPSHOT - tasks-service - Tasks Service for LightRun Article - - 17 - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-artemis - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - org.flywaydb - flyway-core - + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + tasks-service + 0.0.1-SNAPSHOT + tasks-service + Tasks Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-artemis + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.flywaydb + flyway-core + - - com.h2database - h2 - runtime - + + com.h2database + h2 + runtime + org.apache.activemq artemis-jms-server - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java index f8e0d64c0c..dfd9859674 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TasksServiceApplication { - public static void main(String[] args) { - SpringApplication.run(TasksServiceApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(TasksServiceApplication.class, args); + } } diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java index ca13b2ee3d..4e01a649b1 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java @@ -13,10 +13,5 @@ package com.baeldung.tasksservice.adapters.http; import java.time.Instant; -public record TaskResponse(String id, - String title, - Instant created, - String createdBy, - String assignedTo, - String status) { +public record TaskResponse(String id, String title, Instant created, String createdBy, String assignedTo, String status) { } diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java index 6502e43883..a4145a2243 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java @@ -15,9 +15,6 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import com.baeldung.tasksservice.adapters.repository.TaskRecord; -import com.baeldung.tasksservice.service.TasksService; -import com.baeldung.tasksservice.service.UnknownTaskException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; @@ -32,6 +29,10 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.service.TasksService; +import com.baeldung.tasksservice.service.UnknownTaskException; + @RestController @RequestMapping("/") class TasksController { @@ -46,13 +47,12 @@ class TasksController { } @GetMapping - public List searchTasks(@RequestParam("status") Optional status, - @RequestParam("createdBy") Optional createdBy) { + public List searchTasks(@RequestParam("status") Optional status, @RequestParam("createdBy") Optional createdBy) { var tasks = tasksService.search(status, createdBy); return tasks.stream() - .map(this::buildResponse) - .collect(Collectors.toList()); + .map(this::buildResponse) + .collect(Collectors.toList()); } @GetMapping("/{id}") @@ -67,16 +67,14 @@ class TasksController { } @PatchMapping("/{id}") - public TaskResponse patchTask(@PathVariable("id") String id, - @RequestBody PatchTaskRequest body) { + public TaskResponse patchTask(@PathVariable("id") String id, @RequestBody PatchTaskRequest body) { var task = tasksService.updateTask(id, body.status(), body.assignedTo()); return buildResponse(task); } private TaskResponse buildResponse(final TaskRecord task) { - return new TaskResponse(task.getId(), task.getTitle(), task.getCreated(), task.getCreatedBy(), - task.getAssignedTo(), task.getStatus()); + return new TaskResponse(task.getId(), task.getTitle(), task.getCreated(), task.getCreatedBy(), task.getAssignedTo(), task.getStatus()); } @ExceptionHandler(UnknownTaskException.class) diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java index d5a705f56f..c380a16acc 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java @@ -1,8 +1,8 @@ package com.baeldung.tasksservice.adapters.jms; -import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; +import org.springframework.stereotype.Service; import com.baeldung.tasksservice.service.DeletedUserService; diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java index 6646258c22..dee3017a59 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java @@ -11,11 +11,12 @@ package com.baeldung.tasksservice.adapters.repository; +import java.time.Instant; + import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; -import java.time.Instant; @Entity @Table(name = "tasks") @@ -32,8 +33,7 @@ public class TaskRecord { private String assignedTo; private String status; - public TaskRecord(final String id, final String title, final Instant created, final String createdBy, - final String assignedTo, final String status) { + public TaskRecord(final String id, final String title, final Instant created, final String createdBy, final String assignedTo, final String status) { this.id = id; this.title = title; this.created = created; diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java index 50d35d3f93..fa0c3572fb 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java @@ -2,26 +2,26 @@ package com.baeldung.tasksservice.service; import javax.transaction.Transactional; -import com.baeldung.tasksservice.adapters.repository.TaskRecord; -import com.baeldung.tasksservice.adapters.repository.TasksRepository; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.adapters.repository.TasksRepository; + @Service public class DeletedUserService { - @Autowired - private TasksRepository tasksRepository; + @Autowired + private TasksRepository tasksRepository; - @Transactional - public void handleDeletedUser(String user) { - var ownedByUser = tasksRepository.findByCreatedBy(user); - tasksRepository.deleteAll(ownedByUser); + @Transactional + public void handleDeletedUser(String user) { + var ownedByUser = tasksRepository.findByCreatedBy(user); + tasksRepository.deleteAll(ownedByUser); - var assignedToUser = tasksRepository.findByAssignedTo(user); - for (TaskRecord record : assignedToUser) { - record.setAssignedTo(null); - record.setStatus("PENDING"); + var assignedToUser = tasksRepository.findByAssignedTo(user); + for (TaskRecord record : assignedToUser) { + record.setAssignedTo(null); + record.setStatus("PENDING"); + } } - } } diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java index e9ba1a9f70..3539dbbc3c 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java @@ -11,29 +11,33 @@ package com.baeldung.tasksservice.service; -import javax.transaction.Transactional; import java.time.Instant; import java.util.List; import java.util.Optional; import java.util.UUID; -import com.baeldung.tasksservice.adapters.repository.TaskRecord; -import com.baeldung.tasksservice.adapters.repository.TasksRepository; +import javax.transaction.Transactional; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.adapters.repository.TasksRepository; + @Service public class TasksService { @Autowired private TasksRepository tasksRepository; public TaskRecord getTaskById(String id) { - return tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + return tasksRepository.findById(id) + .orElseThrow(() -> new UnknownTaskException(id)); } @Transactional public void deleteTaskById(String id) { - var task = tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + var task = tasksRepository.findById(id) + .orElseThrow(() -> new UnknownTaskException(id)); tasksRepository.delete(task); } @@ -51,7 +55,8 @@ public class TasksService { @Transactional public TaskRecord updateTask(String id, Optional newStatus, Optional newAssignedTo) { - var task = tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + var task = tasksRepository.findById(id) + .orElseThrow(() -> new UnknownTaskException(id)); newStatus.ifPresent(task::setStatus); newAssignedTo.ifPresent(task::setAssignedTo); @@ -60,12 +65,8 @@ public class TasksService { } public TaskRecord createTask(String title, String createdBy) { - var task = new TaskRecord(UUID.randomUUID().toString(), - title, - Instant.now(), - createdBy, - null, - "PENDING"); + var task = new TaskRecord(UUID.randomUUID() + .toString(), title, Instant.now(), createdBy, null, "PENDING"); tasksRepository.save(task); return task; } diff --git a/lightrun/tasks-service/src/main/resources/application.properties b/lightrun/tasks-service/src/main/resources/application.properties index 83bbf1a1b8..88326ed10e 100644 --- a/lightrun/tasks-service/src/main/resources/application.properties +++ b/lightrun/tasks-service/src/main/resources/application.properties @@ -1,13 +1,9 @@ server.port=8082 - spring.artemis.mode=EMBEDDED spring.artemis.host=localhost spring.artemis.port=61616 - spring.artemis.embedded.enabled=true - spring.jms.template.default-destination=my-queue-1 - logging.level.org.apache.activemq.audit.base=WARN logging.level.org.apache.activemq.audit.message=WARN diff --git a/lightrun/users-service/README.md b/lightrun/users-service/README.md index 7c713e6638..e7faae858f 100644 --- a/lightrun/users-service/README.md +++ b/lightrun/users-service/README.md @@ -3,16 +3,21 @@ This application exists as an example for the Lightrun series of articles. ## Building + This application requires [Apache Maven](https://maven.apache.org/) and [Java 17+](https://www.oracle.com/java/technologies/downloads/). It does use the Maven Wrapper, so it can be built with only Java available on the path. As such, building the code is done by executing: + ``` $ ./mvnw install ``` + from the top level. ## Running + The application consists of three services: + * Tasks * Users * API @@ -23,7 +28,9 @@ The Tasks and Users services exist as microservices for managing one facet of da This does mean that the startup order is important. The JMS queue exists within the Tasks service and is connected to from the Users service. As such, the Tasks service must be started before the others. -Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for example: +Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for +example: + ``` $ java -jar ./target/tasks-service-0.0.1-SNAPSHOT.jar ``` diff --git a/lightrun/users-service/pom.xml b/lightrun/users-service/pom.xml index a961e4093b..63596ed67b 100644 --- a/lightrun/users-service/pom.xml +++ b/lightrun/users-service/pom.xml @@ -1,63 +1,63 @@ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.7 - - - com.baeldung - users-service - 0.0.1-SNAPSHOT - users-service - Users Service for LightRun Article - - 17 - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-artemis - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - org.flywaydb - flyway-core - + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + users-service + 0.0.1-SNAPSHOT + users-service + Users Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-artemis + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.flywaydb + flyway-core + - - com.h2database - h2 - runtime - + + com.h2database + h2 + runtime + - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java index 487e8de45b..3910960282 100644 --- a/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class UsersServiceApplication { - public static void main(String[] args) { - SpringApplication.run(UsersServiceApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(UsersServiceApplication.class, args); + } } diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java index b9a20c415b..e74ede65d5 100644 --- a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java @@ -11,6 +11,5 @@ package com.baeldung.usersservice.adapters.http; -public record UserResponse(String id, - String name) { +public record UserResponse(String id, String name) { } diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java index a5138e31d6..795d240fe9 100644 --- a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java @@ -11,9 +11,6 @@ package com.baeldung.usersservice.adapters.http; -import com.baeldung.usersservice.adapters.repository.UserRecord; -import com.baeldung.usersservice.service.UsersService; -import com.baeldung.usersservice.service.UnknownUserException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; @@ -27,6 +24,10 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import com.baeldung.usersservice.adapters.repository.UserRecord; +import com.baeldung.usersservice.service.UnknownUserException; +import com.baeldung.usersservice.service.UsersService; + @RestController @RequestMapping("/") class UsersController { @@ -50,14 +51,14 @@ class UsersController { var user = usersService.createUser(body.name()); return buildResponse(user); } - + @PatchMapping("/{id}") - public UserResponse patchUser(@PathVariable("id") String id, - @RequestBody PatchUserRequest body) { + public UserResponse patchUser(@PathVariable("id") String id, @RequestBody PatchUserRequest body) { var user = usersService.updateUser(id, body.name()); return buildResponse(user); } + private UserResponse buildResponse(final UserRecord user) { return new UserResponse(user.getId(), user.getName()); } diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java index 46954b1ee0..5115a5a77b 100644 --- a/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java @@ -11,18 +11,17 @@ package com.baeldung.usersservice.service; -import javax.transaction.Transactional; - -import java.time.Instant; import java.util.Optional; import java.util.UUID; +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + import com.baeldung.usersservice.adapters.jms.JmsSender; import com.baeldung.usersservice.adapters.repository.UserRecord; import com.baeldung.usersservice.adapters.repository.UsersRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jms.core.JmsTemplate; -import org.springframework.stereotype.Service; @Service public class UsersService { @@ -33,12 +32,14 @@ public class UsersService { private JmsSender jmsSender; public UserRecord getUserById(String id) { - return usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + return usersRepository.findById(id) + .orElseThrow(() -> new UnknownUserException(id)); } @Transactional public void deleteUserById(String id) { - var user = usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + var user = usersRepository.findById(id) + .orElseThrow(() -> new UnknownUserException(id)); usersRepository.delete(user); jmsSender.sendDeleteUserMessage(id); @@ -46,7 +47,8 @@ public class UsersService { @Transactional public UserRecord updateUser(String id, Optional newName) { - var user = usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + var user = usersRepository.findById(id) + .orElseThrow(() -> new UnknownUserException(id)); newName.ifPresent(user::setName); @@ -54,7 +56,8 @@ public class UsersService { } public UserRecord createUser(String name) { - var user = new UserRecord(UUID.randomUUID().toString(), name); + var user = new UserRecord(UUID.randomUUID() + .toString(), name); usersRepository.save(user); return user; } diff --git a/lightrun/users-service/src/main/resources/application.properties b/lightrun/users-service/src/main/resources/application.properties index 8cc8f67d92..616131c42e 100644 --- a/lightrun/users-service/src/main/resources/application.properties +++ b/lightrun/users-service/src/main/resources/application.properties @@ -1,10 +1,7 @@ server.port=8081 - spring.artemis.host=localhost spring.artemis.port=61616 - spring.jms.template.default-destination=my-queue-1 - logging.level.org.apache.activemq.audit.base=WARN logging.level.org.apache.activemq.audit.message=WARN