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.
- 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
ReliableRMQ implements a two-phase messaging pattern to ensure transaction consistency across distributed services:
-
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.
-
Local Transaction: The originating service executes its local transaction.
-
Ready Phase: If the local transaction succeeds, the message transitions to "Ready" state, making it eligible for delivery.
-
Message Delivery: A daemon process monitors and delivers "Ready" messages to the message broker.
-
Confirmation: After successful message delivery, the system confirms completion by removing the message from local storage.
-
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.
ReliableRMQ implements the reliable messaging pattern for distributed transactions:
The framework handles all the complexity of message coordination, persistence, and delivery confirmations, allowing you to focus on your business logic.
Add the dependency to your Maven project:
<dependency>
<groupId>com.levytech</groupId>
<artifactId>reliable-rmq</artifactId>
<version>1.0.1</version>
</dependency>
/**
* 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;
/**
* 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;
Levy Hu - levy-tech-spark
This project is licensed under the MIT License - see the LICENSE file for details.
If you find this project helpful, please consider giving it a star!