gpt4 book ai didi

java - 通过 Reactor Spring 处理异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:54 24 4
gpt4 key购买 nike

我正在使用 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;
}
}

然后我有一个 Controller 将请求传递给 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;
}

}

一切正常,但在 ApplicationService 抛出异常的情况下 Promise 的值未设置,但我在控制台中得到以下信息:

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}

问题是:

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

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

最佳答案

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

现在我的 Controller 看起来像

@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();
}
}

服务看起来像

@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));
}
});
}
}

不好的是现在我必须使用 Stream<T>由于服务中的方法(据推测是业务逻辑),因此使用该服务的任何人现在都知道 Stream -ish 服务的性质,因此 Stream渗入代码的其他部分,例如现在我可能不得不使用 await()在使用该服务的代码中。

完整的申请可在 https://github.com/evgeniysharapov/spring-reactor-demo 获得

关于java - 通过 Reactor Spring 处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com