Update to RxJava 2.0
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 11 Dec 2016 20:17:30 +0000 (21:17 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 11 Dec 2016 20:17:30 +0000 (21:17 +0100)
SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/business/service/RxJavaBusinessLogic.java
SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/business/service/impl/RxJavaBusinessLogicImpl.java
SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/RxJavaCarController.java
SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/controller/adapters/RxJavaAdapter.java

index db032a0..930972f 100644 (file)
@@ -13,8 +13,8 @@ import org.springframework.stereotype.Service;
 import de.spring.webservices.domain.Car;
 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
 import de.spring.webservices.rest.business.service.RxJavaBusinessLogic;
-import rx.Observable;
-import rx.exceptions.Exceptions;
+import io.reactivex.Observable;
+import io.reactivex.exceptions.Exceptions;
 
 
 @Service("rxJavaBusinessLogic")
@@ -59,7 +59,7 @@ public class RxJavaBusinessLogicImpl implements RxJavaBusinessLogic {
                                        
                                        Exceptions.propagate(ex);
                                }
-       
+
                });
        }
 }
index 3aa72d2..34827cb 100644 (file)
@@ -26,7 +26,7 @@ import org.springframework.web.context.request.async.DeferredResult;
 
 import de.spring.webservices.domain.Car;
 import de.spring.webservices.rest.business.service.RxJavaBusinessLogic;
-import rx.Observable;
+import io.reactivex.Observable;
 
 @RestController
 @RequestMapping("/api/rxjava/cars/")
index 04ee276..a2e9920 100644 (file)
@@ -4,8 +4,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.web.context.request.async.DeferredResult;
 
-import rx.Observable;
-import rx.schedulers.Schedulers;
+import io.reactivex.Observable;
+import io.reactivex.Single;
+import io.reactivex.schedulers.Schedulers;
 
 public class RxJavaAdapter {
        private static final Logger LOGGER = LoggerFactory.getLogger(RxJavaAdapter.class);
@@ -18,7 +19,7 @@ public class RxJavaAdapter {
        public static final <T> DeferredResult<T> deferredAdapter(Observable<T> observable) {
 
        DeferredResult<T> deferredResult = new DeferredResult<>(ASYNC_TIMEOUT);
-
+       
        observable
                .subscribeOn(Schedulers.io())
                .subscribe(deferredResult::setResult, exception -> {
@@ -32,6 +33,23 @@ public class RxJavaAdapter {
         return deferredResult; 
        }
        
+       public static final <T> DeferredResult<T> deferredAdapter(Single<T> single) {
+
+       DeferredResult<T> deferredResult = new DeferredResult<>(ASYNC_TIMEOUT);
+
+       single
+               .subscribeOn(Schedulers.io())
+               .subscribe(deferredResult::setResult, exception -> {
+                               Throwable realException = launderException(exception);
+       
+                               LOGGER.error("error: ", realException);
+
+                       deferredResult.setErrorResult(realException);
+               });
+
+        return deferredResult;
+       }
+       
        private static final Throwable launderException(Throwable exception) {
                return exception.getCause() != null
                           ? exception.getCause()