使用STOMP简单代理时的错误处理 [英] Error handling while using STOMP simple broker

查看:832
本文介绍了使用STOMP简单代理时的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用@EnableWebSocketMessageBroker创建简单的STOMP代理.当@MessageMapping方法中发生RuntimeException时,我想接收STOMP ERROR帧,但是默认情况下它不起作用.

I am creating simple STOMP broker using @EnableWebSocketMessageBroker. When RuntimeException happens in @MessageMapping method I would like to receive STOMP ERROR frame, but it doesn't work like that by default.

似乎简单代理中不支持ERROR框架: https://github.com/spring-projects/spring-framework/blob/master/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageType.java# L28-L44

It seems that ERROR frame is not supported in simple broker: https://github.com/spring-projects/spring-framework/blob/master/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageType.java#L28-L44

另一方面,有一种机制可以发送spring-websocket中已经可用的ERROR帧,并且其代码引用了一些与简单代理相关的类: https://github.com/spring-projects/spring-framework/blob/master/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler. java#L349

On the other hand, there is a mechanism to send ERROR frames already available in spring-websocket and its code references some classes which are related to simple broker: https://github.com/spring-projects/spring-framework/blob/master/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java#L349

如何启用在@MessageMapping方法中发生RuntimeException时发送ERROR帧的机制?

How to enable mechanism of sending ERROR frames when RuntimeException happens in @MessageMapping method?

推荐答案

执行此操作的快速简便的方法是,在对@MessageMapping端点进行任何调用之前,先在客户端上分配特定的错误处理程序订阅.然后,使用try catch块将对方法的所有调用包装在您的websocket服务器中,并处理发生的异常.然后是做类似这样的简单情况:

Quick and easy way to do this is to have a specific error handler subscription on the client that subsribes before making any calls to the @MessageMapping endpoints. Then wrap all calls to the method in your websocket server with a try catch block and handle the exceptions that occur. Then its a simple case of doing something like:

messagingTemplate.convertAndSend( "/topic/clientControl/1234", "SHUT_DOWN"); (or send to user etc, depends on your subscription model)

您可以向客户端发送错误消息并允许其处理,也可以决定在服务器端执行什么操作并进行控制订阅,以允许服务器将命令传递给客户端.

You can either send an error message to the client and allow it to handle it or decide what to do server side and have a control subscription allowing the server to pass commands to the client.

这篇关于使用STOMP简单代理时的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆