This Java project is a simple Thrift demo that aims to demonstrate how Thrift works. In this guide, we'll walk you through the process of generating the required Java code using Thrift and executing the project.
Before getting started, make sure you have the following prerequisites installed:
- Thrift compiler
- Maven
-
Open your terminal and navigate to the root folder of the project.
-
Run the following command to generate Java code from the Thrift definition file (
multi.thrift
):
thrift -r --gen java multi.thrift
This command will create the Java code in the gen-java
folder.
- After generating the code, you need to move it to the
src/org/example
directory. You can do this manually or by using the following command:
mv gen-java/* src/org/example/
Now that you have generated the required Java code, you can build and run the Thrift demo project.
- Execute the following command to clean the project:
mvn clean
- Next, build and package the project into a JAR file:
mvn install jar
- The project consists of two main classes:
-
MultiplicationServer: This class has a main method that triggers a thread to start the Thrift server.
-
MultiplicationClient: This class has a main method that connects to the server started in the previous step.
You can run these classes as follows:
- To start the server, run the
MultiplicationServer
class:
java -cp target/<your-jar-filename>.jar org.example.MultiplicationServer
- To run the client, execute the
MultiplicationClient
class:
java -cp target/<your-jar-filename>.jar org.example.MultiplicationClient
Replace <your-jar-filename>
with the actual JAR file name generated by Maven.
You have successfully generated the Thrift Java code, built the project, and executed the Thrift demo. This project showcases how to create a Thrift-based server and client in Java, demonstrating the fundamentals of Thrift RPC communication. Feel free to explore and modify the project as needed for your own use case.
Happy coding!