通过Reactor Spring处理异常

Evg*_*pov 6 java spring spring-mvc reactor project-reactor

我正在使用Reactor 2和Spring 4.这是我的典型代码 - Consumer使用存储库

@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;
   }
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个控制器将请求传递给eventBus我传递请求并返回一个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;
   }

}
Run Code Online (Sandbox Code Playgroud)

事情有效,但是在ApplicationService抛出异常Promise的情况下,s值没有设置,但我确实在控制台中得到了以下内容:

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={}, replyTo=reactor.bus.selector.Selectors$AnonymousKey@4, key=/applications/id, data=2860c555-0bc4-45e6-95ea-f724ae3f4464}
Run Code Online (Sandbox Code Playgroud)

问题是:

  1. eventBus以错误的方式使用Reactor 吗?如果是这样,那么正确的方法是什么

  2. 也许这个功能还没有实现

Evg*_*pov 4

我想我重新评估了在 Spring 应用程序中使用 Reactor 的策略。

现在我的控制器看起来像

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

而且服务看起来像

@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));
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

糟糕的是,现在我必须使用Stream<T>服务中的方法(这应该是业务逻辑),因此使用该服务的任何人现在都知道该Stream服务的本质,因此Stream会渗透到其他服务中部分代码,例如现在我可能必须await()在使用该服务的代码中使用。

完整的应用程序可在https://github.com/evgeniysharapov/spring-reactor-demo获取