-
Notifications
You must be signed in to change notification settings - Fork 8
Callback
Callback is a generic event handler which can be thought of as a callback handler.
This is like an async future or callback. This was modeled after QBit's callback and Guava's Callback, and JavaScripts callbacks. A Result
represents the result or error from an async operation
and is passed to onResult
.
package io.advantageous.reakt;
import org.junit.Test;
import static org.junit.Assert.*;
public class CallbackTest {
@Test
public void test() throws Exception {
TestService testService = new TestService();
Result<Employee>[] results = new Result[1];
Employee[] employee = new Employee[1];
testService.simple(result -> {
results[0] = result;
result.then(e -> employee[0] = e).catchError(error -> {
System.err.println(error.getMessage());
});
});
assertTrue(results[0].complete());
assertFalse(results[0].failure());
assertTrue(results[0].success());
assertNotNull(employee[0]);
}
@Test
public void testError() throws Exception {
TestService testService = new TestService();
Result<Employee>[] results = new Result[1];
testService.error(result -> {
results[0] = result;
});
assertTrue(results[0].complete());
assertTrue(results[0].failure());
assertFalse(results[0].success());
}
@Test
public void testException() throws Exception {
TestService testService = new TestService();
Result<Employee>[] results = new Result[1];
testService.exception(result -> {
results[0] = result;
});
assertTrue(results[0].complete());
assertTrue(results[0].failure());
assertFalse(results[0].success());
}
static class Employee {
private final String id;
Employee(String id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return id != null ? id.equals(employee.id) : employee.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
public static class TestService {
public void simple(Callback<Employee> callback) {
callback.reply(new Employee("Rick"));
}
public void error(Callback<Employee> callback) {
callback.reject("Error");
}
public void exception(Callback<Employee> callback) {
callback.reject(new IllegalStateException("Error"));
}
}
}
package io.advantageous.reakt;
import io.advantageous.reakt.impl.ResultImpl;
/**
* A generic event handler which can be thought of as a callback handler.
* <p>
* This is like an async future or promise.
* <p>
* This was modeled after QBit's callback, and JavaScripts callbacks.
* The {@link Result} result represents the result or error from an async operation.
*
* @param <T> type of result returned from callback
*/
public interface Callback<T> {
/**
* (Client view)
* A result was returned so handle it.
* <p>
* This is registered from the callers (or event receivers perspective).
* A client of a service would override {@code onResult}.
*
* @param result to handle
*/
void onResult(Result<T> result);
/**
* (Service view)
* This allows services to send back a failed result easily to the client/handler.
* <p>
* This is a helper methods for producers (services that produce results) to send a failed result.
*
* @param error error
*/
default void reject(final Throwable error) {
onResult(new ResultImpl<>(error));
}
/**
* (Service view)
* This allows services to send back a failed result easily to the client/handler.
* <p>
* This is a helper methods for producers (services that produce results) to send a failed result.
*
* @param errorMessage error message
*/
default void reject(final String errorMessage) {
onResult(new ResultImpl<>(new IllegalStateException(errorMessage)));
}
/**
* (Service view)
* This allows services to send back a result easily to the client/handler.
* <p>
* This is a helper methods for producers (services that produce results) to send a result.
*
* @param result result value to send.
*/
default void reply(final T result) {
onResult(new ResultImpl<>(result));
}
}
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