MCPcopy Index your code
hub / github.com/advantageous/reakt

github.com/advantageous/reakt @4.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 4.0.1 ↗ · + Follow
572 symbols 2,167 edges 47 files 200 documented · 35% updated 8y ago4.0.1 · 2017-03-28★ 616 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

reakt Build Status Join the chat at https://gitter.im/advantageous/reakt

Reakt website

Reactive interfaces for Java.

Reakt is reactive interfaces for Java which includes: * Promises, * Streams, * Callbacks, * Async Results with Expected * Circuit Breakers

The emphasis is on defining interfaces that enable lambda expressions, and fluent APIs for asynchronous programming for Java.

Note: This mostly just provides the interfaces not the implementations. There are some starter implementations but the idea is that anyone can implement this. It is all about interfaces. There will be adapters for Vertx, RxJava, Reactive Streams, etc. There is support for Guava Async (used by Cassandra) and the QBit microservices lib. Elekt uses Reakt for its reactive leadership election.

Have a question?

Reakt Mailing List

Getting started

Using from maven

Reakt is published in the maven public repo.

<dependency>
    <groupId>io.advantageous.reakt</groupId>
    <artifactId>reakt</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

Using from gradle

compile 'io.advantageous.reakt:reakt:3.0.4.RELEASE'

Fluent Promise API

  employeeService.lookupEmployee(33)
                .then(e -> saveEmployee(e))
                .catchError(error -> logger.error("Unable to lookup employee", error))
                .invoke();

Promises are both a callback and a Result; however, you can work with Callbacks directly.

Using Result and callback directly

        employeeService.lookupEmployee(33, result -> {
            result.then(e -> saveEmployee(e))
                  .catchError(error -> logger.error("Unable to lookup", error));
        });

In both of these examples, lookupEmployee would look like:

Using Result and callback directly


   public void lookupEmployee(long employeeId, Callback<Employee> callback){...}

You can use Promises to transform into other promises.

Transforming into another type of promise using thenMap


        Promise<Employee> employeePromise = Promises.<Employee>blockingPromise();

        Promise<Sheep> sheepPromise = employeePromise
                .thenMap(employee1 -> new Sheep(employee1.getId()));

The thenMap will return a new type of Promise.

You can find more examples in the reakt wiki.

We also support working with streams.

Promise concepts

This has been adapted from this article on ES6 promises. A promise can be:

  • fulfilled The callback/action relating to the promise succeeded
  • rejected The callback/action relating to the promise failed
  • pending The callback/action has not been fulfilled or rejected yet
  • completed The callback/action has been fulfilled/resolved or rejected

Java is not single threaded, meaning that two bits of code can run at the same time, so the design of this promise and streaming library takes that into account.

There are three types of promises: * Callback promises * Blocking promises (for testing and legacy integration) * Replay promises (allow promises to be handled on the same thread as caller)

Replay promises are the most like their JS cousins. Replay promises are usually managed by the Reakt Reactor and supports environments like Vert.x and QBit. See the wiki for more details on Replay promises.

It is common to make async calls to store data in a NoSQL store or to call a remote REST interface or deal with a distributed cache or queue. Also Java is strongly typed so the library that mimics JS promises is going to look a bit different. We tried to use similar terminology where it makes sense.

Events and Streams are great for things that can happen multiple times on the same object — keyup, touchstart, or event a user action stream from Kafka, etc.

With those events you don't really care about what happened before when you attached the listener.

But often times when dealing with services 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. You essentially want to call and handle a response asynchronously and that is what promises allow.

This is not our first time to bat with Promises. QBit has had Promises for a few years now. We just called them CallbackBuilders instead. We wanted to use more standard terminology and wanted to use the same terminology and modeling on projects that do not use QBit like Conekt, Vert.x, RxJava, and reactive streams.

At their most basic level, promises are like event listeners except:

A promise can only succeed or fail once. A promise cannot succeed or fail twice, neither can it switch from success to failure. Once it enters its completed state, then it is done.

Bridges

Reakt Guava Bridge which allows libs that use Guava async support to now have a modern Java feel.

Cassandra Reakt example


register(session.executeAsync("SELECT release_version FROM system.local"), 
  promise().thenExpect(expected -> 
     gui.setMessage("Cassandra version is " +
         expected.get().one().getString("release_version"))
  ).catchError(error -> 
     gui.setMessage("Error while reading Cassandra version: " 
     + error.getMessage())
  )
);

QBit 1 ships with a bridge and QBit 2will use Reakt as its primary reactive callback mechanism.

Conekt, a slimmed down fork of Vert.x, will also use Reakt.

See QBit microservices lib for more details.

See our wiki for more details on Reakt.

Further reading

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.

Find more tutorial on QBit.

Reactive Programming, Java Microservices, Rick Hightower

High-speed microservices consulting firm and authors of QBit with lots of experience with Vertx - Mammatus Technology

Highly recommended consulting and training firm who specializes in microservices architecture and mobile development that are already very familiar with QBit and Vertx as well as iOS and Android - About Objects

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)

Java Microservices Consulting

Microservices Training

[Reactive Microservices Tutorial, using the Reactor] (https://github.com/MammatusTech/qbit-microservices-examples/wiki/Reactor-tutorial--%7C-reactively-handling-async-calls-with-QBit-Reactive-Microservices)

QBit is mentioned in the Restlet blog

All code is written using JetBrains Idea - the best IDE ever!

Extension points exported contracts — how you extend this code

Invokable (Interface)
Marker interface to indicate something is invokable. This is used in a promise to detect if something is invokable. @a [6 …
src/main/java/io/advantageous/reakt/Invokable.java
ReplayPromise (Interface)
Replay promise ensures that the event handler callbacks (then, catchError) happen on the calling thread. @param T v [2 …
src/main/java/io/advantageous/reakt/promise/ReplayPromise.java
TimeSource (Interface)
Time source for a reactor. This interface is used so that a thread may provide time when available instead of calling t [2 …
src/main/java/io/advantageous/reakt/reactor/TimeSource.java
StreamResult (Interface)
The result of an async operations with optional callbacks for cancel and request more. @param Type of result. @auth [1 …
src/main/java/io/advantageous/reakt/StreamResult.java
CallbackHandler (Interface)
A generic event handler which can be thought of as a callback handler. This is like an async future or promise. [1 implementers]
src/main/java/io/advantageous/reakt/CallbackHandler.java

Core symbols most depended-on inside this repo

asHandler
called by 152
src/main/java/io/advantageous/reakt/promise/Promise.java
get
called by 117
src/main/java/io/advantageous/reakt/Result.java
complete
called by 72
src/main/java/io/advantageous/reakt/Result.java
then
called by 43
src/main/java/io/advantageous/reakt/Result.java
success
called by 41
src/main/java/io/advantageous/reakt/Result.java
promise
called by 36
src/main/java/io/advantageous/reakt/promise/Promises.java
accept
called by 30
src/main/java/io/advantageous/reakt/CallbackHandler.java
process
called by 30
src/main/java/io/advantageous/reakt/reactor/Reactor.java

Shape

Method 509
Class 45
Interface 18

Languages

Java100%

Modules by API surface

src/main/java/io/advantageous/reakt/reactor/impl/ReactorImpl.java66 symbols
src/test/java/io/advantageous/reakt/promise/PromiseTest.java63 symbols
src/main/java/io/advantageous/reakt/promise/Promises.java45 symbols
src/test/java/io/advantageous/reakt/promise/InvokablePromiseTest.java31 symbols
src/main/java/io/advantageous/reakt/promise/impl/FinalPromise.java24 symbols
src/main/java/io/advantageous/reakt/promise/impl/BasePromise.java21 symbols
src/test/java/io/advantageous/reakt/reactor/impl/ReactorImplTest.java20 symbols
src/main/java/io/advantageous/reakt/reactor/Reactor.java20 symbols
src/main/java/io/advantageous/reakt/Expected.java19 symbols
src/test/java/io/advantageous/reakt/CallbackTest.java18 symbols
src/main/java/io/advantageous/reakt/promise/PromiseHandler.java18 symbols
src/test/java/io/advantageous/reakt/StreamTest.java17 symbols

For agents

$ claude mcp add reakt \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact