1 package de.spring.webservices.rest.controller.adapters;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.web.context.request.async.DeferredResult;
8 import rx.schedulers.Schedulers;
10 public class RxJavaAdapter {
11 private static final Logger LOGGER = LoggerFactory.getLogger(RxJavaAdapter.class);
15 * WHEN EXCEPTION IN setErrorResult, Spring WILL TRIGGER THE Spring Exception Handler AS YOU KNOW IT (I HOPE)
16 * SO, YOU COULD HOOK UP THE HANDLER AND RETURN YOUR CUSTOM MESSAGESS (as usual)
20 // With no value, we depend on the Tomcat/Jboss/Jetty/etc timeout value for asynchronous requests.
21 // Spring will answer after 60 secs with an empty response (by default) and HTTP 503 status (by default) when timeout.
22 private static final long ASYNC_TIMEOUT = 60000; /* milliseconds */
26 public interface DeferredCall<T> {
31 public static final <T> DeferredResult<T> deferredAdapter(Observable<T> observable) {
33 DeferredResult<T> deferredResult = new DeferredResult<>(ASYNC_TIMEOUT);
36 .subscribeOn(Schedulers.io())
37 .subscribe(deferredResult::setResult, exception -> LOGGER.error("error: ", exception));
39 return deferredResult;