Unlocking PipelinR: A Lightweight Command Processing Pipeline for Java Apps

Nov 23, 2022 | Programming

PipelinR is here to revolutionize your Java application development, especially in the FinTech space. Imagine a bustling restaurant kitchen where every dish is prepared by specialized chefs rather than one chef trying to do it all. That’s the essence of PipelinR – each command is handled by its own dedicated handler, improving maintainability and scalability.

Table of Contents

How to Use

To get started with PipelinR, all you need is the library—no dependencies required! Set it up using Maven:



    net.sizovs
    pipelinr
    0.9

Or with Gradle:


dependencies {
    compile 'net.sizovs:pipelinr:0.9'
}

Ensure your Java version is 1.8 or higher to get started.

Commands

Think of commands as requests sent to a restaurant kitchen. Each command can return a value or simply signify that a task was completed. For example, consider the Ping command:


class Ping implements Command {
    public final String host;
    public Ping(String host) {
        this.host = host;
    }
}

This command fetches the host string when executed, much like how a waiter would serve your meal.

Handlers

Each command must have a handler, similar to how each dish in a restaurant has a chef. Here’s how you would define a Pong class that handles the Ping command:


class Pong implements Command.Handler {
    @Override
    public String handle(Ping command) {
        return "Pong from " + command.host;
    }
}

Pipeline

The pipeline acts as the conductor for commands and handlers, coordinating interactions. You can visualize this as a relay race where each runner (command) passes the baton (the command object) to the next runner (handler). Below is how to create and use a pipeline in PipelinR:


Pipeline pipeline = new Pipelinr()
    .with(() -> Stream.of(new Pong()));

pipeline.send(new Ping("localhost"));

// Or execute commands simply like this:
new Ping("localhost").execute(pipeline);

Commands can also be enhanced with custom middlewares, such as logging or validation, much like adding spices to a dish to enhance its flavor!

Notifications

Starting from version 0.5, PipelinR allows notifications that can dispatch messages to multiple handlers simultaneously. Think of notifications as announcements made in the restaurant that inform multiple chefs of a new special dish:


class Ping implements Notification {}
public class Pong1 implements Notification.Handler {
    @Override
    public void handle(Ping notification) {
        System.out.println("Pong 1");
    }
}
public class Pong2 implements Notification.Handler {
    @Override
    public void handle(Ping notification) {
        System.out.println("Pong 2");
    }
}

To send a notification through the pipeline, use:


new Ping().send(pipeline);

Spring Example

PipelinR easily integrates with Spring and Spring Boot. You will configure your pipeline within a Spring configuration class, allowing seamless command handling. Here’s a simple setup:


@Configuration
class PipelinrConfiguration {
    @Bean
    Pipeline pipeline(ObjectProvider commandHandlers) {
        return new Pipelinr().with(commandHandlers::stream);
    }
}

By using annotations for components, you can easily define handlers and middlewares, and integrate everything into your Spring application.

Async

For reactive applications, adventures in asynchronous command execution are made easy! Implementing an asynchronous ping command is a breeze:


class AsyncPing implements Command> {
    // Handler code here.
}

Sending this command returns a CompletableFuture, allowing you to handle results asynchronously, much like serving customers without making them wait for the entire meal preparation.

How to Contribute

If you’re interested in extending PipelinR’s capabilities, simply fork the repo and send a pull request!

Alternatives

For those familiar with .NET, you might want to explore MediatR, a similar mediator implementation for managing requests.

Contributors

This project is led by Eduards Sizovs. You can find more about him on his blog or follow him on Twitter.

Troubleshooting

As you embark on your journey with PipelinR, you might encounter some hiccups. Here’s a quick troubleshooting guide:

  • Ensure your Java version is compatible – updates may cause discrepancies.
  • If commands aren’t being processed as expected, verify that your handlers are correctly implemented and registered in the pipeline.
  • For issues related to asynchronous execution, double-check your CompletableFuture implementations and ensure they align with Java’s concurrency model.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox