List of usage examples for org.springframework.messaging.simp.stomp StompHeaderAccessor setDestination
@Override public void setDestination(@Nullable String destination)
From source file:org.springframework.samples.portfolio.web.tomcat.TestStompClient.java
public void subscribe(String destination, MessageHandler messageHandler) { String id = String.valueOf(this.subscriptionIndex.getAndIncrement()); this.subscriptionHandlers.put(id, messageHandler); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE); headers.setSubscriptionId(id);/* ww w. ja v a2 s .c o m*/ headers.setDestination(destination); Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); byte[] bytes = encoder.encode(message); try { this.session.sendMessage(new TextMessage(new String(bytes, DEFAULT_CHARSET))); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:org.springframework.samples.portfolio.web.tomcat.TestStompClient.java
public void send(String destination, Object payload) { StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND); headers.setDestination(destination); Message<byte[]> message = (Message<byte[]>) this.messageConverter.toMessage(payload, new MessageHeaders(headers.toMap())); byte[] bytes = this.encoder.encode(message); try {/*from w w w . ja v a 2 s .c o m*/ this.session.sendMessage(new TextMessage(new String(bytes, DEFAULT_CHARSET))); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:org.springframework.web.socket.messaging.StompSubProtocolHandler.java
/** * Handle STOMP messages going back out to WebSocket clients. *//* ww w . j a va 2s .c om*/ @Override @SuppressWarnings("unchecked") public void handleMessageToClient(WebSocketSession session, Message<?> message) { if (!(message.getPayload() instanceof byte[])) { if (logger.isErrorEnabled()) { logger.error("Expected byte[] payload. Ignoring " + message + "."); } return; } StompHeaderAccessor accessor = getStompHeaderAccessor(message); StompCommand command = accessor.getCommand(); if (StompCommand.MESSAGE.equals(command)) { if (accessor.getSubscriptionId() == null && logger.isWarnEnabled()) { logger.warn("No STOMP \"subscription\" header in " + message); } String origDestination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION); if (origDestination != null) { accessor = toMutableAccessor(accessor, message); accessor.removeNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION); accessor.setDestination(origDestination); } } else if (StompCommand.CONNECTED.equals(command)) { this.stats.incrementConnectedCount(); accessor = afterStompSessionConnected(message, accessor, session); if (this.eventPublisher != null && StompCommand.CONNECTED.equals(command)) { try { SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes()); SimpAttributesContextHolder.setAttributes(simpAttributes); Principal user = getUser(session); publishEvent(this.eventPublisher, new SessionConnectedEvent(this, (Message<byte[]>) message, user)); } finally { SimpAttributesContextHolder.resetAttributes(); } } } byte[] payload = (byte[]) message.getPayload(); if (StompCommand.ERROR.equals(command) && getErrorHandler() != null) { Message<byte[]> errorMessage = getErrorHandler().handleErrorMessageToClient((Message<byte[]>) message); if (errorMessage != null) { accessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class); Assert.state(accessor != null, "No StompHeaderAccessor"); payload = errorMessage.getPayload(); } } sendToClient(session, accessor, payload); }