Simple in-process mediator implementation.
Requires Java 17
jmediator attempts to decouple the in-process sending of messages from how they are handled.
First, create a message by creating a class that implements the Request
interface.
The Request
interface handles both commands and queries.
public class HelloRequest implements Request {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Message{" +
"payload='" + name + '\'' +
'}';
}
}
Next, lets create the corresponding handler
@Named
public class HelloRequestHandler implements RequestHandler<HelloRequest, String> {
@Override
public String handle(HelloRequest request) {
return "Hello " + request.getName();
}
}
Finally, we can send a message through the mediator
String response dispatcher.send(new HelloRequest("Sean"));
System.out.println(response); // prints "Hello Sean"
This project was inspired by Jimmy Bogard's Mediator project