STOMP-over-Websocket で RPC のようなセマンティックを実装する @SubscribeMapping アプローチが本当に気に入っています。
残念ながら、その「魔法」では、注釈付きのメソッドが値を返す必要があります。しかし、戻り値がすぐに利用できない場合はどうなるでしょうか? それを待っているメソッド内でのブロックを避けたい。代わりに、準備ができたときに値を発行するコールバックを渡したいと思います。これを行うには、コールバック内でメッセージング テンプレートの convertAndSendToUser() を使用できると考えました。@SubscribeMapping 処理は非常に特殊であり、SimpMessageSendingOperations のインスタンスでは不可能であることが判明しました。
SubscriptionMethodReturnValueHandler で handleReturnValue() を呼び出すことで目標を達成できましたが、これの全体的な仕組みは、ハックではないにしても非常に面倒です (MethodParameter のダミー インスタンスを handleReturnValue() に提供するなど)。
public class MessageController {
private final SubscriptionMethodReturnValueHandler subscriptionMethodReturnValueHandler;
@Autowired
public MessageController(SimpAnnotationMethodMessageHandler annotationMethodMessageHandler) {
SubscriptionMethodReturnValueHandler subscriptionMethodReturnValueHandler = null;
for (HandlerMethodReturnValueHandler returnValueHandler : annotationMethodMessageHandler.getReturnValueHandlers()) {
if (returnValueHandler instanceof SubscriptionMethodReturnValueHandler) {
subscriptionMethodReturnValueHandler = (SubscriptionMethodReturnValueHandler) returnValueHandler;
break;
}
}
this.subscriptionMethodReturnValueHandler = subscriptionMethodReturnValueHandler;
}
@SubscribeMapping("/greeting/{name}")
public void greet(@DestinationVariable String name, Message<?> message) throws Exception {
subscriptionMethodReturnValueHandler.handleReturnValue("Hello " + name, new MethodParameter(Object.class.getMethods()[0], -1), message);
}
}
だから私の質問は簡単です: より良い方法はありますか?