armeria リクエストを処理し、いくつかのイベントを にディスパッチするシナリオがありguava
ますEventBus
。問題は、EventBus ハンドラーでイベントを処理している間にコンテキストが失われることです。イベント プロセッサにアクセスさせる方法はありますかServiceRequestContext
。
class EventListener {
@Subscribe
public void process(SomeCustomizedClass event) {
final ServiceRequestContext context = ServiceRequestContext.currentOrNull();
log.info("process ServiceRequestContext context={}", context);
}
}
イベントハンドラを登録します。
EventBus eventBus = new AsyncEventBus(ThreadPoolTaskExecutor());
eventBus.register(new EventListener());
これが私のArmeria
サービスです
@Slf4j
public class NameAuthRestApi {
final NameAuthService nameAuthService;
@Post("/auth")
@ProducesJson
public Mono<RealNameAuthResp> auth(RealNameAuthReq req) {
return nameAuthService.auth(NameAuthConverter.CONVERTER.toDto(req))
.handle((result, sink) -> {
if (result.isSuccess()) {
// I post an event here, but the event process couldn't access the ServiceRequestContext
// that's would be the problem.
eventBus.post(new SomeCustomizedClass(result));
final RealNameAuthResp realNameAuthResp = new RealNameAuthResp();
realNameAuthResp.setTradeNo(result.getTradeNo());
realNameAuthResp.setSuccess(true);
sink.next(realNameAuthResp);
sink.complete();
} else {
sink.error(new SystemException(ErrorCode.API_ERROR, result.errors()));
}
});
}
}