This Spring Boot library provide an implementation for the behavioral pattern Mediator.
The library is a Maven Artifact published on GitHub Package Registry. Check the package versions list here.
The library version dependency should be declared on pom.xml file.
<dependency>
<groupId>io.github.josephrodriguez</groupId>
<artifactId>mediator-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
Install the package with Maven running the command line.
$ mvn install
Auto-configuration feature is supported by the library using the annotation @EnableMediator
annotation in the Spring Boot Application.
import io.github.josephrodriguez.annotations.EnableMediator;
@EnableMediator
@SpringBootApplication
public class SpringBootStarterKitApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarterKitApplication.class, args);
}
}
Lets define the class that
EchoRequest.java
@AllArgsConstructor
public class EchoRequest implements Request<EchoResponse> {
private final String message;
}
EchoResponse.java
@Data
@AllArgsConstructor
public class EchoResponse {
private final String message;
}
Implement the RequestHandler
interfaces to handle the request object.
EchoRequestHandler.java
import io.github.josephrodriguez.interfaces.RequestHandler;
@Service
public class EchoRequestHandler implements RequestHandler<EchoRequest, EchoResponse> {
@Override
public EchoResponse handle(EchoRequest request) {
return new EchoResponse(request.getMessage());
}
}
Use the Mediator service with dependency injection.
import io.github.josephrodriguez.Mediator;
@RestController
public class EchoController {
private Mediator mediator;
public EchoController(Mediator mediator) {
this.mediator = mediator;
}
@RequestMapping("/echo")
public ResponseEntity<EchoResponse> echo() throws UnsupportedRequestException {
EchoRequest request = new EchoRequest(UUID.randomUUID().toString());
EchoResponse response = mediator.send(request);
return ResponseEntity
.ok()
.body(response);
}
}
Asynchronous operations are supported using the CompletableFuture<?>
class.
EchoRequest request = new EchoRequest("Hi Mediator");
CompletableFuture<EchoResponse> future = mediator.sendAsync(request);
EchoResponse response = response.join();
DateTimeEvent event = new DateTimeEvent();
CompletableFuture<Void> future = mediator.publishAsync(event);