Example usage for org.springframework.messaging.simp SimpMessageHeaderAccessor setDestination

List of usage examples for org.springframework.messaging.simp SimpMessageHeaderAccessor setDestination

Introduction

In this page you can find the example usage for org.springframework.messaging.simp SimpMessageHeaderAccessor setDestination.

Prototype

public void setDestination(@Nullable String destination) 

Source Link

Usage

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;// ww  w .j  a  v  a 2  s.c om
    }

    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);
    }
}