930972fc7a1ed44da85d84693d7e23aa6c1b0894
[JavaForFun] /
1 package de.spring.webservices.rest.business.service.impl;
2
3 import java.io.IOException;
4
5 import javax.inject.Inject;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.springframework.data.domain.Page;
10 import org.springframework.data.domain.Pageable;
11 import org.springframework.stereotype.Service;
12
13 import de.spring.webservices.domain.Car;
14 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
15 import de.spring.webservices.rest.business.service.RxJavaBusinessLogic;
16 import io.reactivex.Observable;
17 import io.reactivex.exceptions.Exceptions;
18
19
20 @Service("rxJavaBusinessLogic")
21 public class RxJavaBusinessLogicImpl implements RxJavaBusinessLogic {
22         private static final Logger LOGGER = LoggerFactory.getLogger(RxJavaBusinessLogicImpl.class);
23
24     private final AwesomeBusinessLogic awesomeBusinessLogic;
25     
26     @Inject
27         public RxJavaBusinessLogicImpl(AwesomeBusinessLogic awesomeBusinessLogic) {
28                 this.awesomeBusinessLogic = awesomeBusinessLogic;
29         }
30
31         @Override
32         public Observable<Page<Car>> findAll(Pageable pageRequest) {
33         return Observable.create(observer -> observer.onNext( awesomeBusinessLogic.findAll(pageRequest)));
34
35         }
36
37         @Override
38         public Observable<Car> findById(long id) {
39         return Observable.create(observer -> observer.onNext( awesomeBusinessLogic.findById(id)));
40         }
41
42         @Override
43         public Observable<Car> create(Car car) {        
44                 return Observable.create(observer -> observer.onNext(awesomeBusinessLogic.create(car)));
45         }
46         
47         @Override
48         public Observable<Car> createThrowable(Car car) {       
49                 return Observable.create(observer -> {
50
51                                 try {
52                                         observer.onNext(awesomeBusinessLogic.createThrowable(car));
53                                 } catch (IOException ex) {
54                                         // I could use this implementation. Instead, I will wrap my exception because
55                                         // that is what you would be doing if you were using any other method from RxJava (like map() for example)
56                                         // observer.onError(ex);
57                                         
58                                         LOGGER.error("createThrowable error: ", ex);
59                                         
60                                         Exceptions.propagate(ex);
61                                 }
62
63                 });
64         }
65 }