Eucalyptus is a high-performance, asynchronous client-server framework built on Netty for efficient data processing and communication.
- Asynchronous Communication: Send requests and receive responses asynchronously
- Multithreaded Processing: Efficient multithreaded processing of client requests
- Flexible Data Model: Send and receive any Java objects (serialized as JSON)
- Robust Error Handling: Comprehensive error handling and logging
- Simple API: Easy-to-use client and server APIs
- Auto-Reconnect: Automatic reconnection to the server if the connection is lost
- Scalable Architecture: Designed to handle many concurrent connections
Eucalyptus consists of two main components:
- EucalyptusServer: A multithreaded Netty-based server that can process client requests efficiently
- EucalyptusClient: A non-blocking client that can send requests to the server and receive responses asynchronously
Add the following to your build.gradle file:
dependencies {
implementation 'org.example:eucalyptus:1.0-SNAPSHOT'
}Add the following to your pom.xml file:
<dependency>
<groupId>org.example</groupId>
<artifactId>eucalyptus</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>-
Clone the repository:
git clone https://github.com/yourusername/eucalyptus.git -
Build the project:
cd eucalyptus ./gradlew build -
Add the generated JAR file to your project's classpath.
// Create and start the server on port 8080
EucalyptusServer server = new EucalyptusServer(8080);
server.start();
// Add shutdown hook to stop the server when the JVM shuts down
Runtime.getRuntime().addShutdownHook(new Thread(server::stop));// Create and connect the client
EucalyptusClient client = new EucalyptusClient("localhost", 8080);
client.connect();
try {
// Send a request and get a CompletableFuture for the response
List<Object> numbers = Arrays.asList(1, 2, 3, 4, 5);
CompletableFuture<DataPacket> future = client.sendRequest("CALCULATE_SUM", numbers);
// Process the response when it arrives
future.thenAccept(response -> {
System.out.println("Sum: " + response.getData().get(0));
});
// Or wait for the response synchronously
DataPacket response = future.get();
System.out.println("Sum: " + response.getData().get(0));
// Send a one-way message (no response expected)
client.sendOneWayMessage("LOG", Arrays.asList("This is a log message"));
} finally {
// Disconnect the client when done
client.disconnect();
}The server currently supports the following operations:
- CALCULATE_SUM: Calculate the sum of a list of numbers
- CALCULATE_AVERAGE: Calculate the average of a list of numbers
- UPPERCASE: Convert a list of strings to uppercase
You can easily extend the server to support additional operations by modifying the processPacket method in the ServerHandler class.
To implement custom data processing on the server:
- Modify the
processPacketmethod inServerHandler.java - Add your custom operation to the switch statement
- Process the data as needed
- Return a response packet with the results
Example:
case "MY_CUSTOM_OPERATION":
// Process the data
List<Object> resultData = new ArrayList<>();
// ... custom processing logic ...
resultData.add(result);
break;Eucalyptus client provides an auto-reconnect feature that automatically attempts to reconnect to the server if the connection is lost.
// Create and connect the client
EucalyptusClient client = new EucalyptusClient("localhost", 8080);
client.connect();
// Enable auto-reconnect (enabled by default)
client.setAutoReconnect(true);
// Check if auto-reconnect is enabled
boolean isAutoReconnectEnabled = client.isAutoReconnectEnabled();When auto-reconnect is enabled, the client will:
- Detect connection loss automatically
- Attempt to reconnect every 5 seconds
- Continue reconnection attempts for up to 1 minute (configurable)
- Automatically restore the connection when the server becomes available again
The project includes several example applications that demonstrate how to use Eucalyptus:
EucalyptusExample.java demonstrates basic client-server communication, including:
- Starting a server
- Connecting a client
- Sending requests and receiving responses
- Sending one-way messages
AutoReconnectExample.java demonstrates the auto-reconnect feature:
- Connecting to a server with auto-reconnect enabled
- Handling server disconnection and reconnection
- Continuing operation after reconnection
The main client class for communicating with the server.
// Create a client for the specified host and port
EucalyptusClient(String host, int port)// Connect to the server
void connect() throws Exception
// Disconnect from the server
void disconnect()
// Check if connected to the server
boolean isConnected()
// Enable or disable auto-reconnect
void setAutoReconnect(boolean enable)
// Check if auto-reconnect is enabled
boolean isAutoReconnectEnabled()
// Send a request and get a future for the response
CompletableFuture<DataPacket> sendRequest(String operation, List<Object> data) throws Exception
// Send a one-way message (no response expected)
void sendOneWayMessage(String operation, List<Object> data) throws ExceptionThe main server class for processing client requests.
// Create a server with the specified port and default thread count
EucalyptusServer(int port)
// Create a server with the specified port and thread count
EucalyptusServer(int port, int processingThreads)// Start the server
void start() throws Exception
// Stop the server
void stop()
// Check if the server is running
boolean isRunning()The data model class for communication between client and server.
// Create a new data packet
DataPacket(String operation, List<Object> data, boolean requiresResponse)
// Get/set the packet ID
UUID getId()
void setId(UUID id)
// Get/set the operation
String getOperation()
void setOperation(String operation)
// Get/set the data
List<Object> getData()
void setData(List<Object> data)
// Get/set whether the packet requires a response
boolean isRequiresResponse()
void setRequiresResponse(boolean requiresResponse)- Host and Port: Specify the server host and port when creating the client
- Auto-Reconnect: Enable or disable automatic reconnection to the server
- Reconnect Interval: Fixed at 5 seconds between reconnection attempts
- Max Reconnect Attempts: Fixed at 12 attempts (1 minute total)
- Port: Specify the port to listen on when creating the server
- Processing Threads: Specify the number of threads to use for processing requests (defaults to available processors)
- Java 8 or higher
- Netty 4.1.x
- Jackson 2.x for JSON serialization/deserialization
- SLF4J and Logback for logging
Contributions are welcome! Here's how you can contribute:
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature/my-new-feature - Submit a pull request
- Follow Java coding conventions
- Write unit tests for new features
- Update documentation for changes
This project is licensed under the MIT License - see the LICENSE file for details.