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.
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
Performs basic arithmetic operations (ADD, SUBTRACT, MULTIPLY, DIVIDE) on two numbers.
Processes text messages and returns enriched information including timestamp, length, and uppercase transformation.
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
- Client creates a request and generates a unique correlation ID
- Request is published to
thrift-exchangewith appropriate routing key - Server consumes request from its queue
- Server processes request using Thrift processor
- Server sends response back to client's reply queue with matching correlation ID
- Client receives response and returns result
- Java 11 or higher
- Maven 3.6+
- Docker and Docker Compose (for RabbitMQ)
docker-compose up -dThis will start RabbitMQ with:
- AMQP port: 5672
- Management UI: http://localhost:15672
- Credentials: admin/admin
mvn clean installIn one terminal:
Mac/Linux/Git Bash:
./run-server.shWindows PowerShell:
.\run-server.ps1Mac/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.ServiceBMainIn another terminal:
Mac/Linux/Git Bash:
./run-client.shWindows PowerShell:
.\run-client.ps1Mac/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.ServiceAMainOnce Service A is running, you'll see an interactive CLI. Available commands:
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
> 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)
> quit
- 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.
If you have the Thrift compiler installed and want to regenerate the Java classes:
Mac:
brew install thriftWindows:
choco install thriftLinux (Ubuntu/Debian):
sudo apt-get install thrift-compilerMac/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.thriftWindows 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.thriftNote: The generated code is already included in the project, so this step is optional.
- 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
- 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
Ensure RabbitMQ is running:
docker-compose psCheck that Service B is running and consuming from the correct queues. Verify in RabbitMQ Management UI.
Ensure Java 11+ and Maven 3.6+ are installed:
java -version
mvn -version- Stop Service A and Service B (Ctrl+C in their terminals)
- Stop RabbitMQ:
docker-compose downTo also remove the RabbitMQ data volume:
docker-compose down -vThis project is provided as-is for demonstration purposes.