use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class HibernateAsyncController method display.
public XFuture<Render> display(Integer id) {
EntityManager mgr = Em.get();
XFuture<Integer> future = new XFuture<Integer>();
kickOffAsyncResponse(future);
return future.thenApply(intVal -> runDisplay(mgr, id));
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class TestPermitQueue method testAddPermits.
@Test
public void testAddPermits() throws InterruptedException {
PermitQueue queue = new PermitQueue(1);
XFuture<Long> future1 = new XFuture<Long>();
svc.addToReturn(future1);
svc.addToReturn(future1);
svc.addToReturn(future1);
svc.addToReturn(future1);
queue.runRequest(() -> svc.runFunction(1));
queue.runRequest(() -> svc.runFunction(2));
queue.runRequest(() -> svc.runFunction(3));
queue.runRequest(() -> svc.runFunction(4));
List<Integer> results = svc.getAndClear();
Assert.assertEquals(1, results.size());
Assert.assertEquals(1, results.get(0).intValue());
queue.modifyPermitPoolSize(2);
List<Integer> results2 = svc.getAndClear();
Assert.assertEquals(2, results2.size());
Assert.assertEquals(2, results2.get(0).intValue());
Assert.assertEquals(3, results2.get(1).intValue());
queue.releasePermit();
List<Integer> results3 = svc.getAndClear();
Assert.assertEquals(1, results3.size());
Assert.assertEquals(4, results3.get(0).intValue());
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class MyStreamRef method cancel.
@Override
public XFuture<Void> cancel(CancelReason reason) {
// swap out writer for a cancelled one
XFuture<StreamWriter> writer = new XFuture<StreamWriter>();
writer.completeExceptionally(new CancellationException("Cancelled. reason=" + reason));
ref.set(writer);
return XFuture.completedFuture(null);
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class RouterServiceImpl method incomingRequest.
@Override
public StreamRef incomingRequest(Http2Request req, RouterResponseHandler handler) {
// ******************************************************************************************
// DO NOT ADD CODE HERE OR ABOVE THIS METHOD in RouterService. This is our CATCH-ALL point so
// ANY code above that is not protected from our catch and respond to clients
// ******************************************************************************************
String txId = generate();
// I do NOT like doing Guice creation on the request path(Dagger creation would probably be ok) BUT this
// is very worth it AND customers can swap out these critical classes if they need to quickly temporarily fix a bug while
// we work on the bug. We can easily give customers bug fixes like add binder.bind(ClassWithBug.class).to(BugFixCode.class)
ProxyStreamHandle proxyHandler = proxyProvider.get();
proxyHandler.init(handler, req);
MDC.put("txId", txId);
// top level handler...
try {
RouterStreamRef streamRef = incomingRequestProtected(req, proxyHandler);
XFuture<StreamWriter> writer = streamRef.getWriter().thenApply(w -> new TxStreamWriter(txId, w));
XFuture<StreamWriter> finalWriter = writer.handle((r, t) -> {
if (t == null)
return XFuture.completedFuture(r);
XFuture<StreamWriter> fut = proxyHandler.topLevelFailure(req, t);
return fut;
}).thenCompose(Function.identity());
return new RouterStreamRef("routerSevcTop", finalWriter, streamRef);
} finally {
MDC.remove("txId");
}
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class AbstractRouterService method incomingRequest.
public RouterStreamRef incomingRequest(RouterRequest routerRequest, ProxyStreamHandle handler) {
try {
Session session = (Session) cookieTranslator.translateCookieToScope(routerRequest, new SessionImpl(translator));
FlashSub flash = (FlashSub) cookieTranslator.translateCookieToScope(routerRequest, new FlashImpl(translator));
Validation validation = (Validation) cookieTranslator.translateCookieToScope(routerRequest, new ValidationImpl(translator));
ApplicationContext ctx = webInjector.getAppContext();
RequestContext requestCtx = new RequestContext(validation, flash, session, routerRequest, ctx);
String user = session.get("userId");
MDC.put("userId", user);
// TODO(dhiller): This is request heaaders choke point but need ot also perhaps setup streaming choke point
// here as well
Current.setContext(requestCtx);
try {
return incomingRequestImpl(requestCtx, handler);
} finally {
Current.setContext(null);
}
} catch (BadCookieException e) {
// CHEAT: we know this is syncrhonous exception from the translateCookieToScope
log.warn("This occurs if secret key changed, or you booted another webapp with different key on same port or someone modified the cookie", e);
XFuture<StreamWriter> writer = handler.sendRedirectAndClearCookie(routerRequest, e.getCookieName());
return new RouterStreamRef("cookieFailed", writer, null);
}
}
Aggregations