Inqwise Async bridges standard Java blocking I/O structures with Vert.x's asynchronous, non-blocking APIs. By providing robust wrappers for traditional Java classes such as InputStream, OutputStream, Reader, Writer, and more, Inqwise Async enables developers to integrate existing Java codebases into Vert.x-based reactive applications without disrupting the event loop. This bridge ensures efficient and scalable I/O operations by managing back-pressure and flow control, facilitating the modernization of legacy systems for high-performance, event-driven environments.
Inqwise Async is a Java library designed to seamlessly integrate standard blocking I/O operations with Vert.x's non-blocking, asynchronous framework. It provides wrappers for Java's traditional I/O classes, allowing developers to incorporate existing blocking code into reactive applications without compromising the performance and scalability that Vert.x offers. This integration is crucial for modernizing legacy systems and ensuring efficient resource management in high-throughput environments.
- Seamless Integration: Wraps standard Java I/O classes (
InputStream,OutputStream,Reader,Writer) as Vert.xReadStreamandWriteStream. - Non-Blocking Operations: Ensures that blocking I/O does not hinder the Vert.x event loop, maintaining application responsiveness.
- Back-Pressure Management: Handles flow control to prevent overwhelming the system with data.
- Thread-Safe Execution: Utilizes
ExecutorServiceto manage blocking operations in separate threads. - Easy to Use API: Provides intuitive methods for wrapping and interacting with I/O streams.
- Extensible Design: Can be extended to support additional I/O classes and functionalities as needed.
To include the Inqwise Async library in your Maven project, add the following dependency to your pom.xml file:
<dependency>
<groupId>com.inqwise</groupId>
<artifactId>inqwise-async</artifactId>
<version>${latest.version}</version>
</dependency>Ensure that your project's repositories are correctly configured. If the library is available on Maven Central, no additional repository configuration is required.
Note: Replace the version number with the latest version if a newer one is available.
Inqwise Async provides straightforward methods to wrap Java's blocking I/O streams into Vert.x's non-blocking streams. Below is a guide on how to utilize these wrappers effectively.
The stream package within Inqwise Async offers classes that encapsulate Java's I/O streams and expose them as Vert.x ReadStream and WriteStream. This allows for asynchronous data processing and integration with Vert.x's reactive pipelines.
AsyncReadStream: Wraps a standardInputStreamand exposes it as a Vert.xReadStream<Buffer>.AsyncWriteStream: Wraps a standardOutputStreamand exposes it as a Vert.xWriteStream<Buffer>.
These classes manage the execution of blocking operations using a dedicated ExecutorService, ensuring that the Vert.x event loop remains unblocked.
When chaining stream configuration methods, register exceptionHandler(...) and endHandler(...) before invoking handler(...) so that error and completion logic is ready before data emission begins.
import com.inqwise.async.stream.AsyncReadStream;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.streams.ReadStream;
import java.io.FileInputStream;
import java.io.InputStream;
public class ReadStreamExample {
public static void main(String[] args) throws Exception {
Vertx vertx = Vertx.vertx();
InputStream inputStream = new FileInputStream("example.txt");
ReadStream<Buffer> readStream = new AsyncReadStream(vertx, inputStream);
readStream
.exceptionHandler(err -> {
System.err.println("Error: " + err.getMessage());
})
.endHandler(v -> {
System.out.println("Stream has ended.");
vertx.close();
})
.handler(buffer -> {
System.out.println("Received data: " + buffer.toString());
});
}
}Explanation:
- Initialization: Create a Vert.x instance and an
InputStreampointing to your source file. - Wrapping: Instantiate
AsyncReadStreamwith Vert.x and theInputStream. - Handlers:
- Data Handler: Processes incoming data asynchronously.
- Exception Handler: Handles any errors during the read operation.
- End Handler: Notifies when the stream has been fully read or closed.
import com.inqwise.async.stream.AsyncWriteStream;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.streams.WriteStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class WriteStreamExample {
public static void main(String[] args) throws Exception {
Vertx vertx = Vertx.vertx();
OutputStream outputStream = new FileOutputStream("output.txt");
WriteStream<Buffer> writeStream = new AsyncWriteStream(vertx, outputStream);
writeStream.exceptionHandler(err -> {
System.err.println("Error: " + err.getMessage());
});
Buffer data = Buffer.buffer("Hello, Inqwise Async!");
writeStream.write(data).onComplete(ar -> {
if (ar.succeeded()) {
System.out.println("Data written successfully.");
} else {
System.err.println("Failed to write data: " + ar.cause().getMessage());
}
});
writeStream.end(ar -> {
if (ar.succeeded()) {
System.out.println("Stream closed successfully.");
} else {
System.err.println("Failed to close stream: " + ar.cause().getMessage());
}
vertx.close();
});
}
}Explanation:
- Initialization: Create a Vert.x instance and an
OutputStreampointing to your destination file. - Wrapping: Instantiate
AsyncWriteStreamwith Vert.x and theOutputStream. - Handlers:
- Exception Handler: Handles any errors during the write operation.
- Write Operation: Writes data asynchronously and notifies upon completion.
- End Handler: Closes the stream and notifies upon successful closure or failure.
Contributions are welcome! If you'd like to contribute to the project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes with clear commit messages.
- Submit a pull request describing your changes.
Please ensure that your code adheres to the project's coding standards and includes appropriate tests.
This project is licensed under the MIT License.
If you have any questions or need further assistance, feel free to open an issue or contact the maintainers.