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
- Host: GitHub
- URL: https://github.com/m3dev/multilane
- Owner: m3dev
- License: other
- Archived: true
- Created: 2012-05-29T03:26:48.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2013-06-09T15:22:20.000Z (about 13 years ago)
- Last Synced: 2025-12-16T13:30:30.496Z (6 months ago)
- Language: Java
- Homepage:
- Size: 313 KB
- Stars: 4
- Watchers: 68
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
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 = "
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" -> "
"p2" -> "
"p3" -> "
"p4" -> "
"p5" -> "
"p6" -> "
)
*/
// 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