Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 当返回值被延迟时,为类似RPC的行为使用@SubscribeMapping带注释的方法_Spring_Spring Websocket_Spring Messaging - Fatal编程技术网

Spring 当返回值被延迟时,为类似RPC的行为使用@SubscribeMapping带注释的方法

Spring 当返回值被延迟时,为类似RPC的行为使用@SubscribeMapping带注释的方法,spring,spring-websocket,spring-messaging,Spring,Spring Websocket,Spring Messaging,我非常喜欢@SubscribeMapping方法,它使用STOMP over Websocket实现类似RPC的语义 不幸的是,它的“魔力”要求带注释的方法返回一个值。但是,如果返回值不可用怎么办?我希望避免在等待它的方法内部阻塞。相反,我希望传递一个回调,当它准备好时,它将发布一个值。我想我可以在回调中使用消息传递模板的convertandtouser()来实现这一点。事实证明@SubscribeMapping处理非常特殊,对于SimpMessageSendingOperations实例是不可

我非常喜欢@SubscribeMapping方法,它使用STOMP over Websocket实现类似RPC的语义

不幸的是,它的“魔力”要求带注释的方法返回一个值。但是,如果返回值不可用怎么办?我希望避免在等待它的方法内部阻塞。相反,我希望传递一个回调,当它准备好时,它将发布一个值。我想我可以在回调中使用消息传递模板的convertandtouser()来实现这一点。事实证明@SubscribeMapping处理非常特殊,对于SimpMessageSendingOperations实例是不可能的

我能够通过对SubscriptionMethodReturnValueHandler调用HandlerReturnValue()来实现我的目标,但这一过程的整体机制非常繁琐(如果不是黑客的话)(比如为HandlerReturnValue()提供MethodParameter的虚拟实例):

公共类消息控制器{
私有最终订阅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);
    }

}