Example usage for org.springframework.messaging.simp.stomp StompDecoder HEARTBEAT_PAYLOAD

List of usage examples for org.springframework.messaging.simp.stomp StompDecoder HEARTBEAT_PAYLOAD

Introduction

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

Prototype

null HEARTBEAT_PAYLOAD

To view the source code for org.springframework.messaging.simp.stomp StompDecoder HEARTBEAT_PAYLOAD.

Click Source Link

Usage

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

/**
 * Encodes the given payload and headers into a {@code byte[]}.
 * @param headers the headers//from w ww .j a v a 2  s .co m
 * @param payload the payload
 * @return the encoded message
 */
public byte[] encode(Map<String, Object> headers, byte[] payload) {
    Assert.notNull(headers, "'headers' is required");
    Assert.notNull(payload, "'payload' is required");

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(128 + payload.length);
        DataOutputStream output = new DataOutputStream(baos);

        if (SimpMessageType.HEARTBEAT.equals(SimpMessageHeaderAccessor.getMessageType(headers))) {
            logger.trace("Encoding heartbeat");
            output.write(StompDecoder.HEARTBEAT_PAYLOAD);
        }

        else {
            StompCommand command = StompHeaderAccessor.getCommand(headers);
            if (command == null) {
                throw new IllegalStateException("Missing STOMP command: " + headers);
            }

            output.write(command.toString().getBytes(StandardCharsets.UTF_8));
            output.write(LF);
            writeHeaders(command, headers, payload, output);
            output.write(LF);
            writeBody(payload, output);
            output.write((byte) 0);
        }

        return baos.toByteArray();
    } catch (IOException ex) {
        throw new StompConversionException("Failed to encode STOMP frame, headers=" + headers, ex);
    }
}