Skip to content

๐Ÿ› ๏ธ A lightweight library for handling AMQP messaging in Java with AOP (Aspect-Oriented Programming).

License

Notifications You must be signed in to change notification settings

DashioDevs/aspect-amqp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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:

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

๐Ÿ› ๏ธ Usage

Create a @PublishMessage Annotation

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

Adding @MessageMapping

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

Fancy expressions

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

๐Ÿ“– 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.

About

๐Ÿ› ๏ธ A lightweight library for handling AMQP messaging in Java with AOP (Aspect-Oriented Programming).

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages