This module provides a MessageAspect
that allows automatic sending of messages to RabbitMQ after a method execution. It is based on Spring AOP and uses Testcontainers for integration tests with a real RabbitMQ instance.
To integrate the module into a Spring project, add the following dependencies to your pom.xml
file:
<dependencies>
<!-- Spring Boot Starter for RabbitMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- Spring AOP for Aspect-Oriented Programming -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- The aspect self -->
<dependency>
<groupId>net.dashio</groupId>
<artifactId>aspect-amqp</artifactId>
</dependency>
</dependencies>
Use the @PublishMessage
annotation on a method to enable the RabbitMQ message sending process:
@PublishMessage(
exchange = "your_exchange",
routingKey = "your_routing_key"
)
public YourReturnType yourMethod() {
return new YourReturnType();
}
Define a @MessageMapping
child annotation to specify how the message should be transformed:
@PublishMessage(
exchange = "your_exchange",
routingKey = "your_routing_key",
mapping = @MessageMapping(function = "#value.name", value = "value")
)
public YourReturnType yourMethod() {
return new YourReturnType();
}
Somewhere you got:
import java.util.UUID;
public record User(UUID id, String name) {
//Your methods
}
public class Utils {
public String concatUser(User user) {
return user.id + ":" + user.name;
}
}
@PublishMessage(
exchange = "your_exchange",
routingKey = "your_routing_key",
mapping = @MessageMapping(function = "T(your.package.Utils).concatUser(#value)", value = "value")
)
public YourReturnType yourMethod() {
return new YourReturnType();
}
@PublishMessage
: Used to mark methods that should send a message to RabbitMQ.@MessageMapping
: Defines how data within the object should be processed for RabbitMQ message transfer.
- Automation: No need to manually send messages to RabbitMQ.
- Aspect-Oriented: Use AOP (Aspect-Oriented Programming) to send messages after the method execution.
- Add custom message transformations by configuring the
@MessageMapping
function. - Extend the aspect to add additional logic before or after the message processing.