2015-05-06 15 views
5

Tôi đang sử dụng lò phản ứng 2 và mùa xuân 4. Dưới đây là các mã tiêu biểu mà tôi có - một Consumer làm việc với một khongoại lệ Xử bởi Reactor Xuân

@Consumer 
public class ApplicationService { 

    @Selector(value="/applications/id", type = SelectorType.URI) 
    @ReplyTo 
    public Application byApplicationId(String id) throws ApplicationNotFoundException { 
     Application app = appRepo.findOne(id); 
     if(app == null) 
     throw new ApplicationNotFoundException("Application `" + id + "` could not be found."); 
     return app; 
    } 
} 

Sau đó, tôi có một bộ điều khiển đã vượt qua yêu cầu đến một eventBus vào mà tôi vượt qua các yêu cầu và trả về một Promise

@RestController 
@RequestMapping("/applications") 
public class ApplicationsController { 
    @RequestMapping(value = "/{id}", method = GET, produces = APPLICATION_JSON_VALUE) 
    public Promise<Event<Application>> byApplicationId(@PathVariable final String id) { 
     final Promise<Event<Application>> p = Promises.prepare(env); 
     eventBus.sendAndReceive("/applications/id", Event.wrap(id), p); 
     return p; 
    } 

} 

điều cần làm việc nhưng trong trường hợp của ApplicationService ném một ngoại lệ giá trị Promise s không được thiết lập nhưng tôi có được sau trong ngày e console:

16:46:58.003 [main] ERROR reactor.bus.EventBus - null 
java.lang.reflect.UndeclaredThrowableException 
    at org.springframework.util.ReflectionUtils.rethrowRuntimeException(ReflectionUtils.java:302) 
... 
Caused by: com.metlife.harmony.exceptions.ApplicationNotFoundException: Application `2860c555-0bc4-45e6-95ea-f724ae3f4464` could not be found. 
    at com.metlife.harmony.services.ApplicationService.byApplicationId(ApplicationService.java:46) ~[classes/:?] 
... 
Caused by: reactor.core.support.Exceptions$ValueCause: Exception while signaling value: reactor.bus.Event.class : Event{id=null, headers={}, [email protected], key=/applications/id, data=2860c555-0bc4-45e6-95ea-f724ae3f4464} 

Câu hỏi là:

  1. để tôi sử dụng lò phản ứng và eventBus một cách sai lầm? và nếu như vậy, là những gì đúng cách

  2. có lẽ chức năng này là chưa được thực hiện

+0

'eventBus.sendAndReceive ("/ ứng dụng/id", Event.wrap (id), p); 'nó không cuase đúc lỗi? –

+0

@AnadiMisra vào thời điểm nào? – EvgeniySharapov

+0

Đã cố gắng mã của bạn ra khỏi tò mò và tôi đã nhận điều này 'Phương thức sendAndReceive (Object, Event , Consumer ) trong loại EventBus không áp dụng cho các đối số (String, Event , Promise >)' at that line, my Promise object 'Promise > response = Promises.prepare (env);' –

Trả lời

3

Tôi đoán tôi đang đánh giá lại chiến lược của việc sử dụng lò phản ứng trong ứng dụng Spring tôi.

Bây giờ điều khiển của tôi trông giống như

@RestController 
public class GreetingController { 

    @Autowired 
    private GreetingService greetingService; 

    @RequestMapping("/greeting") 
    public Promise<ResponseEntity<?>> greeting(final @RequestParam(value = "name", defaultValue = "World") String name) { 
     return greetingService.provideGreetingFor(name).map(new Function<Greeting, ResponseEntity<?>>() { 
      @Override 
      public ResponseEntity<?> apply(Greeting t) { 
       return new ResponseEntity<>(t, HttpStatus.OK); 
      } 
     }).onErrorReturn(WrongNameException.class, new Function<WrongNameException, ResponseEntity<?>>() { 
      @Override 
      public ResponseEntity<?> apply(WrongNameException t) { 
       return new ResponseEntity<>(t.getMessage(), HttpStatus.BAD_REQUEST); 
      } 
     }).next(); 
    } 
} 

Và các dịch vụ trông giống như

@Service 
public class GreetingService { 
    @Autowired 
    private Environment env; 

    private static final String template = "Hello, %s!"; 
    private final AtomicLong counter = new AtomicLong(); 

    public Stream<Greeting> provideGreetingFor(String name) { 
     return Streams.just(name).dispatchOn(env).map(new Function<String, Greeting>() { 
      @Override 
      public Greeting apply(String t) { 
       if (t == null || t.matches(".*\\d+.*")) 
        throw new WrongNameException(); 
       return new Greeting(counter.incrementAndGet(), String.format(template, t)); 
      } 
     }); 
    } 
} 

gì xấu là bây giờ tôi phải sử dụng Stream<T> như là kết quả của phương pháp trong dịch vụ (đó là được cho là logic nghiệp vụ), vì vậy, bất kỳ ai sử dụng dịch vụ giờ đây đều biết về bản chất của dịch vụ Stream và kết quả là Stream chảy máu vào các phần khác của mã, ví dụ: bây giờ tôi có thể phải sử dụng await() trong mã bằng cách sử dụng dịch vụ.

ứng dụng đầy đủ có sẵn tại https://github.com/evgeniysharapov/spring-reactor-demo