Streaming programming and event driven lightweight development framework.
- Clone project
git clone https://github.com/java-flow/java-flow.git
- Publish to local Maven repository
gradle publishToMavenLocal
- To add a dependency using Maven, use the following:
<dependency>
<groupId>com.javaflow</groupId>
<artifactId>java-flow</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
More examples can be viewed: Examples
@AllArgsConstructor
public class EchoFlow extends FlowDefiner {
private final Integer listenPort;
@Override
public Flow define() {
return new Flow("Echo")
.next(new TcpIn(listenPort, STRING))
.next(new TcpOut());
}
public static void main(String[] args) {
new EchoFlow(8080).deploy();
}
}
@AllArgsConstructor
public class PrintTimeFlow extends FlowDefiner {
private final Integer listenPort;
@Override
public Flow define() {
return new Flow("PrintTime")
.next(new TcpIn(listenPort, STRING))
.next(msg -> msg.payload(ZonedDateTime.now()))
.next(new TcpOut());
}
public static void main(String[] args) {
new PrintTimeFlow(8080).deploy();
}
}
@AllArgsConstructor
public class RequestFlow extends FlowDefiner {
private final String host;
private final Integer port;
@Override
public Flow define() {
return new Flow("Request")
.next(new TcpRequest(host, port))
.next(msg -> msg);
}
public static void main(String[] args) {
Flow flow = new RequestFlow("localhost", 8080).deploy();
flow.invoke(new Msg().payload("hello"));
}
}
@AllArgsConstructor
public class EchoFlow extends FlowDefiner {
private final Integer listenPort;
private final HttpMethod method;
private final String url;
@Override
public Flow define() {
return new Flow("Echo")
.next(new HttpIn(listenPort, method, url))
.next(new HttpOut());
}
public static void main(String[] args) {
new EchoFlow(8080, HttpMethod.post, "/echo").deploy();
}
}
@AllArgsConstructor
public class PrintTimeFlow extends FlowDefiner {
private final Integer listenPort;
private final HttpMethod method;
private final String url;
@Override
public Flow define() {
return new Flow("PrintTime")
.next(new HttpIn(listenPort, method, url))
.next(msg -> msg.payload(ZonedDateTime.now()))
.next(new HttpOut());
}
public static void main(String[] args) {
new PrintTimeFlow(8080, HttpMethod.get, "/time").deploy();
}
}
@AllArgsConstructor
public class RequestFlow extends FlowDefiner {
@Override
public Flow define() {
return new Flow("Request")
.next(msg -> {
msg.put(HttpRequest.Fields.method, "POST");
msg.put(HttpRequest.Fields.url, "https://localhost:8080/echo");
return msg;
})
.next(new HttpRequest())
.next(msg -> msg);
}
public static void main(String[] args) {
Flow flow = new RequestFlow().deploy();
flow.invoke(new Msg().payload("hello"));
}
}
There are mainly the following concepts:
- Object
Flow
Node
Network
Function
Msg
payload
- Method
Flow.next(node)
Insert a node at the end of the flow.Flow.deploy()
Deploy flow,Trigger all nodesonDeploy
method.Flow.destroy()
Destroy flow,Trigger all nodesonDestroy
method.Flow.nextInvoke(node, msg)
Invoke flow,Start from the specified node.Flow.invoke(msg)
Invoke flow. Start from the first node.Node.invoke(msg)
Call the node. The input is the message returned by the previous node, and the output message will be passed to the next node.
- Event
Node.onDeploy
Triggered during flow deployment. The node resource can be initialized at this time.Node.onDestroy
Triggered during flow destruction. The node resources can be released at this time.
The inspiration of this project comes from Node-Red, Imagine that it would be a very flexible and simple development method if common basic functions were encapsulated into processing nodes, and each node provided unified input and output ports to form a business process. This will be suitable for simple application development.