Search in sources :

Example 1 with StompHeaders

use of org.springframework.messaging.simp.stomp.StompHeaders in project spring-integration by spring-projects.

the class StompSessionManagerTests method testDoConnectFailure.

@Test
public void testDoConnectFailure() throws Exception {
    StompClientSupport stompClient = mock(StompClientSupport.class);
    stompClient.setTaskScheduler(new ConcurrentTaskScheduler());
    AbstractStompSessionManager sessionManager = new AbstractStompSessionManager(stompClient) {

        private final AtomicBoolean thrown = new AtomicBoolean();

        @Override
        protected ListenableFuture<StompSession> doConnect(StompSessionHandler handler) {
            if (!this.thrown.getAndSet(true)) {
                throw new RuntimeException("intentional");
            } else {
                SettableListenableFuture<StompSession> future = new SettableListenableFuture<StompSession>();
                StompSession stompSession = mock(StompSession.class);
                future.set(stompSession);
                handler.afterConnected(stompSession, getConnectHeaders());
                return future;
            }
        }
    };
    sessionManager.start();
    final SettableListenableFuture<StompSession> stompSessionFuture = new SettableListenableFuture<StompSession>();
    sessionManager.connect(new StompSessionHandlerAdapter() {

        @Override
        public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
            stompSessionFuture.set(session);
        }
    });
    assertNotNull(stompSessionFuture.get(10, TimeUnit.SECONDS));
    sessionManager.stop();
}
Also used : ConcurrentTaskScheduler(org.springframework.scheduling.concurrent.ConcurrentTaskScheduler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) StompClientSupport(org.springframework.messaging.simp.stomp.StompClientSupport) StompSession(org.springframework.messaging.simp.stomp.StompSession) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) StompSessionHandlerAdapter(org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter) Test(org.junit.Test)

Example 2 with StompHeaders

use of org.springframework.messaging.simp.stomp.StompHeaders in project spring-framework by spring-projects.

the class WebSocketStompClientTests method heartbeatDefaultValueWithScheduler.

@Test
public void heartbeatDefaultValueWithScheduler() throws Exception {
    WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
    stompClient.setTaskScheduler(mock(TaskScheduler.class));
    assertThat(stompClient.getDefaultHeartbeat()).isEqualTo(new long[] { 10000, 10000 });
    StompHeaders connectHeaders = stompClient.processConnectHeaders(null);
    assertThat(connectHeaders.getHeartbeat()).isEqualTo(new long[] { 10000, 10000 });
}
Also used : WebSocketClient(org.springframework.web.socket.client.WebSocketClient) TaskScheduler(org.springframework.scheduling.TaskScheduler) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) Test(org.junit.jupiter.api.Test)

Example 3 with StompHeaders

use of org.springframework.messaging.simp.stomp.StompHeaders in project spring-framework by spring-projects.

the class WebSocketStompClientTests method heartbeatDefaultValue.

@Test
public void heartbeatDefaultValue() throws Exception {
    WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
    assertThat(stompClient.getDefaultHeartbeat()).isEqualTo(new long[] { 0, 0 });
    StompHeaders connectHeaders = stompClient.processConnectHeaders(null);
    assertThat(connectHeaders.getHeartbeat()).isEqualTo(new long[] { 0, 0 });
}
Also used : WebSocketClient(org.springframework.web.socket.client.WebSocketClient) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) Test(org.junit.jupiter.api.Test)

Example 4 with StompHeaders

use of org.springframework.messaging.simp.stomp.StompHeaders in project spring-framework by spring-projects.

the class WebSocketStompClientTests method handleWebSocketMessageSplitAcrossTwoMessage.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void handleWebSocketMessageSplitAcrossTwoMessage() throws Exception {
    WebSocketHandler webSocketHandler = connect();
    String part1 = "SEND\na:alpha\n\nMessage";
    webSocketHandler.handleMessage(this.webSocketSession, new TextMessage(part1));
    verifyNoMoreInteractions(this.stompSession);
    String part2 = " payload\0";
    webSocketHandler.handleMessage(this.webSocketSession, new TextMessage(part2));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(this.stompSession).handleMessage(captor.capture());
    Message<byte[]> message = captor.getValue();
    assertThat(message).isNotNull();
    StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
    assertThat(accessor.getCommand()).isEqualTo(StompCommand.SEND);
    assertThat(headers.getFirst("a")).isEqualTo("alpha");
    assertThat(new String(message.getPayload(), StandardCharsets.UTF_8)).isEqualTo("Message payload");
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) PongMessage(org.springframework.web.socket.PongMessage) Message(org.springframework.messaging.Message) BinaryMessage(org.springframework.web.socket.BinaryMessage) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 5 with StompHeaders

use of org.springframework.messaging.simp.stomp.StompHeaders in project spring-boot by spring-projects.

the class WebSocketMessagingAutoConfigurationTests method performStompSubscription.

private Object performStompSubscription(String topic) throws Throwable {
    TestPropertyValues.of("server.port:0", "spring.jackson.serialization.indent-output:true").applyTo(this.context);
    this.context.register(WebSocketMessagingConfiguration.class);
    new ServerPortInfoApplicationContextInitializer().initialize(this.context);
    this.context.refresh();
    WebSocketStompClient stompClient = new WebSocketStompClient(this.sockJsClient);
    final AtomicReference<Throwable> failure = new AtomicReference<>();
    final AtomicReference<Object> result = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    StompSessionHandler handler = new StompSessionHandlerAdapter() {

        @Override
        public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
            session.subscribe(topic, new StompFrameHandler() {

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    result.set(payload);
                    latch.countDown();
                }

                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return Object.class;
                }
            });
        }

        @Override
        public void handleFrame(StompHeaders headers, Object payload) {
            latch.countDown();
        }

        @Override
        public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
            failure.set(exception);
            latch.countDown();
        }

        @Override
        public void handleTransportError(StompSession session, Throwable exception) {
            failure.set(exception);
            latch.countDown();
        }
    };
    stompClient.setMessageConverter(new SimpleMessageConverter());
    stompClient.connect("ws://localhost:{port}/messaging", handler, this.context.getEnvironment().getProperty("local.server.port"));
    if (!latch.await(30, TimeUnit.SECONDS)) {
        if (failure.get() != null) {
            throw failure.get();
        }
        fail("Response was not received within 30 seconds");
    }
    return result.get();
}
Also used : ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) StompFrameHandler(org.springframework.messaging.simp.stomp.StompFrameHandler) StompSession(org.springframework.messaging.simp.stomp.StompSession) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) StompCommand(org.springframework.messaging.simp.stomp.StompCommand) Type(java.lang.reflect.Type) WebSocketStompClient(org.springframework.web.socket.messaging.WebSocketStompClient) SimpleMessageConverter(org.springframework.messaging.converter.SimpleMessageConverter) StompSessionHandlerAdapter(org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)

Aggregations

StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)11 StompSession (org.springframework.messaging.simp.stomp.StompSession)6 Test (org.junit.jupiter.api.Test)5 Message (org.springframework.messaging.Message)4 Type (java.lang.reflect.Type)3 StompFrameHandler (org.springframework.messaging.simp.stomp.StompFrameHandler)3 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)3 StompSessionHandler (org.springframework.messaging.simp.stomp.StompSessionHandler)3 StompSessionHandlerAdapter (org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)3 BinaryMessage (org.springframework.web.socket.BinaryMessage)3 PongMessage (org.springframework.web.socket.PongMessage)3 TextMessage (org.springframework.web.socket.TextMessage)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2 ServerPortInfoApplicationContextInitializer (org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer)2 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)2 StompReceiptEvent (org.springframework.integration.stomp.event.StompReceiptEvent)2 SimpleMessageConverter (org.springframework.messaging.converter.SimpleMessageConverter)2 StompCommand (org.springframework.messaging.simp.stomp.StompCommand)2