Required call to onCompleted()
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 1 Jan 2017 13:16:57 +0000 (14:16 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 1 Jan 2017 13:16:57 +0000 (14:16 +0100)
SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/business/service/impl/AwesomeBusinessLogicImpl.java
SpringJava/RxJava/web-services-spring-rxjava-server/src/main/java/de/spring/webservices/rest/business/service/impl/RxJavaBusinessLogicImpl.java

index f41af5d..f81e8f6 100644 (file)
@@ -12,6 +12,7 @@ import org.springframework.stereotype.Service;
 
 import de.spring.webservices.domain.Car;
 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
+import rx.exceptions.Exceptions;
 
 @Service("awesomeBusinessLogic")
 public class AwesomeBusinessLogicImpl implements AwesomeBusinessLogic {
@@ -65,5 +66,9 @@ public class AwesomeBusinessLogicImpl implements AwesomeBusinessLogic {
        public Car createThrowable(Car car) throws IOException {
                
                throw new IOException("createThrowable FATAL ERROR");
+               // Spring sees both exceptions.
+               // It seems like calling Exceptions.propagate(ex) in RxJava code is only required
+               // for no RuntimeExceptions :/
+               // throw new RuntimeException("createThrowable FATAL ERROR");
        }
 }
index c747c7f..771fb40 100644 (file)
@@ -37,6 +37,7 @@ public class RxJavaBusinessLogicImpl implements RxJavaBusinessLogic {
                        @Override
                        public void call(Subscriber<? super Page<Car>> observer) {
                                observer.onNext( awesomeBusinessLogic.findAll(pageRequest));
+                               observer.onCompleted();
                        }
                }).subscribeOn(Schedulers.io());
        }
@@ -47,22 +48,25 @@ public class RxJavaBusinessLogicImpl implements RxJavaBusinessLogic {
                        @Override
                        public void call(Subscriber<? super Page<Car>> observer) {
                                observer.onNext( awesomeBusinessLogic.findAll(pageRequest));
+                               observer.onCompleted();
                        }
                }).subscribeOn(Schedulers.io());
        }
 
        @Override
        public Observable<Car> findById(long id) {
-       return Observable.create((Subscriber<? super Car> observer) ->
-                               observer.onNext( awesomeBusinessLogic.findById(id)))
-                       .subscribeOn(Schedulers.io());
+       return Observable.create((Subscriber<? super Car> observer) -> {
+                               observer.onNext( awesomeBusinessLogic.findById(id));
+                               observer.onCompleted();
+                       }).subscribeOn(Schedulers.io());
        }
 
        @Override
        public Observable<Car> create(Car car) {        
-               return Observable.create((Subscriber<? super Car> observer) ->
-                                       observer.onNext(awesomeBusinessLogic.create(car)))
-                               .subscribeOn(Schedulers.io());
+               return Observable.create((Subscriber<? super Car> observer) -> {
+                                       observer.onNext(awesomeBusinessLogic.create(car));
+                                       observer.onCompleted();
+                               }).subscribeOn(Schedulers.io());
        }
        
        @Override
@@ -71,6 +75,7 @@ public class RxJavaBusinessLogicImpl implements RxJavaBusinessLogic {
 
                                try {
                                        observer.onNext(awesomeBusinessLogic.createThrowable(car));
+                                       observer.onCompleted();
                                } catch (IOException ex) {
                                        // I could use this implementation. Instead, I will wrap my exception because
                                        // that is what you would be doing if you were using any other method from RxJava (like map() for example)
@@ -80,9 +85,6 @@ public class RxJavaBusinessLogicImpl implements RxJavaBusinessLogic {
                                        
                                        Exceptions.propagate(ex);
                                }
-                               
-                               // No idea when to use this stuff :(
-                               // observer.onCompleted();
 
                }).subscribeOn(Schedulers.io());
        }