-
Notifications
You must be signed in to change notification settings - Fork 8
BlockingPromise
A BlockingPromise
is very much like a Java Future. It is blocking.
This is useful for unit testing and for legacy integration.
Promises returns a blocking promise as follows:
/**
* Create a blocking promise.
* NOTE BLOCKING PROMISES ARE FOR LEGACY INTEGRATION AND TESTING ONLY!!!
* After you create a promise you register its then and catchError and then you use it to
* handle a callback.
*
* @param <T> type of result
* @return new promise
*/
static <T> Promise<T> blockingPromise() {
return new BlockingPromise<>();
}
Here are some example testing with a blocking promise from our unit tests.
@Test
public void testAsyncServiceWithInvokePromiseFail() {
Promise<URI> promise = Promises.blockingPromise();
/* Set up handlers. */
promise.then(this::handleSuccess)
.catchError(this::handleError);
/* Force a failure by passing null. */
asyncServiceDiscovery.lookupService(promise, null);
try {
promise.get();
fail();
} catch (Exception ex) {
}
...
}
Here is another example that uses a blocking promise to do a unit test (success path).
@Test
public void testAsyncWithBlockingPromise() throws Exception {
TestService testService = new TestService();
Employee[] employee = new Employee[1];
Expected[] value = new Expected[1];
/* Note this is only for legacy integration and testing. */
Promise<Employee> promise = Promises.blockingPromise();
promise.then(e -> employee[0] = e);
promise.thenExpect(employeeValue -> value[0] = employeeValue);
testService.async(promise);
assertNotNull(promise.get());
assertNotNull(promise.expect());
assertTrue(promise.complete());
assertFalse(promise.failure());
assertTrue(promise.success());
assertNull(promise.cause());
assertNotNull(employee[0]);
assertNotNull(value[0]);
}
You can even use blocking promises with services that return a promise, i.e., an invokable promise.
@Test
public void testAsyncServiceWithInvokePromiseFail() {
Promise<URI> promise = Promises.blockingPromise();
promise.then(this::handleSuccess)
.catchError(this::handleError);
asyncServiceDiscovery.lookupService(null).invokeWithPromise(promise);
try {
promise.get();
fail();
} catch (Exception ex) {
}
...
}
Blocking promises are very important for legacy integration, prototyping and unit testing, but you should not use them in a true reactive Java app.
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