Search in sources :

Example 1 with ConcurrentWebSocketSessionDecorator

use of org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator in project spring-framework by spring-projects.

the class OrderedMessageSendingIntegrationTests method sendAfterBlockedSend.

@Test
void sendAfterBlockedSend() throws InterruptedException {
    int messageCount = 1000;
    ConcurrentWebSocketSessionDecorator concurrentSessionDecorator = new ConcurrentWebSocketSessionDecorator(this.blockingSession, 60 * 1000, messageCount * MESSAGE_SIZE);
    TestMessageHandler handler = new TestMessageHandler(concurrentSessionDecorator);
    subscribableChannel.subscribe(handler);
    List<Message<?>> expectedMessages = new ArrayList<>(messageCount);
    // Send one to block
    Message<byte[]> message = createMessage(0);
    expectedMessages.add(message);
    this.orderedMessageChannel.send(message);
    CountDownLatch latch = new CountDownLatch(messageCount);
    handler.setMessageLatch(latch);
    for (int i = 1; i <= messageCount; i++) {
        message = createMessage(i);
        expectedMessages.add(message);
        this.orderedMessageChannel.send(message);
    }
    latch.await(5, TimeUnit.SECONDS);
    assertThat(concurrentSessionDecorator.getTimeSinceSendStarted() > 0).isTrue();
    assertThat(concurrentSessionDecorator.getBufferSize()).isEqualTo((messageCount * MESSAGE_SIZE));
    assertThat(handler.getSavedMessages()).containsExactlyElementsOf(expectedMessages);
    assertThat(blockingSession.isOpen()).isTrue();
}
Also used : Message(org.springframework.messaging.Message) ConcurrentWebSocketSessionDecorator(org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with ConcurrentWebSocketSessionDecorator

use of org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator in project spring-framework by spring-projects.

the class OrderedMessageSendingIntegrationTests method exceedTimeLimit.

@Test
void exceedTimeLimit() throws InterruptedException {
    ConcurrentWebSocketSessionDecorator concurrentSessionDecorator = new ConcurrentWebSocketSessionDecorator(this.blockingSession, 100, 1024);
    TestMessageHandler messageHandler = new TestMessageHandler(concurrentSessionDecorator);
    subscribableChannel.subscribe(messageHandler);
    // Send one to block
    this.orderedMessageChannel.send(createMessage(0));
    // Exceed send time..
    Thread.sleep(200);
    CountDownLatch messageLatch = new CountDownLatch(1);
    messageHandler.setMessageLatch(messageLatch);
    // Send one more
    this.orderedMessageChannel.send(createMessage(1));
    messageLatch.await(5, TimeUnit.SECONDS);
    assertThat(messageHandler.getSavedException()).hasMessageMatching("Send time [\\d]+ \\(ms\\) for session '1' exceeded the allowed limit 100");
}
Also used : ConcurrentWebSocketSessionDecorator(org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 3 with ConcurrentWebSocketSessionDecorator

use of org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator in project roof-im by madfroglx.

the class WebSocketRequestEnterPoint method afterConnectionEstablished.

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    ConcurrentWebSocketSessionDecorator socketSessionDecorator = new ConcurrentWebSocketSessionDecorator(session, getSendTimeLimit(), getSendBufferSizeLimit());
    webSocketSessionConnectManager.put(session.getId(), socketSessionDecorator);
}
Also used : ConcurrentWebSocketSessionDecorator(org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator)

Example 4 with ConcurrentWebSocketSessionDecorator

use of org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator in project spring-framework by spring-projects.

the class SubProtocolWebSocketHandler method afterConnectionEstablished.

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    // WebSocketHandlerDecorator could close the session
    if (!session.isOpen()) {
        return;
    }
    this.stats.incrementSessionCount(session);
    session = new ConcurrentWebSocketSessionDecorator(session, getSendTimeLimit(), getSendBufferSizeLimit());
    this.sessions.put(session.getId(), new WebSocketSessionHolder(session));
    findProtocolHandler(session).afterSessionStarted(session, this.clientInboundChannel);
}
Also used : ConcurrentWebSocketSessionDecorator(org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator)

Example 5 with ConcurrentWebSocketSessionDecorator

use of org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator in project spring-framework by spring-projects.

the class StompSubProtocolHandler method handleMessageToClient.

/**
 * Handle STOMP messages going back out to WebSocket clients.
 */
@Override
@SuppressWarnings("unchecked")
public void handleMessageToClient(WebSocketSession session, Message<?> message) {
    if (!(message.getPayload() instanceof byte[])) {
        if (logger.isErrorEnabled()) {
            logger.error("Expected byte[] payload. Ignoring " + message + ".");
        }
        return;
    }
    StompHeaderAccessor accessor = getStompHeaderAccessor(message);
    StompCommand command = accessor.getCommand();
    if (StompCommand.MESSAGE.equals(command)) {
        if (accessor.getSubscriptionId() == null && logger.isWarnEnabled()) {
            logger.warn("No STOMP \"subscription\" header in " + message);
        }
        String origDestination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
        if (origDestination != null) {
            accessor = toMutableAccessor(accessor, message);
            accessor.removeNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
            accessor.setDestination(origDestination);
        }
    } else if (StompCommand.CONNECTED.equals(command)) {
        this.stats.incrementConnectedCount();
        accessor = afterStompSessionConnected(message, accessor, session);
        if (this.eventPublisher != null) {
            try {
                SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes());
                SimpAttributesContextHolder.setAttributes(simpAttributes);
                Principal user = getUser(session);
                publishEvent(this.eventPublisher, new SessionConnectedEvent(this, (Message<byte[]>) message, user));
            } finally {
                SimpAttributesContextHolder.resetAttributes();
            }
        }
    }
    byte[] payload = (byte[]) message.getPayload();
    if (StompCommand.ERROR.equals(command) && getErrorHandler() != null) {
        Message<byte[]> errorMessage = getErrorHandler().handleErrorMessageToClient((Message<byte[]>) message);
        if (errorMessage != null) {
            accessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class);
            Assert.state(accessor != null, "No StompHeaderAccessor");
            payload = errorMessage.getPayload();
        }
    }
    Runnable task = OrderedMessageChannelDecorator.getNextMessageTask(message);
    if (task != null) {
        Assert.isInstanceOf(ConcurrentWebSocketSessionDecorator.class, session);
        ((ConcurrentWebSocketSessionDecorator) session).setMessageCallback(m -> task.run());
    }
    sendToClient(session, accessor, payload);
}
Also used : SimpAttributes(org.springframework.messaging.simp.SimpAttributes) ConcurrentWebSocketSessionDecorator(org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompCommand(org.springframework.messaging.simp.stomp.StompCommand) Principal(java.security.Principal)

Aggregations

ConcurrentWebSocketSessionDecorator (org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator)6 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.jupiter.api.Test)3 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 Message (org.springframework.messaging.Message)1 SimpAttributes (org.springframework.messaging.simp.SimpAttributes)1 StompCommand (org.springframework.messaging.simp.stomp.StompCommand)1 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)1