Example usage for org.springframework.messaging.simp SimpMessageType MESSAGE

List of usage examples for org.springframework.messaging.simp SimpMessageType MESSAGE

Introduction

In this page you can find the example usage for org.springframework.messaging.simp SimpMessageType MESSAGE.

Prototype

SimpMessageType MESSAGE

To view the source code for org.springframework.messaging.simp SimpMessageType MESSAGE.

Click Source Link

Usage

From source file:de.metas.ui.web.debug.DebugRestController.java

@PostMapping("/websocket/post")
public void postToWebsocket(@RequestParam("endpoint") final String endpoint,
        @RequestBody final String messageStr) {
    final Charset charset = Charset.forName("UTF-8");
    final Map<String, Object> headers = ImmutableMap.<String, Object>builder()
            .put("simpMessageType", SimpMessageType.MESSAGE)
            .put("contentType", new MimeType("application", "json", charset)).build();
    final Message<?> message = new GenericMessage<>(messageStr.getBytes(charset), headers);
    websocketMessagingTemplate.send(endpoint, message);
}

From source file:org.springframework.cloud.stream.app.websocket.sink.WebsocketSinkConfiguration.java

@ServiceActivator(inputChannel = Sink.INPUT)
public void websocketSink(Message<?> message) {
    if (logger.isTraceEnabled()) {
        logger.trace(String.format("Handling message: %s", message));
    }/*from  w  w  w  . java  2  s  .  c  o  m*/

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    headers.setMessageTypeIfNotSet(SimpMessageType.MESSAGE);
    String messagePayload = message.getPayload().toString();
    for (Channel channel : WebsocketSinkServer.channels) {
        if (logger.isTraceEnabled()) {
            logger.trace(
                    String.format("Writing message %s to channel %s", messagePayload, channel.localAddress()));
        }

        channel.write(new TextWebSocketFrame(messagePayload));
        channel.flush();
    }

    if (traceEndpointEnabled) {
        addMessageToTraceRepository(message);
    }
}

From source file:org.springframework.messaging.simp.annotation.support.SubscriptionMethodReturnValueHandler.java

private MessageHeaders createHeaders(@Nullable String sessionId, String subscriptionId,
        MethodParameter returnType) {/*www . j  a v  a2s.c o m*/
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    if (getHeaderInitializer() != null) {
        getHeaderInitializer().initHeaders(accessor);
    }
    if (sessionId != null) {
        accessor.setSessionId(sessionId);
    }
    accessor.setSubscriptionId(subscriptionId);
    accessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
    accessor.setLeaveMutable(true);
    return accessor.getMessageHeaders();
}

From source file:org.springframework.messaging.simp.broker.AbstractSubscriptionRegistry.java

@Override
public final MultiValueMap<String, String> findSubscriptions(Message<?> message) {
    MessageHeaders headers = message.getHeaders();

    SimpMessageType type = SimpMessageHeaderAccessor.getMessageType(headers);
    if (!SimpMessageType.MESSAGE.equals(type)) {
        throw new IllegalArgumentException("Unexpected message type: " + type);
    }/*w  ww  .  j av  a  2 s  .  c  o m*/

    String destination = SimpMessageHeaderAccessor.getDestination(headers);
    if (destination == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No destination in " + message);
        }
        return EMPTY_MAP;
    }

    return findSubscriptionsInternal(destination, message);
}

From source file:org.springframework.messaging.simp.broker.OrderedMessageSenderTests.java

@Test
public void test() throws InterruptedException {

    int start = 1;
    int end = 1000;

    AtomicInteger index = new AtomicInteger(start);
    AtomicReference<Object> result = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);

    this.channel.subscribe(message -> {
        int expected = index.getAndIncrement();
        Integer actual = (Integer) message.getHeaders().getOrDefault("seq", -1);
        if (actual != expected) {
            result.set("Expected: " + expected + ", but was: " + actual);
            latch.countDown();//  w w w  . j av a 2s .  co  m
            return;
        }
        if (actual == 100 || actual == 200) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                result.set(ex.toString());
                latch.countDown();
            }
        }
        if (actual == end) {
            result.set("Done");
            latch.countDown();
        }
    });

    for (int i = start; i <= end; i++) {
        SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
        accessor.setHeader("seq", i);
        accessor.setLeaveMutable(true);
        this.sender.send(MessageBuilder.createMessage("payload", accessor.getMessageHeaders()));
    }

    latch.await(10, TimeUnit.SECONDS);
    assertEquals("Done", result.get());
}

From source file:org.springframework.messaging.simp.handler.AbstractSubscriptionRegistry.java

@Override
public final MultiValueMap<String, String> findSubscriptions(Message<?> message) {
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    if (!SimpMessageType.MESSAGE.equals(headers.getMessageType())) {
        logger.error("Unexpected message type: " + message);
        return null;
    }/*from ww  w.ja  v a2s .  c  om*/
    String destination = headers.getDestination();
    if (destination == null) {
        logger.error("Ignoring destination. No destination in message: " + message);
        return null;
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Find subscriptions, destination=" + headers.getDestination());
    }
    return findSubscriptionsInternal(destination, message);
}

From source file:org.springframework.messaging.simp.handler.AnnotationMethodMessageHandler.java

@Override
public void handleMessage(Message<?> message) throws MessagingException {

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    SimpMessageType messageType = headers.getMessageType();

    if (SimpMessageType.MESSAGE.equals(messageType)) {
        handleMessageInternal(message, this.messageMethods);
    } else if (SimpMessageType.SUBSCRIBE.equals(messageType)) {
        handleMessageInternal(message, this.subscribeMethods);
    } else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        handleMessageInternal(message, this.unsubscribeMethods);
    }/*from  w  ww  . jav a2 s .  c  o  m*/
}

From source file:org.springframework.messaging.simp.handler.DefaultUserDestinationResolver.java

private UserDestinationInfo getUserDestinationInfo(SimpMessageHeaderAccessor headers) {

    String destination = headers.getDestination();

    String targetUser;//from  ww w  . ja v  a 2  s  .c  o  m
    String targetDestination;

    Principal user = headers.getUser();
    SimpMessageType messageType = headers.getMessageType();

    if (SimpMessageType.SUBSCRIBE.equals(messageType) || SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        if (!checkDestination(destination, this.subscriptionDestinationPrefix)) {
            return null;
        }
        if (user == null) {
            logger.warn("Ignoring message, no user information");
            return null;
        }
        targetUser = user.getName();
        targetDestination = destination.substring(this.destinationPrefix.length() - 1);
    } else if (SimpMessageType.MESSAGE.equals(messageType)) {
        if (!checkDestination(destination, this.destinationPrefix)) {
            return null;
        }
        int startIndex = this.destinationPrefix.length();
        int endIndex = destination.indexOf('/', startIndex);
        Assert.isTrue(endIndex > 0, "Expected destination pattern \"/user/{userId}/**\"");
        targetUser = destination.substring(startIndex, endIndex);
        targetDestination = destination.substring(endIndex);

    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("Ignoring " + messageType + " message");
        }
        return null;
    }

    return new UserDestinationInfo(targetUser, targetDestination);
}

From source file:org.springframework.messaging.simp.handler.SimpleBrokerMessageHandler.java

@Override
public void handleMessage(Message<?> message) throws MessagingException {

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    SimpMessageType messageType = headers.getMessageType();
    String destination = headers.getDestination();

    if (!checkDestinationPrefix(destination)) {
        return;//from w  ww.  ja v  a  2  s. co m
    }

    if (SimpMessageType.SUBSCRIBE.equals(messageType)) {
        preProcessMessage(message);
        this.subscriptionRegistry.registerSubscription(message);
    } else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        preProcessMessage(message);
        this.subscriptionRegistry.unregisterSubscription(message);
    } else if (SimpMessageType.MESSAGE.equals(messageType)) {
        preProcessMessage(message);
        sendMessageToSubscribers(headers.getDestination(), message);
    } else if (SimpMessageType.DISCONNECT.equals(messageType)) {
        preProcessMessage(message);
        String sessionId = SimpMessageHeaderAccessor.wrap(message).getSessionId();
        this.subscriptionRegistry.unregisterAllSubscriptions(sessionId);
    }
}

From source file:org.springframework.messaging.simp.handler.UserDestinationMessageHandler.java

@Override
public void handleMessage(Message<?> message) throws MessagingException {

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    SimpMessageType messageType = headers.getMessageType();
    String destination = headers.getDestination();

    if (!SimpMessageType.MESSAGE.equals(messageType)) {
        return;/*from  www  .j  a v a 2 s.  c  o  m*/
    }

    if (!checkDestination(destination)) {
        return;
    }

    if (logger.isTraceEnabled()) {
        logger.trace("Processing message to destination " + destination);
    }

    UserDestinationParser destinationParser = new UserDestinationParser(destination);
    String user = destinationParser.getUser();

    if (user == null) {
        if (logger.isErrorEnabled()) {
            logger.error("Ignoring message, expected destination pattern \"" + this.destinationPrefix
                    + "{userId}/**\": " + destination);
        }
        return;
    }

    for (String sessionId : this.userQueueSuffixResolver.getUserQueueSuffixes(user)) {

        String targetDestination = destinationParser.getTargetDestination(sessionId);
        headers.setDestination(targetDestination);
        message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();

        if (logger.isTraceEnabled()) {
            logger.trace("Sending message to resolved target destination " + targetDestination);
        }
        this.messagingTemplate.send(targetDestination, message);
    }
}