Skip to content

cearps/thriftRabbitMQTest

Repository files navigation

Thrift RPC over RabbitMQ Demo

This project demonstrates two Java services communicating via Apache Thrift RPC over RabbitMQ message broker. It showcases a custom transport layer that enables RPC-style request-response communication using AMQP.

This project is mostly AI generated for the purpose of quickly bootstrapping a project to test Thrift RPC over RabbitMQ.

Project Structure

thriftRabbitMQTest/
├── pom.xml                      # Parent Maven configuration
├── docker-compose.yml           # RabbitMQ setup
├── thrift-definitions/
│   └── service.thrift          # Thrift service definitions
├── service-a/                   # Client module
│   └── src/main/java/com/example/thrift/
│       ├── client/             # CLI client implementation
│       ├── transport/          # Custom RabbitMQ transport
│       └── generated/          # Generated Thrift code
└── service-b/                   # Server module
    └── src/main/java/com/example/thrift/
        ├── server/             # Service implementations
        ├── transport/          # Custom RabbitMQ transport
        └── generated/          # Generated Thrift code

Services

CalculatorService

Performs basic arithmetic operations (ADD, SUBTRACT, MULTIPLY, DIVIDE) on two numbers.

MessageProcessor

Processes text messages and returns enriched information including timestamp, length, and uppercase transformation.

Architecture

Custom RabbitMQTransport

The RabbitMQTransport class extends Thrift's TTransport to work over AMQP:

  • Client Mode: Sends requests with correlation IDs to service queues and waits for responses on temporary reply queues
  • Server Mode: Receives requests from service queues and sends responses back to the client's reply-to queue
  • RPC Pattern: Uses direct exchange with routing keys for service addressing
  • Correlation IDs: Ensures responses are matched to their corresponding requests

Message Flow

  1. Client creates a request and generates a unique correlation ID
  2. Request is published to thrift-exchange with appropriate routing key
  3. Server consumes request from its queue
  4. Server processes request using Thrift processor
  5. Server sends response back to client's reply queue with matching correlation ID
  6. Client receives response and returns result

Prerequisites

  • Java 11 or higher
  • Maven 3.6+
  • Docker and Docker Compose (for RabbitMQ)

Setup Instructions

1. Start RabbitMQ

docker-compose up -d

This will start RabbitMQ with:

2. Build the Project

mvn clean install

3. Run Service B (Server)

In one terminal:

Quick Start - Using Helper Scripts:

Mac/Linux/Git Bash:

./run-server.sh

Windows PowerShell:

.\run-server.ps1

Manual Commands:

Mac/Linux/Git Bash:

cd service-b
mvn exec:java -Dexec.mainClass="com.example.thrift.server.ServiceBMain"

Windows PowerShell:

cd service-b
mvn exec:java '-Dexec.mainClass=com.example.thrift.server.ServiceBMain'

Windows Command Prompt (cmd):

cd service-b
mvn exec:java -Dexec.mainClass=com.example.thrift.server.ServiceBMain

4. Run Service A (Client)

In another terminal:

Quick Start - Using Helper Scripts:

Mac/Linux/Git Bash:

./run-client.sh

Windows PowerShell:

.\run-client.ps1

Manual Commands:

Mac/Linux/Git Bash:

cd service-a
mvn exec:java -Dexec.mainClass="com.example.thrift.client.ServiceAMain"

Windows PowerShell:

cd service-a
mvn exec:java '-Dexec.mainClass=com.example.thrift.client.ServiceAMain'

Windows Command Prompt (cmd):

cd service-a
mvn exec:java -Dexec.mainClass=com.example.thrift.client.ServiceAMain

Usage

Once Service A is running, you'll see an interactive CLI. Available commands:

Calculate Operation

Important: Use operation names (ADD, SUBTRACT, MULTIPLY, DIVIDE), not symbols (+, -, *, /)

> calc 10 ADD 5
Result: 15.0

> calc 100 SUBTRACT 25
Result: 75.0

> calc 100 DIVIDE 4
Result: 25.0

> calc 7 MULTIPLY 8
Result: 56.0

Process Message

> msg Hello World
Response: [2025-12-03 10:30:45] Processed: Hello World (length: 11, uppercase: HELLO WORLD)

> msg Testing the message processor
Response: [2025-12-03 10:31:12] Processed: Testing the message processor (length: 29, uppercase: TESTING THE MESSAGE PROCESSOR)

Exit

> quit

RabbitMQ Configuration

  • Exchange: thrift-exchange (direct)
  • Calculator Queue: calculator
  • Message Processor Queue: message-processor
  • Reply Queues: Temporary, auto-deleted queues created per client session

You can monitor the message flow in the RabbitMQ Management UI at http://localhost:15672.

Regenerating Thrift Code (Optional)

If you have the Thrift compiler installed and want to regenerate the Java classes:

Install Thrift Compiler

Mac:

brew install thrift

Windows:

choco install thrift

Linux (Ubuntu/Debian):

sudo apt-get install thrift-compiler

Generate Java Code

Mac/Linux/Git Bash:

thrift --gen java -out service-a/src/main/java thrift-definitions/service.thrift
thrift --gen java -out service-b/src/main/java thrift-definitions/service.thrift

Windows PowerShell/CMD:

thrift --gen java -out service-a\src\main\java thrift-definitions\service.thrift
thrift --gen java -out service-b\src\main\java thrift-definitions\service.thrift

Note: The generated code is already included in the project, so this step is optional.

Dependencies

  • Apache Thrift: 0.19.0 - RPC framework
  • RabbitMQ Java Client: 5.20.0 - AMQP client
  • SLF4J: 2.0.9 - Logging facade
  • Logback: 1.4.11 - Logging implementation

Key Features

  • RPC over Message Queue: Traditional RPC semantics using asynchronous message passing
  • Correlation IDs: Request-response matching without connection state
  • Service Discovery: Direct exchange routing enables service addressing by name
  • Transparent Transport: Services use standard Thrift clients/processors without modification
  • Fault Isolation: Message broker provides decoupling and buffering
  • Scalability: Multiple server instances can consume from the same queue

Troubleshooting

Connection Refused

Ensure RabbitMQ is running:

docker-compose ps

Timeout Errors

Check that Service B is running and consuming from the correct queues. Verify in RabbitMQ Management UI.

Build Errors

Ensure Java 11+ and Maven 3.6+ are installed:

java -version
mvn -version

Stopping the Services

  1. Stop Service A and Service B (Ctrl+C in their terminals)
  2. Stop RabbitMQ:
docker-compose down

To also remove the RabbitMQ data volume:

docker-compose down -v

License

This project is provided as-is for demonstration purposes.

About

Repository for testing out RPC communication over a message queue between two Java services

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages