Search in sources :

Example 1 with StompFrameHandler

use of org.springframework.messaging.simp.stomp.StompFrameHandler 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)

Example 2 with StompFrameHandler

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

the class WebSocketMessagingAutoConfigurationTests method performStompSubscription.

private Object performStompSubscription(final String topic) throws Throwable {
    EnvironmentTestUtils.addEnvironment(this.context, "server.port:0", "spring.jackson.serialization.indent-output:true");
    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(30000, 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)

Example 3 with StompFrameHandler

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

the class StompInboundChannelAdapter method subscribeDestination.

private void subscribeDestination(final String destination) {
    if (this.stompSession != null) {
        final StompSession.Subscription subscription = this.stompSession.subscribe(destination, new StompFrameHandler() {

            @Override
            public Type getPayloadType(StompHeaders headers) {
                return StompInboundChannelAdapter.this.payloadType;
            }

            @Override
            public void handleFrame(StompHeaders headers, Object body) {
                Message<?> message;
                if (body instanceof Message) {
                    message = (Message<?>) body;
                } else {
                    message = getMessageBuilderFactory().withPayload(body).copyHeaders(StompInboundChannelAdapter.this.headerMapper.toHeaders(headers)).build();
                }
                sendMessage(message);
            }
        });
        if (this.stompSessionManager.isAutoReceiptEnabled()) {
            final ApplicationEventPublisher applicationEventPublisher = this.applicationEventPublisher;
            if (applicationEventPublisher != null) {
                subscription.addReceiptTask(() -> {
                    StompReceiptEvent event = new StompReceiptEvent(StompInboundChannelAdapter.this, destination, subscription.getReceiptId(), StompCommand.SUBSCRIBE, false);
                    applicationEventPublisher.publishEvent(event);
                });
            }
            subscription.addReceiptLostTask(() -> {
                if (applicationEventPublisher != null) {
                    StompReceiptEvent event = new StompReceiptEvent(StompInboundChannelAdapter.this, destination, subscription.getReceiptId(), StompCommand.SUBSCRIBE, true);
                    applicationEventPublisher.publishEvent(event);
                } else {
                    logger.error("The receipt [" + subscription.getReceiptId() + "] is lost for [" + subscription.getSubscriptionId() + "] on destination [" + destination + "]");
                }
            });
        }
        this.subscriptions.put(destination, subscription);
    } else {
        logger.warn("The StompInboundChannelAdapter [" + getComponentName() + "] ins't connected to StompSession. Check the state of [" + this.stompSessionManager + "]");
    }
}
Also used : StompReceiptEvent(org.springframework.integration.stomp.event.StompReceiptEvent) StompFrameHandler(org.springframework.messaging.simp.stomp.StompFrameHandler) Type(java.lang.reflect.Type) StompSession(org.springframework.messaging.simp.stomp.StompSession) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders)

Aggregations

Type (java.lang.reflect.Type)3 StompFrameHandler (org.springframework.messaging.simp.stomp.StompFrameHandler)3 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)3 StompSession (org.springframework.messaging.simp.stomp.StompSession)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ServerPortInfoApplicationContextInitializer (org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer)2 SimpleMessageConverter (org.springframework.messaging.converter.SimpleMessageConverter)2 StompCommand (org.springframework.messaging.simp.stomp.StompCommand)2 StompSessionHandler (org.springframework.messaging.simp.stomp.StompSessionHandler)2 StompSessionHandlerAdapter (org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)2 WebSocketStompClient (org.springframework.web.socket.messaging.WebSocketStompClient)2 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 StompReceiptEvent (org.springframework.integration.stomp.event.StompReceiptEvent)1 Message (org.springframework.messaging.Message)1 ErrorMessage (org.springframework.messaging.support.ErrorMessage)1