-
Notifications
You must be signed in to change notification settings - Fork 8
Reactive Java with Reakt Abstract
Reakt provides promises, streams and the Reakt Reactor to handle asynchronous call coordination. It was influenced by the design of Promises in ES6.
You want to async call serviceA, then serviceB, take the results of serviceA & serviceB, then call serviceC. Then based on the results of call C, call D or E and then return the results to the original caller. Calls to A, B, C, D and E are all async calls and none should take longer than 10 seconds, if they do, then return a timeout to the original caller. The whole async call sequence should timeout in 20 seconds if it does not complete, and also check for circuit breakers, and provide back pressure feedback so the system does not have cascading failures.
Reakt was created as part of another project to do call async call coordination. Reakt split out the call coordination which is more of a generalized async handling concern.
Reakt already has extension libraries for Guava/Cassandra and Vert.x, and QBit supports Reakt.
Reakt is a utility lib for async call coordination. It is open source under the Apache 2 licenses. Splitting out call coordination into a second library allowed us to focus on async call coordination on the JVM, and making that experience clean and enjoyable by using ideas from Promises from ES6, but adapting them for the JVM.
Reactive programming centers around data event flows and propagation of change. There are many example of reactive programming: spreadsheets, Swing event loop, the Vert.x event bus, Node.js event loop and the JavaScript browser event loop.
While Functional reactive programming has its place, and is increasingly used to process large streams of data into useable information in near real time. However, there is a much more common and mundane use of reactive programming.
Object-oriented reactive programming (OORP) combines object oriented programming with reactive programming. This has become a very popular model with tools like Angular, React and jQuery. jQuery, ES6 and more manage call coordination with Promises.
Promises are a common way to manage streams of event data into actionable responses.
While, events and streams are great for things that can happen multiple times — keyup, or event a user action stream for Kafka, etc. With those types of events there is little concern for what happened before per se.
However, when dealing with service calls and data repositories, you want to handle a response with a specific next action, and a different action if there was an error or timeout from the responses.
Promises are like event listeners except. Except a promise can only succeed or fail once not twice. Once it enters its completed state, success or failure, then it is done.
Reakt promises are very similar in concept to ES6 promises, which have become standardized in the JavaScript/TypeScript/ES5/ES6/ES7 pantheon on languages.
A promise can be:
- fulfilled The callback relating to the promise succeeded
- rejected The callback relating to the promise failed pending The callback has not been fulfilled or rejected yet
- completed The callback has been resolved or rejected
Since Java is not single threaded, the design of this promise and streaming library takes that into account. There are techniques to adapt the Promise constructs from the single threaded event loop in JavaScript to the JVM world (multi-threaded, race condition possibilities, thread visibility, etc.).
There are three types of promises:
- Callback promises (async)
- Blocking promises (for testing and legacy integration)
- Replay promises (allow promises to be handled on the same thread as caller, managed by a Reactor)
Unlike their JavaScript cousins that do not have to worry about thread safety. Promises in Reakt can be invokable which makes promises very fluent, and closer to their JavaScript cousins.
Passing a promise as a callback handler
employeeService.lookupEmployee(33, result -> {
result.then(e -> saveEmployee(e))
.catchError(error -> {
logger.error("Unable to lookup", error);
});
});
Promises in Java become even more fluent when you use invokable promises.
Using an invokable promise
employeeService.lookupEmployee("123")
.then((employee)-> {...})
.catchError(...).invoke();
Replay promises are the most like their JS cousins. Replay promises are usually managed by the Reakt Reactor and supports environments like Vert.x, Akka, Reactor, Netty, async noSQL drivers, QBit, and others.
Java Promises
- Promise
- Promise then*() and catchError()
- Promise thenMap()
- Promise all()
- Promise any()
- Blocking Promise
- Invokable Promise
- Reactor Replay Promises
Reactor, Stream, Results
- QBit Reactive Microservices
- Reakt Reactive Java
- Reakt Guava Bridge
- QBit Extensions
- Elekt Consul Leadership election
- Elekt Leadership election
- Reactive Microservices
What is Microservices Architecture?
QBit Java Micorservices lib tutorials
The Java microservice lib. QBit is a reactive programming lib for building microservices - JSON, HTTP, WebSocket, and REST. QBit uses reactive programming to build elastic REST, and WebSockets based cloud friendly, web services. SOA evolved for mobile and cloud. ServiceDiscovery, Health, reactive StatService, events, Java idiomatic reactive programming for Microservices.
Reactive Programming, Java Microservices, Rick Hightower
Java Microservices Architecture
[Microservice Service Discovery with Consul] (http://www.mammatustech.com/Microservice-Service-Discovery-with-Consul)
Microservices Service Discovery Tutorial with Consul
[Reactive Microservices] (http://www.mammatustech.com/reactive-microservices)
[High Speed Microservices] (http://www.mammatustech.com/high-speed-microservices)
Reactive Microservices Tutorial, using the Reactor
QBit is mentioned in the Restlet blog
All code is written using JetBrains Idea - the best IDE ever!
Kafka training, Kafka consulting, Cassandra training, Cassandra consulting, Spark training, Spark consulting
Java Promises
- Promise
- Promise then*() and catchError()
- Promise thenMap()
- Promise all()
- Promise any()
- Blocking Promise
- Invokable Promise
- Reactor Replay Promises
Callback, and async Results
Reactor, Stream and Stream Result
Expected & Circuit Breaker
Scala Akka and Reakt