Reformatted code

This commit is contained in:
Graham Cox 2022-05-19 10:14:05 +01:00
parent 177f912393
commit 762d5f0f56
27 changed files with 278 additions and 283 deletions

View File

@ -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
```

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>

View File

@ -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);

View File

@ -9,9 +9,9 @@ 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);
})

View File

@ -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) {
}

View File

@ -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,12 +33,7 @@ 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) {
@ -47,6 +48,7 @@ public class TasksController {
return new UserResponse(user.id(), user.name());
}
@ExceptionHandler(UnknownTaskException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleUnknownTask() {

View File

@ -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) {
}

View File

@ -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;

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baelduung</groupId>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>

View File

@ -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) {
}

View File

@ -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,8 +47,7 @@ class TasksController {
}
@GetMapping
public List<TaskResponse> searchTasks(@RequestParam("status") Optional<String> status,
@RequestParam("createdBy") Optional<String> createdBy) {
public List<TaskResponse> searchTasks(@RequestParam("status") Optional<String> status, @RequestParam("createdBy") Optional<String> createdBy) {
var tasks = tasksService.search(status, createdBy);
return tasks.stream()
@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -2,12 +2,12 @@ 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

View File

@ -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<String> newStatus, Optional<String> 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;
}

View File

@ -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

View File

@ -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
```

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>

View File

@ -11,6 +11,5 @@
package com.baeldung.usersservice.adapters.http;
public record UserResponse(String id,
String name) {
public record UserResponse(String id, String name) {
}

View File

@ -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 {
@ -52,12 +53,12 @@ class UsersController {
}
@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());
}

View File

@ -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<String> 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;
}

View File

@ -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