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
+8 -1
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
```
+57 -57
View File
@@ -1,63 +1,63 @@
<?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"
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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>users-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>users-service</name>
<description>Users Service for LightRun Article</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>users-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>users-service</name>
<description>Users Service for LightRun Article</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -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);
}
}
@@ -11,6 +11,5 @@
package com.baeldung.usersservice.adapters.http;
public record UserResponse(String id,
String name) {
public record UserResponse(String id, String name) {
}
@@ -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());
}
@@ -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;
}
@@ -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