Search in sources :

Example 6 with StompSession

use of org.springframework.messaging.simp.stomp.StompSession in project elastest-torm by elastest.

the class DockerServiceItTest method connectToRabbitQueue.

private WaitForMessagesHandler connectToRabbitQueue(String queueId) throws InterruptedException, ExecutionException, TimeoutException {
    StompSession stompSession = connectToRabbitMQ(serverPort);
    String queueToSuscribe = "/topic/" + queueId;
    log.info("Container log queue '" + queueToSuscribe + "'");
    WaitForMessagesHandler handler = new WaitForMessagesHandler("1");
    stompSession.subscribe(queueToSuscribe, handler);
    return handler;
}
Also used : StompSession(org.springframework.messaging.simp.stomp.StompSession) WaitForMessagesHandler(io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler)

Example 7 with StompSession

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

Example 8 with StompSession

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

the class StompMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(final Message<?> message) throws Exception {
    try {
        connectIfNecessary();
    } catch (Exception e) {
        throw new MessageDeliveryException(message, "The [" + this + "] could not deliver message.", e);
    }
    StompSession stompSession = this.stompSession;
    StompHeaders stompHeaders = new StompHeaders();
    this.headerMapper.fromHeaders(message.getHeaders(), stompHeaders);
    if (stompHeaders.getDestination() == null) {
        Assert.state(this.destinationExpression != null, "One of 'destination' or 'destinationExpression' must be" + " provided, if message header doesn't supply 'destination' STOMP header.");
        String destination = this.destinationExpression.getValue(this.evaluationContext, message, String.class);
        stompHeaders.setDestination(destination);
    }
    final StompSession.Receiptable receiptable = stompSession.send(stompHeaders, message.getPayload());
    if (receiptable.getReceiptId() != null) {
        final String destination = stompHeaders.getDestination();
        final ApplicationEventPublisher applicationEventPublisher = this.applicationEventPublisher;
        if (applicationEventPublisher != null) {
            receiptable.addReceiptTask(() -> {
                StompReceiptEvent event = new StompReceiptEvent(StompMessageHandler.this, destination, receiptable.getReceiptId(), StompCommand.SEND, false);
                event.setMessage(message);
                applicationEventPublisher.publishEvent(event);
            });
        }
        receiptable.addReceiptLostTask(() -> {
            if (applicationEventPublisher != null) {
                StompReceiptEvent event = new StompReceiptEvent(StompMessageHandler.this, destination, receiptable.getReceiptId(), StompCommand.SEND, true);
                event.setMessage(message);
                applicationEventPublisher.publishEvent(event);
            } else {
                logger.error("The receipt [" + receiptable.getReceiptId() + "] is lost for [" + message + "] on destination [" + destination + "]");
            }
        });
    }
}
Also used : StompReceiptEvent(org.springframework.integration.stomp.event.StompReceiptEvent) StompSession(org.springframework.messaging.simp.stomp.StompSession) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) MessagingException(org.springframework.messaging.MessagingException) ConnectionLostException(org.springframework.messaging.simp.stomp.ConnectionLostException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 9 with StompSession

use of org.springframework.messaging.simp.stomp.StompSession in project tutorials by eugenp.

the class MyStompSessionHandlerIntegrationTest method givenValidSession_whenConnected_SendsMessage.

@Test
public void givenValidSession_whenConnected_SendsMessage() {
    StompSession mockSession = Mockito.mock(StompSession.class);
    StompHeaders mockHeader = Mockito.mock(StompHeaders.class);
    MyStompSessionHandler sessionHandler = new MyStompSessionHandler();
    sessionHandler.afterConnected(mockSession, mockHeader);
    Mockito.verify(mockSession).subscribe("/topic/messages", sessionHandler);
    Mockito.verify(mockSession).send(Mockito.anyString(), Mockito.anyObject());
}
Also used : StompSession(org.springframework.messaging.simp.stomp.StompSession) MyStompSessionHandler(org.baeldung.websocket.client.MyStompSessionHandler) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) Test(org.junit.Test)

Example 10 with StompSession

use of org.springframework.messaging.simp.stomp.StompSession in project yaps-client by serdaroquai.

the class RemoteConnectionManager method connect.

@Scheduled(fixedDelay = 10000)
private void connect() throws InterruptedException, ExecutionException, JSONException {
    if (isConnected.compareAndSet(false, true)) {
        try {
            logger.info(String.format("Establishing websocket connection to %s", remoteUrl));
            WebSocketHttpHeaders handshakeHeaders = new WebSocketHttpHeaders();
            handshakeHeaders.set("token", token);
            handshakeHeaders.set("userId", userId);
            ListenableFuture<StompSession> connect = stompClient.connect(remoteUrl, handshakeHeaders, this);
            stompSession = connect.get();
        } catch (Exception e) {
            logger.error(String.format("Could not connect to %s", remoteUrl));
            disconnect();
        }
    }
}
Also used : StompSession(org.springframework.messaging.simp.stomp.StompSession) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) JSONException(org.json.JSONException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

StompSession (org.springframework.messaging.simp.stomp.StompSession)10 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)6 StompSessionHandler (org.springframework.messaging.simp.stomp.StompSessionHandler)4 Type (java.lang.reflect.Type)3 StompFrameHandler (org.springframework.messaging.simp.stomp.StompFrameHandler)3 StompSessionHandlerAdapter (org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)3 WebSocketStompClient (org.springframework.web.socket.messaging.WebSocketStompClient)3 WaitForMessagesHandler (io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)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 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TJob (io.elastest.etm.model.TJob)1 TJobExecution (io.elastest.etm.model.TJobExecution)1