这是我可以详细说明的最佳想法,以在 REST 调用中获得与 Reactor 的异步性,以提高可用性:我不满意..关于如何做得更好的任何想法?
休息接口:
@RestController
public class REST_Interface {
@Autowired
public EventBus eventBus;
@RequestMapping("/getGreeting/{name}")
public DeferredResult<String> getGreeting(@PathVariable String name) {
DeferredResult<String> result = new DeferredResult<>();
EventWrapper<String> wrapper = new EventWrapper<>(result, name);
eventBus.notify("greeting.topic", Event.wrap(wrapper));
return result;
}
}
事件包装器:
public class EventWrapper<T> {
private DeferredResult<T> deferredResult;
private T payload;
public EventWrapper(DeferredResult<T> deferredResult, T payload) {
this.deferredResult = deferredResult;
this.payload = payload;
}
// Getters & Setters
}
事件消费者:
@Consumer
public class EventConsumer {
@Autowired
public EventBus eventBus;
@Selector("greeting.topic")
public void onTestTopic(Object o) {
System.out.println("Event receved for Logging " + o.toString());
}
@Selector("greeting.topic")
public void wrappedGreeter(EventWrapper<String> ew) {
ew.getDeferredResult().setResult("Ciao " + ow.getPayload().toUpperCase());
}
}