An open API service indexing awesome lists of open source software.

https://github.com/m3dev/multilane

Multi-lane Expressway to Aggregate Values in Parallel
https://github.com/m3dev/multilane

Last synced: 5 months ago
JSON representation

Multi-lane Expressway to Aggregate Values in Parallel

Awesome Lists containing this project

README

          

# multilane

'multilane' is a multi-lane expressway to aggregate values in parallel.

# Setup

## Maven

Available on the maven central repository. Add the following dependency:

```xml


com.m3
multilane
[1.0,)

```

CAUTION: Don't use older version than 1.0.5. The previous versions have [a critical bug](https://github.com/m3dev/multilane/issues/1).

## Example

### Basic MultiLane Usage

Simply aggregate same type values.

```java
MultiLane multiLane = new MultiLaneTemplate {};

int timeoutMillis = 1000;
multiLane.start("part1", new InputAction("A", timeoutMillis) {
protected Result process(String input) throws Exception {
return doSomething(input);
}
});
multiLane.start("part2", new InputAction("B", timeoutMillis) {
protected Result process(String input) throws Exception {
return doSomething(input);
}
}, Result.Failed); // with default value

// Use #collect() if you want to handle each action's failure
Map> result = multiLane.collect();

// Use #collectValues() if you just want to aggregate values
// It'll use null or default value if action failed or timed out)
Map resultMap = multiLane.collectValues();
```

Or use built-in if possible.

```java
HttpGetToStringMultiLane multiLane = new HttpGetToStringMultiLane();

long timeoutMillis = 2500L;
HttpGetToStringAction action1 = new HttpGetToStringAction("http://localhost:8080/api/1s", timeoutMillis);
HttpGetToStringAction action2 = new HttpGetToStringAction("http://localhost:8080/api/2s", timeoutMillis);
HttpGetToStringAction action3 = new HttpGetToStringAction("http://localhost:8080/api/3s", timeoutMillis);

String unavailable = "

  • Unavailable
  • ";

    multiLane.start("p1", action1, unavailable);
    multiLane.start("p2", action1, unavailable);
    multiLane.start("p3", action1, unavailable);
    multiLane.start("p4", action2, unavailable);
    multiLane.start("p5", action2, unavailable);
    multiLane.start("p6", action3, unavailable);

    // Blocking here!
    Map parts = multiLane.collectValues();
    /*
    Map(
    "p1" -> "

  • Response in 1s
  • ",
    "p2" -> "
  • Response in 1s
  • ",
    "p3" -> "
  • Response in 1s
  • ",
    "p4" -> "
  • Response in 2s
  • ",
    "p5" -> "
  • Response in 2s
  • ",
    "p6" -> "
  • Unavailable
  • "
    )
    */

    // or handle Either values if you need to check the errors
    Map> results = multiLane.collect();
    ```

    ### ActionToBeanMultiLane Usage

    Aggregate result values as Java bean fields.

    ```java
    public static class Profile {

    private Name name;
    private Integer age;

    public Name getName() { return name; }
    public void setName(Name name) { this.name = name; }
    public Intger getAge() { return age; }
    public void setAge(Integer age) { this.age = age; }
    }

    ActionToBeanMultiLane multiLane = new ActionToBeanMultiLane();

    int timeoutMillis = 1000;
    multiLane.start("name", new InputAction("alice", timeoutMillis) {
    public Name process(String input) throws Exception {
    return new Name(input.toUpperCase());
    }
    });
    multiLane.start("age", new NoInputAction(timeoutMillis) {
    public Integer process() throws Exception {
    return 20;
    }
    });

    Profile profile = multiLane.collectValuesAsBean(Profile.class);
    profile.getName(); // "ALICE"
    profile.getAge(); // 20
    ```

    ## License

    Copyright 2012 M3, Inc.

    Apache License, Version 2.0

    http://www.apache.org/licenses/LICENSE-2.0.html