Example usage for org.springframework.amqp.core MessageProperties CONTENT_TYPE_JSON

List of usage examples for org.springframework.amqp.core MessageProperties CONTENT_TYPE_JSON

Introduction

In this page you can find the example usage for org.springframework.amqp.core MessageProperties CONTENT_TYPE_JSON.

Prototype

String CONTENT_TYPE_JSON

To view the source code for org.springframework.amqp.core MessageProperties CONTENT_TYPE_JSON.

Click Source Link

Usage

From source file:com.dc.gameserver.extComponents.SpringRabbitmq.FastJsonMessageConverter.java

protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)
        throws MessageConversionException {
    byte[] bytes = null;
    try {//  w  ww.ja v a2  s. c om
        String jsonString = JSON.toJSONString(objectToConvert);
        bytes = jsonString.getBytes(this.defaultCharset);
    } catch (UnsupportedEncodingException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    }
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setContentEncoding(this.defaultCharset);
    messageProperties.setContentLength(bytes.length);
    return new Message(bytes, messageProperties);

}

From source file:com.bfair.pricing.gateway.RabbitPriceDataGateway.java

private MessageProperties getMessageProperties() {
    if (properties == null) {
        properties = new MessageProperties();
        properties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    }/*from   w  ww  . java 2  s  .c o m*/
    return properties;
}

From source file:com.github.liyp.rabbitmq.demo.FastJsonMessageConverter.java

protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)
        throws MessageConversionException {
    byte[] bytes = null;
    try {/*from   w w w .  j  a  va  2  s  . c om*/
        String jsonString = JSON.toJSONString(objectToConvert);
        bytes = jsonString.getBytes(this.defaultCharset);
    } catch (UnsupportedEncodingException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    }
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setContentEncoding(this.defaultCharset);
    if (bytes != null) {
        messageProperties.setContentLength(bytes.length);
    }
    return new Message(bytes, messageProperties);
}

From source file:amqp.spring.converter.XStreamConverter.java

@Override
protected Message createMessage(Object object, MessageProperties messageProperties) {
    try {//  w w  w . j  av a  2s  .com
        byte[] body = null;
        if (object != null) {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            StaxWriter writer = new StaxWriter(new QNameMap(),
                    this.outputFactory.createXMLStreamWriter(outStream));
            this.objectMapper.marshal(object, writer);
            body = outStream.toByteArray();
        }

        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        messageProperties.setContentEncoding(this.encoding);
        messageProperties.setContentLength(body != null ? body.length : 0);
        classMapper.fromClass(object.getClass(), messageProperties);
        return new Message(body, messageProperties);
    } catch (XMLStreamException ex) {
        String typeId = (String) messageProperties.getHeaders()
                .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME);
        LOG.error("XMLStreamException trying to marshal message of type {}", typeId, ex);
        throw new MessageConversionException("Could not marshal message of type " + typeId, ex);
    } catch (XStreamException ex) {
        //For some reason messages appear to be getting eaten at this stage... very nasty when you try to troubleshoot.
        String typeId = (String) messageProperties.getHeaders()
                .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME);
        LOG.error("XStreamException trying to marshal message of type {}", typeId, ex);
        throw new MessageConversionException("Could not marshal message of type " + typeId, ex);
    }
}

From source file:amqp.spring.converter.XStreamConverter.java

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    MessageProperties messageProperties = message.getMessageProperties();
    if (messageProperties == null)
        throw new MessageConversionException("Cannot decode a message with no properties!");

    byte[] body = message.getBody();
    if (body == null)
        return null;

    String messageEncoding = messageProperties.getContentEncoding();
    if (messageEncoding == null)
        messageEncoding = getEncoding();

    String contentType = messageProperties.getContentType();
    if (!MessageProperties.CONTENT_TYPE_JSON.equalsIgnoreCase(contentType))
        throw new MessageConversionException("Cannot understand a message of type " + contentType);

    try {// w  w  w  . ja v  a2s  . c o  m
        ByteArrayInputStream inStream = new ByteArrayInputStream(body);
        StaxReader reader = new StaxReader(new QNameMap(),
                this.inputFactory.createXMLStreamReader(inStream, messageEncoding));
        return this.objectMapper.unmarshal(reader);
    } catch (XMLStreamException ex) {
        String typeId = (String) messageProperties.getHeaders()
                .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME);
        LOG.error("XMLStreamException trying to unmarshal message of type {}", typeId, ex);
        throw new MessageConversionException("Could not unmarshal message of type " + typeId, ex);
    } catch (XStreamException ex) {
        //For some reason messages appear to be getting eaten at this stage... very nasty when you try to troubleshoot.
        String typeId = (String) messageProperties.getHeaders()
                .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME);
        LOG.error("XStreamException trying to unmarshal message of type {}", typeId, ex);
        throw new MessageConversionException("Could not unmarshal message of type " + typeId, ex);
    }
}

From source file:org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.java

@Override
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)
        throws MessageConversionException {
    byte[] bytes;
    try {/*from  w  w w. ja  v a 2s.  c o m*/
        String jsonString = this.jsonObjectMapper.writeValueAsString(objectToConvert);
        bytes = jsonString.getBytes(getDefaultCharset());
    } catch (IOException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    }
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setContentEncoding(getDefaultCharset());
    messageProperties.setContentLength(bytes.length);

    if (getClassMapper() == null) {
        getJavaTypeMapper().fromJavaType(this.jsonObjectMapper.constructType(objectToConvert.getClass()),
                messageProperties);

    } else {
        getClassMapper().fromClass(objectToConvert.getClass(), messageProperties);

    }

    return new Message(bytes, messageProperties);
}

From source file:org.springframework.amqp.support.converter.JsonMessageConverter.java

@Override
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)
        throws MessageConversionException {
    byte[] bytes = null;
    try {//from ww w .  j av a  2 s .c  o m
        String jsonString = jsonObjectMapper.writeValueAsString(objectToConvert);
        bytes = jsonString.getBytes(this.defaultCharset);
    } catch (UnsupportedEncodingException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    } catch (JsonGenerationException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    } catch (JsonMappingException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    } catch (IOException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    }
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setContentEncoding(this.defaultCharset);
    if (bytes != null) {
        messageProperties.setContentLength(bytes.length);
    }

    if (getClassMapper() == null) {
        getJavaTypeMapper().fromJavaType(type(objectToConvert.getClass()), messageProperties);

    } else {
        getClassMapper().fromClass(objectToConvert.getClass(), messageProperties);

    }

    return new Message(bytes, messageProperties);

}