Skip to content

josephrodriguez/mediator-spring-boot-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Library for Mediator pattern

GitHub release (latest by date) GitHub Workflow Status (branch) codecov Quality Gate Status Maintainability Rating Reliability Rating Security Rating

Main Purpose

This Spring Boot library provide an implementation for the behavioral pattern Mediator.

Getting Started

The library is a Maven Artifact published on GitHub Package Registry. Check the package versions list here.

Dependency

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

Install the package with Maven running the command line.

$ mvn install

Usage

Annotation

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

Request

Lets define the class that

EchoRequest.java

@AllArgsConstructor
public class EchoRequest implements Request<EchoResponse> {
    private final String message;
}

Response

EchoResponse.java

@Data
@AllArgsConstructor
public class EchoResponse {
    private final String message;
}

Handler

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());
    }
}

Mediator in action

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 support

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 2

  •  
  •  

Languages