Spring 当返回值被延迟时,为类似RPC的行为使用@SubscribeMapping带注释的方法
我非常喜欢@SubscribeMapping方法,它使用STOMP over Websocket实现类似RPC的语义 不幸的是,它的“魔力”要求带注释的方法返回一个值。但是,如果返回值不可用怎么办?我希望避免在等待它的方法内部阻塞。相反,我希望传递一个回调,当它准备好时,它将发布一个值。我想我可以在回调中使用消息传递模板的convertandtouser()来实现这一点。事实证明@SubscribeMapping处理非常特殊,对于SimpMessageSendingOperations实例是不可能的 我能够通过对SubscriptionMethodReturnValueHandler调用HandlerReturnValue()来实现我的目标,但这一过程的整体机制非常繁琐(如果不是黑客的话)(比如为HandlerReturnValue()提供MethodParameter的虚拟实例):Spring 当返回值被延迟时,为类似RPC的行为使用@SubscribeMapping带注释的方法,spring,spring-websocket,spring-messaging,Spring,Spring Websocket,Spring Messaging,我非常喜欢@SubscribeMapping方法,它使用STOMP over Websocket实现类似RPC的语义 不幸的是,它的“魔力”要求带注释的方法返回一个值。但是,如果返回值不可用怎么办?我希望避免在等待它的方法内部阻塞。相反,我希望传递一个回调,当它准备好时,它将发布一个值。我想我可以在回调中使用消息传递模板的convertandtouser()来实现这一点。事实证明@SubscribeMapping处理非常特殊,对于SimpMessageSendingOperations实例是不可
公共类消息控制器{
私有最终订阅MethodReturnValueHandler订阅MethodReturnValueHandler;
@自动连线
public MessageController(SimpAnnotationMethodMessageHandler注释MethodMessageHandler){
SubscriptionMethodReturnValueHandler SubscriptionMethodReturnValueHandler=null;
对于(HandlerMethodReturnValueHandler returnValueHandler:annotationMethodMessageHandler.GetReturnValueHandler()){
if(returnValueHandler SubscriptionMethodReturnValueHandler实例){
subscriptionMethodReturnValueHandler=(subscriptionMethodReturnValueHandler)returnValueHandler;
打破
}
}
this.subscriptionMethodReturnValueHandler=subscriptionMethodReturnValueHandler;
}
@SubscribeMapping(“/greeting/{name}”)
public void greet(@DestinationVariable String name,Message Message)引发异常{
subscriptionMethodReturnValueHandler.HandlerReturnValue(“Hello”+名称,新方法参数(Object.class.getMethods()[0],-1),消息);
}
}
所以我的问题很简单:有更好的方法吗
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);
}
}