在javax websockets中,我们可以使用类似下面的内容
Session.getAsyncRemote()。sendText(String text) 。Session.getBasicRemote()sendText();
我们如何使用spring websocket发送异步消息。 从Spring Webscockets的WebSocketSession中我们可以提取RemoteEndPoint并发送异步消息
PS注意:我正在使用Basic Spring websockets ...
配置和代码如下:
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@EnableWebSocket
public class WebMVCConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
private static final String ENDPOINT_URL = "/echo";
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(socketHandler(), ENDPOINT_URL).setAllowedOrigins("*");
}
@Bean
public WebSocketHandler socketHandler() {
return new WebSocketTestHandler();
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public DefaultHandshakeHandler handshakeHandler() {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setInputBufferSize(8192);
policy.setIdleTimeout(600000);
return new DefaultHandshakeHandler(new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy)));
}
public class SpringMVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ApplicationConfig.class, RabbitMQConfig.class, RabbitConnectionFactory.class,
WebPropertyPlaceHolderConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
}
@Configuration
public class WebSocketTestHandler extends TextWebSocketHandler {
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
log.info("Connection is established to Server....:: Session Open : {}", session.isOpen());
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
}
@Override
public void afterConnectionClosed(WebSocketSession curSession, CloseStatus status) throws Exception {
}
}
所以在handleTextMessage(WebSocketSession session,TextMessage message)中{ 在这个方法里面我正在创建多个线程并发送相同的会话对象和一些其他参数。在每个线程内部没有修改任何与会话对象相关的参数但我正在尝试执行
TextMessage socketMessage = new TextMessage(message);
session.sendMessage(socketMessage);
}
所以每个线程都试图使用相同的会话Object发送消息。但是我面临以下错误
java.lang.IllegalStateException: Blocking message pending 10000 for BLOCKING
at org.eclipse.jetty.websocket.common.WebSocketRemoteEndpoint.lockMsg(WebSocketRemoteEndpoint.java:130) ~[websocket-common-9.3.8.v20160314.jar:9.3.8.v20160314]
at org.eclipse.jetty.websocket.common.WebSocketRemoteEndpoint.sendString(WebSocketRemoteEndpoint.java:379) ~[websocket-common-9.3.8.v20160314.jar:9.3.8.v20160314]
at org.springframework.web.socket.adapter.jetty.JettyWebSocketSession.sendTextMessage(JettyWebSocketSession.java:188) ~[spring-websocket-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.socket.adapter.AbstractWebSocketSession.sendMessage(AbstractWebSocketSession.java:105) ~[spring-websocket-4.2.4.RELEASE.jar:4.2.4.RELEASE]
那么可以使用spring websockets发送异步消息吗? 如果是,请告诉我上述代码中需要进行哪些配置更改。或者我们可以从spring Websocket Session中提取核心AsyncRemoteEndPoint和BasicRemoteEndpoint吗?我们是否可以发送异步消息..如果不是以上两种情况都可以..移动代码到共同的位置并进行同步(sessionObject) { 发信息 如果问题的框架不清楚或已经是重复的问题,我们很抱歉
请注意我没有使用任何Stomp客户端或任何其他功能超过弹簧websocket ..使用普通的弹簧websockets ..并且可以不使用Future(java功能)(如果是..会更好) ?
答案 0 :(得分:0)
我在会话中使用了ConcurrentWebSocketSessionDecorator。 根据: https://jira.spring.io/browse/SPR-13602
装饰器“使用发送缓冲区强制一次发送一条消息,并发送每个会话的时间限制。这有助于限制慢速客户端的影响”