Skip to content

levy-tech-spark/ReliableRMQ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ReliableRMQ

📝 Overview

ReliableRMQ is a Spring Boot framework for implementing distributed transactions using reliable messaging with RabbitMQ. It provides a simple, annotation-based approach to handle distributed transactions with automatic rollback support.

🚀 Features

  • Spring Boot Integration - Seamless integration with Spring Boot applications through starter dependency
  • Simple API - Easy-to-use wrapper for RabbitMQ operations
  • Annotation-Based Transactions - Implement distributed transactions with simple annotations
  • Automatic Rollback - Full support for exception handling and transaction rollback
  • Configurable Persistence - Redis-based message persistence with customization options
  • Production Ready - Battle-tested in real-world applications

🧠 Understanding Reliable Messaging Transactions

ReliableRMQ implements a two-phase messaging pattern to ensure transaction consistency across distributed services:

  1. Prepare Phase: When a transaction begins, the message is first persisted locally (in Redis by default) in a "Prepared" state before any business logic executes.

  2. Local Transaction: The originating service executes its local transaction.

  3. Ready Phase: If the local transaction succeeds, the message transitions to "Ready" state, making it eligible for delivery.

  4. Message Delivery: A daemon process monitors and delivers "Ready" messages to the message broker.

  5. Confirmation: After successful message delivery, the system confirms completion by removing the message from local storage.

  6. Automatic Recovery: The system handles failures at any stage through timeout mechanisms and retry logic:

    • Messages stuck in "Prepared" state are rolled back
    • Messages in "Ready" state that fail to deliver are retried
    • End-to-end acknowledgments ensure transaction completion

This approach solves the distributed transaction problem without complex protocols like 2PC or XA transactions, offering better performance while maintaining reliability.

🔍 How It Works

ReliableRMQ implements the reliable messaging pattern for distributed transactions:

reliable-rmq-flow

The framework handles all the complexity of message coordination, persistence, and delivery confirmations, allowing you to focus on your business logic.

📦 Installation

Add the dependency to your Maven project:

<dependency>
    <groupId>com.levytech</groupId>
    <artifactId>reliable-rmq</artifactId>
    <version>1.0.1</version>
</dependency>

⚙️ Configuration

Distributed Transaction Settings

/**
 * Whether to enable distributed transactions (default: false)
 */
private boolean transaction = false;

/**
 * Maximum retries for commit acknowledgment failures
 */
private Integer commitMaxRetries = 3;

/**
 * Maximum retries for message receipt acknowledgment failures
 */
private Integer receiveMaxRetries = 3;

/**
 * Whether to use Redis for message persistence
 * You can implement your own {@link top.arkstack.shine.mq.coordinator.Coordinator}
 * or disable Redis dependency by setting this to false
 */
private boolean redisPersistence = true;

/**
 * Redis cache key prefix
 */
private String redisPrefix = "";

/**
 * Timeout for messages in Prepare/Ready states (in seconds, default: 3 minutes)
 */
private long timeOut = 3 * 60;

/**
 * TTL for returnCallback status (in seconds, default: 1 day)
 */
private long returnCallbackTTL = 24 * 60 * 60;

RabbitMQ Settings

/**
 * Whether to initialize message listeners (disable if service is Producer-only)
 */
private boolean listenerEnable = false;

/**
 * Acknowledgment mode:
 * 0 - AUTO
 * 1 - MANUAL
 * 2 - NONE
 */
private int acknowledgeMode = 1;

/**
 * Maximum unacknowledged messages per consumer
 */
private Integer prefetchCount = null;

/**
 * Number of consumers per queue
 */
private Integer consumersPerQueue = null;

/**
 * Whether messages persist after broker restart
 */
private boolean durable = true;

/**
 * Whether queue is exclusive to the declaring connection
 */
private boolean exclusive = false;

/**
 * Whether queue is deleted when connection closes
 */
private boolean autoDelete = false;

/**
 * Channel cache size
 */
private Integer channelCacheSize = null;

👤 Author

Levy Hu - levy-tech-spark

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

⭐ Support

If you find this project helpful, please consider giving it a star!

About

Spring Boot framework for implementing distributed transactions using reliable messaging with RabbitMQ

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages