Example usage for javax.jms BytesMessage getJMSType

List of usage examples for javax.jms BytesMessage getJMSType

Introduction

In this page you can find the example usage for javax.jms BytesMessage getJMSType.

Prototype


String getJMSType() throws JMSException;

Source Link

Document

Gets the message type identifier supplied by the client when the message was sent.

Usage

From source file:org.bremersee.common.jms.DefaultJmsConverter.java

private Object tryToGetPayload(BytesMessage msg, Class<?> payloadClass) throws JMSException {
    int len;/*www .  ja  va 2s  . c  om*/
    byte[] buf = new byte[1024];
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    while ((len = msg.readBytes(buf)) != -1) {
        out.write(buf, 0, len);
    }
    byte[] payloadBytes = out.toByteArray();
    if (payloadBytes == null || payloadBytes.length == 0) {
        return null;
    }

    Object payload = null;
    if (shouldTryJson(payloadClass, payloadBytes)) {
        try {
            payload = objectMapper.readValue(payloadBytes, payloadClass);

        } catch (Throwable t0) { // NOSONAR
            log.info("Reading JSON from message with JMSType [" + msg.getJMSType() + "] failed.");
        }
    }

    if (shouldTryUnmarshal(payload, payloadClass)) {
        try {
            payload = marshaller.unmarshal(new StreamSource(new ByteArrayInputStream(payloadBytes)));

        } catch (Throwable t0) { // NOSONAR
            log.info("Reading XML from message with JMSType [" + msg.getJMSType() + "] failed.");
        }
    }

    if (shouldTryDeserialize(payload, payloadClass)) {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(payloadBytes));
            payload = objectInputStream.readObject();

        } catch (Throwable t0) { // NOSONAR
            log.info("Reading serialized object of JMSType [" + msg.getJMSType() + "] failed.");
        }
    }

    if (payload == null) {
        MessageConversionException e = new MessageConversionException("Converting BytesMessage failed.");
        log.error("Could not convert bytes:\n" + Base64.encodeBase64String(payloadBytes) + "\n", e);
        throw e;
    }

    return payload;
}