An open API service indexing awesome lists of open source software.

https://github.com/dashiodevs/aspect-amqp

🛠️ A lightweight library for handling AMQP messaging in Java with AOP (Aspect-Oriented Programming).
https://github.com/dashiodevs/aspect-amqp

amqp aop aspect-oriented-programming aspectj java rabbitmq spring

Last synced: 5 months ago
JSON representation

🛠️ A lightweight library for handling AMQP messaging in Java with AOP (Aspect-Oriented Programming).

Awesome Lists containing this project

README

        

# Message Aspect for RabbitMQ 🐇📤

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.

---

## 📦 Installation

### Dependencies

To integrate the module into a Spring project, add the following dependencies to your `pom.xml` file:

```xml



org.springframework.boot
spring-boot-starter-amqp



org.springframework.boot
spring-boot-starter-aop



net.dashio
aspect-amqp

````

---

## 🛠️ Usage

### Create a @PublishMessage Annotation

Use the `@PublishMessage` annotation on a method to enable the RabbitMQ message sending process:

```java
@PublishMessage(
exchange = "your_exchange",
routingKey = "your_routing_key"
)
public YourReturnType yourMethod() {
return new YourReturnType();
}
```

### Adding @MessageMapping

Define a `@MessageMapping` child annotation to specify how the message should be transformed:

```java
@PublishMessage(
exchange = "your_exchange",
routingKey = "your_routing_key",
mapping = @MessageMapping(function = "#value.name", value = "value")
)
public YourReturnType yourMethod() {
return new YourReturnType();
}
```

### Fancy expressions

Somewhere you got:

```java
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;
}
}
```

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

```

---
## 📖 How the MessageAspect Works

- **`@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.

---

## 💡 Benefits

- **Automation**: No need to manually send messages to RabbitMQ.
- **Aspect-Oriented**: Use AOP (Aspect-Oriented Programming) to send messages after the method execution.

---

## 🔧 Extendability

- Add custom **message transformations** by configuring the `@MessageMapping` function.
- Extend the aspect to add additional logic before or after the message processing.