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

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

Introduction

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

Prototype

public void setContentEncoding(String contentEncoding) 

Source Link

Usage

From source file:io.meles.amqp.spring.SnappyMessageConverterTest.java

@Test
public void shouldDecompressCompressedMessage() {
    final byte[] compressedBody = { 1, 3, 5, 3, 3, };
    final MessageProperties properties = new MessageProperties();
    properties.setContentEncoding("snappy");

    when(compressor.decompress(compressedBody)).thenReturn(new byte[] { 6, 4, 5, 6, 4, 32, 45, 62, 1 });
    final Object resultBody = new Object();
    when(converter.fromMessage(any(Message.class))).thenReturn(resultBody);

    final Object convertedBody = snappyMessageConverterUnderTest
            .fromMessage(new Message(compressedBody, properties));

    assertThat(convertedBody, is(sameInstance(resultBody)));
}

From source file:io.meles.amqp.spring.SnappyMessageConverterTest.java

@Test
public void shouldOnlyDecompressSnappyEncoding() {
    final byte[] compressedBody = { 1, 3, 5, 3, 3, };
    final MessageProperties properties = new MessageProperties();
    properties.setContentEncoding("this_is_not_snappy");

    final Object resultBody = new Object();
    when(converter.fromMessage(any(Message.class))).thenReturn(resultBody);

    final Object convertedBody = snappyMessageConverterUnderTest
            .fromMessage(new Message(compressedBody, properties));

    verifyNoMoreInteractions(compressor);

    assertThat(convertedBody, is(sameInstance(resultBody)));
}

From source file:io.meles.amqp.spring.SnappyMessageConverter.java

@Override
protected Message createMessage(final Object object, final MessageProperties messageProperties) {
    final Message messageToCompress = underlyingConverter.toMessage(object, messageProperties);
    final byte[] uncompressedBody = messageToCompress.getBody();

    final byte[] compressedBody = compressor.compress(uncompressedBody);

    if (compressedBody.length >= uncompressedBody.length) { // there's no benefit to compression
        return messageToCompress;
    }//from  www .j  a v a 2 s  . com

    final MessageProperties uncompressedProperties = messageToCompress.getMessageProperties();
    // TODO 1) should we copy the properties
    // TODO 2) what if there's already an encoding set
    uncompressedProperties.setContentEncoding("snappy");

    return new Message(compressedBody, uncompressedProperties);
}

From source file:org.resthub.rpc.AMQPHessianProxy.java

/**
 * Send request message//ww  w . j  a v  a  2s .  c  om
 * 
 * @param connectionFactory Spring connection factory
 * @param method Method to call
 * @param args Method arguments
 * @return Response to the sent request
 * @throws IOException
 */
private Message sendRequest(ConnectionFactory connectionFactory, Method method, Object[] args)
        throws IOException {
    RabbitTemplate template = this._factory.getTemplate();

    byte[] payload = createRequestBody(method, args);

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("x-application/hessian");
    if (_factory.isCompressed()) {
        messageProperties.setContentEncoding("deflate");
    }

    Message message = new Message(payload, messageProperties);
    Message response = template.sendAndReceive(_factory.getRequestExchangeName(_factory.getServiceInterface()),
            _factory.getRequestQueueName(_factory.getServiceInterface()), message);

    return response;
}

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

@Override
protected Message createMessage(Object object, MessageProperties messageProperties) {
    try {/*from  w  w w.j  av a2  s .co m*/
        byte[] body = null;
        if (object != null) {
            body = object.toString().getBytes(this.encoding);
        }

        String msgContentType = this.contentType == null ? DEFAULT_CONTENT_TYPE : this.contentType;
        messageProperties.setContentType(msgContentType);
        messageProperties.setContentEncoding(this.encoding);
        messageProperties.setContentLength(body != null ? body.length : 0);
        return new Message(body, messageProperties);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Cannot encode strings as {}", this.encoding, ex);
        throw new MessageConversionException("Cannot encode strings as " + this.encoding, ex);
    }
}

From source file:io.meles.amqp.spring.SnappyMessageConverter.java

@Override
public Object fromMessage(final Message message) throws MessageConversionException {
    final MessageProperties compressedProperties = message.getMessageProperties();
    if (!"snappy".equals(compressedProperties.getContentEncoding())) {
        return underlyingConverter.fromMessage(message);
    }//from www  .  ja v a 2 s  .  c om

    final byte[] compressedBody = message.getBody();
    final byte[] decompressedBody = compressor.decompress(compressedBody);

    // TODO should we copy the properties
    compressedProperties.setContentEncoding(null);
    return underlyingConverter.fromMessage(new Message(decompressedBody, compressedProperties));
}

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

protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)
        throws MessageConversionException {
    byte[] bytes = null;
    try {//from   www.ja va 2s.co  m
        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.github.liyp.rabbitmq.demo.FastJsonMessageConverter.java

protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)
        throws MessageConversionException {
    byte[] bytes = null;
    try {//  w  w  w . j a  v  a 2s  . c o m
        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 ww. j  a  v  a2  s. c  o  m
        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:weChat.amqp.RpcTest.java

/**
 * Sends a message to a service that upcases the String and returns as a
 * reply using a {@link RabbitTemplate} configured with a fixed reply queue
 * and reply listener, configured with JavaConfig.
 *///from  ww w  .  ja  v  a2  s  . com
@Test
public void test() {
    AmqpReqParam param = new AmqpReqParam();
    param.setCmdid("WJ007");
    param.setCompanycode("01103");
    param.setWechatpubinfoid(1);
    BaseDto dto = new BaseDto();
    dto.put("cardnum", "5000011");
    param.setParams(dto);
    //String corrId = UUID.randomUUID().toString();
    String corrId = FixedReplyQueueConfig.responseQueue;
    Object resp = rabbitTemplate.convertSendAndReceive(FixedReplyQueueConfig.routing, param, (message) -> {
        MessageProperties properities = message.getMessageProperties();

        System.out.println("corrId:" + corrId);
        properities.setCorrelationId(corrId.getBytes());
        properities.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        properities.setTimestamp(new Date());
        long currentTimeMillis = System.currentTimeMillis();
        properities.setMessageId(String.valueOf(currentTimeMillis));
        properities.setExpiration(String.valueOf(50000));
        properities.setContentEncoding(corrId);

        return message;
    });
    System.out.println("?" + resp);
}