Example usage for org.springframework.amqp.core Message Message

List of usage examples for org.springframework.amqp.core Message Message

Introduction

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

Prototype

public Message(byte[] body, MessageProperties messageProperties) 

Source Link

Usage

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

@Test
public void testDefaultType() {
    byte[] bytes = "{\"name\" : \"foo\" }".getBytes();
    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("application/json");
    Message message = new Message(bytes, messageProperties);
    JsonMessageConverter converter = new JsonMessageConverter();
    DefaultClassMapper classMapper = new DefaultClassMapper();
    classMapper.setDefaultType(Foo.class);
    converter.setClassMapper(classMapper);
    Object foo = converter.fromMessage(message);
    assertTrue(foo instanceof Foo);
}

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

/**
 * Send request message//from   w  w w  . ja  va2  s.  co  m
 * 
 * @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: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:org.springframework.amqp.support.converter.Jackson2JsonMessageConverterTests.java

@Test
public void testDefaultTypeConfig() {
    byte[] bytes = "{\"name\" : \"foo\" }".getBytes();
    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("application/json");
    Message message = new Message(bytes, messageProperties);
    Object foo = jsonConverterWithDefaultType.fromMessage(message);
    assertTrue(foo instanceof Foo);
}

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

@Test
public void testNoJsonContentType() {
    byte[] bytes = "{\"name\" : \"foo\" }".getBytes();
    MessageProperties messageProperties = new MessageProperties();
    Message message = new Message(bytes, messageProperties);
    Object foo = jsonConverterWithDefaultType.fromMessage(message);
    assertEquals(new String(bytes), new String((byte[]) foo));
}

From source file:com.jbrisbin.vpc.jobsched.mapred.MapReduceMessageHandler.java

private void onKeyChange(final String id, final String key) {
    final String token = id + key;
    Closure cl = contextCache.remove(token);
    Closure onKeyChange = (Closure) cl.getProperty("onKeyChange");
    onKeyChange.setProperty("key", key);
    final Object o = onKeyChange.call();
    if (null != o) {
        // Send to rereduce
        rabbitTemplate.send("", DigestUtils.md5Hex(id), new MessageCreator() {
            public Message createMessage() {
                MessageProperties props = new RabbitMessageProperties();
                props.setContentType("application/json");
                props.setCorrelationId(id.getBytes());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                try {
                    mapper.writeValue(out, o);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }/*from ww w .ja  va  2  s .co m*/
                Message msg = new Message(out.toByteArray(), props);
                return msg;
            }
        });
    }
}

From source file:org.kurento.rabbitmq.RabbitTemplate.java

protected Message doSendAndReceiveWithTemporary(final String exchange, final String routingKey,
        final Message message) {
    return this.execute(new ChannelCallback<Message>() {

        @Override//from   w  w w.j  a  v  a2s .c  om
        public Message doInRabbit(Channel channel) throws Exception {
            final ArrayBlockingQueue<Message> replyHandoff = new ArrayBlockingQueue<Message>(1);

            Assert.isNull(message.getMessageProperties().getReplyTo(),
                    "Send-and-receive methods can only be used if the Message does not already have a replyTo property.");
            DeclareOk queueDeclaration = channel.queueDeclare();
            String replyTo = queueDeclaration.getQueue();
            message.getMessageProperties().setReplyTo(replyTo);

            String consumerTag = UUID.randomUUID().toString();
            DefaultConsumer consumer = new DefaultConsumer(channel) {

                @Override
                public void handleDelivery(String consumerTag, Envelope envelope,
                        AMQP.BasicProperties properties, byte[] body) throws IOException {
                    MessageProperties messageProperties = messagePropertiesConverter
                            .toMessageProperties(properties, envelope, encoding);
                    Message reply = new Message(body, messageProperties);
                    if (logger.isTraceEnabled()) {
                        logger.trace("Message received " + reply);
                    }
                    try {
                        replyHandoff.put(reply);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            };
            channel.basicConsume(replyTo, true, consumerTag, true, true, null, consumer);
            doSend(channel, exchange, routingKey, message, null);
            Message reply = (replyTimeout < 0) ? replyHandoff.take()
                    : replyHandoff.poll(replyTimeout, TimeUnit.MILLISECONDS);
            channel.basicCancel(consumerTag);
            return reply;
        }
    });
}

From source file:org.kurento.rabbitmq.RabbitTemplate.java

private Message buildMessageFromResponse(GetResponse response) {
    MessageProperties messageProps = this.messagePropertiesConverter.toMessageProperties(response.getProps(),
            response.getEnvelope(), this.encoding);
    messageProps.setMessageCount(response.getMessageCount());
    return new Message(response.getBody(), messageProps);
}

From source file:org.kurento.rabbitmq.RabbitTemplate.java

@Override
public void handleReturn(int replyCode, String replyText, String exchange, String routingKey,
        BasicProperties properties, byte[] body) throws IOException {
    if (this.returnCallback == null) {
        if (logger.isWarnEnabled()) {
            logger.warn("Returned message but no callback available");
        }/*from ww  w  .  jav  a  2  s .co  m*/
    } else {
        properties.getHeaders().remove(PublisherCallbackChannel.RETURN_CORRELATION);
        MessageProperties messageProperties = messagePropertiesConverter.toMessageProperties(properties, null,
                this.encoding);
        Message returnedMessage = new Message(body, messageProperties);
        this.returnCallback.returnedMessage(returnedMessage, replyCode, replyText, exchange, routingKey);
    }
}