Example usage for org.springframework.messaging.simp.stomp StompCommand SEND

List of usage examples for org.springframework.messaging.simp.stomp StompCommand SEND

Introduction

In this page you can find the example usage for org.springframework.messaging.simp.stomp StompCommand SEND.

Prototype

StompCommand SEND

To view the source code for org.springframework.messaging.simp.stomp StompCommand SEND.

Click Source Link

Usage

From source file:com.codeveo.lago.bot.stomp.client.WebSocketStompSession.java

public void send(String destination, Object payload) {
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
    headers.setDestination(destination);
    sendInternal(/*from w w w  . j  a va  2s .c o m*/
            (Message<byte[]>) this.messageConverter.toMessage(payload, new MessageHeaders(headers.toMap())));
}

From source file:org.tmarciniak.mtp.web.websocket.support.client.WebSocketStompSession.java

@SuppressWarnings("unchecked")
public void send(String destination, Object payload) {
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
    headers.setDestination(destination);
    Message<?> message = this.messageConverter.toMessage(payload, new MessageHeaders(headers.toMap()));
    sendInternal((Message<byte[]>) message);
}

From source file:org.springframework.samples.portfolio.web.standalone.StandalonePortfolioControllerTests.java

@Test
public void executeTrade() throws Exception {

    Trade trade = new Trade();
    trade.setAction(Trade.TradeAction.Buy);
    trade.setTicker("DELL");
    trade.setShares(25);/*from  ww  w .  ja v a2  s .co  m*/

    byte[] payload = new ObjectMapper().writeValueAsBytes(trade);

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
    headers.setDestination("/app/trade");
    headers.setSessionId("0");
    headers.setUser(new TestPrincipal("fabrice"));
    headers.setSessionAttributes(new HashMap<String, Object>());
    Message<byte[]> message = MessageBuilder.withPayload(payload).setHeaders(headers).build();

    this.annotationMethodMessageHandler.handleMessage(message);

    assertEquals(1, this.tradeService.getTrades().size());
    Trade actual = this.tradeService.getTrades().get(0);

    assertEquals(Trade.TradeAction.Buy, actual.getAction());
    assertEquals("DELL", actual.getTicker());
    assertEquals(25, actual.getShares());
    assertEquals("fabrice", actual.getUsername());
}

From source file:smpp.networking.SimpleStompClient.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 {//  w  w  w  .  jav  a 2 s  . c  om
        this.session.getRemote().sendString(new String(bytes, DEFAULT_CHARSET));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

}

From source file:org.springframework.samples.portfolio.web.context.ContextPortfolioControllerTests.java

@Test
public void executeTrade() throws Exception {

    Trade trade = new Trade();
    trade.setAction(Trade.TradeAction.Buy);
    trade.setTicker("DELL");
    trade.setShares(25);/*from   www.ja  v a  2 s  . c  o m*/

    byte[] payload = new ObjectMapper().writeValueAsBytes(trade);

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
    headers.setDestination("/app/trade");
    headers.setSessionId("0");
    headers.setUser(new TestPrincipal("fabrice"));
    headers.setSessionAttributes(new HashMap<String, Object>());
    Message<byte[]> message = MessageBuilder.createMessage(payload, headers.getMessageHeaders());

    this.brokerChannelInterceptor.setIncludedDestinations("/user/**");
    this.brokerChannelInterceptor.startRecording();

    this.clientInboundChannel.send(message);

    Message<?> positionUpdate = this.brokerChannelInterceptor.awaitMessage(5);
    assertNotNull(positionUpdate);

    StompHeaderAccessor positionUpdateHeaders = StompHeaderAccessor.wrap(positionUpdate);
    assertEquals("/user/fabrice/queue/position-updates", positionUpdateHeaders.getDestination());

    String json = new String((byte[]) positionUpdate.getPayload(), Charset.forName("UTF-8"));
    new JsonPathExpectationsHelper("$.ticker").assertValue(json, "DELL");
    new JsonPathExpectationsHelper("$.shares").assertValue(json, 75);
}

From source file:org.springframework.messaging.simp.stomp.DefaultStompSession.java

@Override
public Receiptable send(StompHeaders stompHeaders, Object payload) {
    Assert.hasText(stompHeaders.getDestination(), "Destination header is required");

    String receiptId = checkOrAddReceipt(stompHeaders);
    Receiptable receiptable = new ReceiptHandler(receiptId);

    StompHeaderAccessor accessor = createHeaderAccessor(StompCommand.SEND);
    accessor.addNativeHeaders(stompHeaders);
    Message<byte[]> message = createMessage(accessor, payload);
    execute(message);//from ww w . j  a v a  2 s . c  o m

    return receiptable;
}

From source file:org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler.java

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

    StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
    String sessionId = headers.getSessionId();
    String destination = headers.getDestination();
    StompCommand command = headers.getCommand();
    SimpMessageType messageType = headers.getMessageType();

    if (!this.running) {
        if (logger.isTraceEnabled()) {
            logger.trace("STOMP broker relay not running. Ignoring message id=" + headers.getId());
        }/*from  ww w . j  a va 2  s  .  co m*/
        return;
    }

    if (SimpMessageType.MESSAGE.equals(messageType)) {
        sessionId = (sessionId == null) ? STOMP_RELAY_SYSTEM_SESSION_ID : sessionId;
        headers.setSessionId(sessionId);
        command = (command == null) ? StompCommand.SEND : command;
        headers.setCommandIfNotSet(command);
        message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();
    }

    if (headers.getCommand() == null) {
        logger.error("Ignoring message, no STOMP command: " + message);
        return;
    }
    if (sessionId == null) {
        logger.error("Ignoring message, no sessionId: " + message);
        return;
    }

    try {
        if (checkDestinationPrefix(command, destination)) {

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

            if (SimpMessageType.CONNECT.equals(messageType)) {
                headers.setHeartbeat(0, 0); // TODO: disable for now
                message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();
                RelaySession session = new RelaySession(sessionId);
                this.relaySessions.put(sessionId, session);
                session.open(message);
            } else if (SimpMessageType.DISCONNECT.equals(messageType)) {
                RelaySession session = this.relaySessions.remove(sessionId);
                if (session == null) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Session already removed, sessionId=" + sessionId);
                    }
                    return;
                }
                session.forward(message);
            } else {
                RelaySession session = this.relaySessions.get(sessionId);
                if (session == null) {
                    logger.warn("Session id=" + sessionId + " not found. Ignoring message: " + message);
                    return;
                }
                session.forward(message);
            }
        }
    } catch (Throwable t) {
        logger.error("Failed to handle message " + message, t);
    }
}

From source file:org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandlerIntegrationTests.java

@Test(expected = MessageDeliveryException.class)
public void messageDeliveryExceptionIfSystemSessionForwardFails() throws Exception {

    logger.debug("Starting test messageDeliveryExceptionIfSystemSessionForwardFails()");

    stopActiveMqBrokerAndAwait();//from  w w  w  .  jav a 2 s.co m
    this.eventPublisher.expectBrokerAvailabilityEvent(false);

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
    this.relay.handleMessage(MessageBuilder.createMessage("test".getBytes(), headers.getMessageHeaders()));
}

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 {// www.  jav a2s.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

private StompHeaderAccessor getStompHeaderAccessor(Message<?> message) {
    MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
    if (accessor instanceof StompHeaderAccessor) {
        return (StompHeaderAccessor) accessor;
    } else {//from www . j a  v a2 s .  c  o  m
        StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(message);
        SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(message.getHeaders());
        if (SimpMessageType.CONNECT_ACK.equals(messageType)) {
            stompAccessor = convertConnectAcktoStompConnected(stompAccessor);
        } else if (SimpMessageType.DISCONNECT_ACK.equals(messageType)) {
            String receipt = getDisconnectReceipt(stompAccessor);
            if (receipt != null) {
                stompAccessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
                stompAccessor.setReceiptId(receipt);
            } else {
                stompAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
                stompAccessor.setMessage("Session closed.");
            }
        } else if (SimpMessageType.HEARTBEAT.equals(messageType)) {
            stompAccessor = StompHeaderAccessor.createForHeartbeat();
        } else if (stompAccessor.getCommand() == null || StompCommand.SEND.equals(stompAccessor.getCommand())) {
            stompAccessor.updateStompCommandAsServerMessage();
        }
        return stompAccessor;
    }
}