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

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

Introduction

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

Prototype

MessageProperties

Source Link

Usage

From source file:com.xoom.rabbit.test.Main.java

public static void main(String[] args) throws InterruptedException {
    if (args.length != 9) {
        System.out.println(//from   ww  w .  j  av  a 2s  .  c  om
                "usage: java -jar target/rabbit-tester-0.1-SNAPSHOT-standalone.jar [consumer_threads] [number_of_messages] [amqp_host] [amqp_port] [produce] [consume] [message size in bytes] [username] [password]");
        return;
    }
    final long startTime = System.currentTimeMillis();
    int consumerThreads = Integer.parseInt(args[0]);
    final int messages = Integer.parseInt(args[1]);
    String host = args[2];
    int port = Integer.parseInt(args[3]);
    boolean produce = Boolean.parseBoolean(args[4]);
    boolean consume = Boolean.parseBoolean(args[5]);
    final int messageSize = Integer.parseInt(args[6]);
    String username = args[7];
    String password = args[8];

    if (produce) {
        System.out.println("Sending " + messages + " messages to " + host + ":" + port);
    }
    if (consume) {
        System.out.println("Consuming " + messages + " messages from " + host + ":" + port);
    }
    if (!produce && !consume) {
        System.out.println("Not producing or consuming any messages.");
    }

    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
    connectionFactory.setUsername(username);
    connectionFactory.setPassword(password);
    connectionFactory.setChannelCacheSize(consumerThreads + 1);

    RabbitAdmin amqpAdmin = new RabbitAdmin(connectionFactory);

    DirectExchange exchange = new DirectExchange(EXCHANGE_NAME, true, false);
    Queue queue = new Queue(QUEUE_NAME);
    amqpAdmin.declareExchange(exchange);
    amqpAdmin.declareQueue(queue);
    amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY));

    final AmqpTemplate amqpTemplate = new RabbitTemplate(connectionFactory);

    final CountDownLatch producerLatch = new CountDownLatch(messages);
    final CountDownLatch consumerLatch = new CountDownLatch(messages);

    SimpleMessageListenerContainer listenerContainer = null;

    if (consume) {
        listenerContainer = new SimpleMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        listenerContainer.setQueueNames(QUEUE_NAME);
        listenerContainer.setConcurrentConsumers(consumerThreads);
        listenerContainer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if (consumerLatch.getCount() == 1) {
                    System.out.println("Finished consuming " + messages + " messages in "
                            + (System.currentTimeMillis() - startTime) + "ms");
                }
                consumerLatch.countDown();
            }
        });
        listenerContainer.start();
    }

    if (produce) {
        while (producerLatch.getCount() > 0) {
            try {
                byte[] message = new byte[messageSize];
                RND.nextBytes(message);
                amqpTemplate.send(EXCHANGE_NAME, ROUTING_KEY, new Message(message, new MessageProperties()));
                producerLatch.countDown();
            } catch (Exception e) {
                System.out.println("Failed to send message " + (messages - producerLatch.getCount())
                        + " will retry forever.");
            }
        }
    }

    if (consume) {
        consumerLatch.await();
        listenerContainer.shutdown();
    }

    connectionFactory.destroy();
}

From source file:amqp.spring.camel.component.SpringAMQPHeaderTest.java

@Test
public void fromBasicProperties() throws Exception {
    MessageProperties properties = new MessageProperties();
    properties.setHeader("NotSecret", "Popcorn");
    org.springframework.amqp.core.Message message = new Message(new byte[] {}, properties);
    message.getMessageProperties().setPriority(1);
    message.getMessageProperties().setReplyTo("BuzzSaw");

    SpringAMQPMessage camelMessage = SpringAMQPHeader.setBasicPropertiesToHeaders(new SpringAMQPMessage(),
            message);//from  www. java  2s . c om
    Assert.assertNull(camelMessage.getHeader("NotSecret"));
    Assert.assertEquals(1, camelMessage.getHeader(SpringAMQPHeader.PRIORITY));
    Assert.assertEquals("BuzzSaw", camelMessage.getHeader(SpringAMQPHeader.REPLY_TO));
}

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

@Test
public void testConversion() throws Exception {
    TestObject testObject = new TestObject();
    testObject.setValue("TESTING");

    MessageProperties messageProperties = new MessageProperties();

    MessageConverter converter = new StringConverter();
    Message amqpMessage = converter.toMessage(testObject, messageProperties);
    Object newObject = converter.fromMessage(amqpMessage);

    Assert.assertEquals("TESTING", newObject);
}

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

@Test
public void testConversion() throws Exception {
    TestObject testObject = new TestObject();
    testObject.setValue("TESTING");

    MessageProperties messageProperties = new MessageProperties();

    MessageConverter converter = new XStreamConverter();
    ((XStreamConverter) converter).setEncoding("UTF-8");
    Message amqpMessage = converter.toMessage(testObject, messageProperties);
    Assert.assertEquals("{\"amqp.spring.converter.XStreamConverterTest_-TestObject\":{\"value\":\"TESTING\"}}",
            new String(amqpMessage.getBody()));

    Object newObject = converter.fromMessage(amqpMessage);
    Assert.assertEquals(testObject, newObject);
    Assert.assertEquals("UTF-8", ((XStreamConverter) converter).getEncoding());
}

From source file:com.anton.dev.tqrbs2.test.TestSender.java

public void sendMessage() {
    String msg = "mensaje de test en cola";
    final Message message = new Message(msg.getBytes(), new MessageProperties());
    LOGGER.info("Enviando: " + msg);
    // template.send("myExchange", "userMesssage", message);        
    template.convertAndSend("myExchange", "userMesssage", msg);
}

From source file:amqp.spring.camel.component.SpringAMQPMessageTest.java

@Test
public void testExchangePattern() throws Exception {
    org.apache.camel.Message camelMessage = new DefaultMessage();
    Exchange exchange = new DefaultExchange(new DefaultCamelContext(), ExchangePattern.InOut);
    exchange.setIn(camelMessage);//from   w ww. ja v a2s . c o  m

    MessageProperties properties = new MessageProperties();
    org.springframework.amqp.core.Message amqpMessage = new org.springframework.amqp.core.Message(
            "Testing".getBytes(), properties);

    amqpMessage = new SpringAMQPMessage.HeadersPostProcessor(camelMessage).postProcessMessage(amqpMessage);
    ExchangePattern exchangePattern = SpringAMQPMessage.getExchangePattern(amqpMessage);
    Assert.assertEquals(exchange.getPattern(), exchangePattern);
}

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

@Test
public void testStringConversion() throws Exception {
    TestObject testObject = new TestObject();
    testObject.setValue("TESTING");

    StringConverter stringConverter = new StringConverter();
    stringConverter.setContentType("application/xml");

    ContentTypeConverterFactory converter = new ContentTypeConverterFactory();
    converter.getConverters().put("application/json", new XStreamConverter());
    converter.getConverters().put("application/xml", new StringConverter());
    converter.setDefaultContentType("application/json");
    converter.setFallbackConverter(new StringConverter());

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("application/xml");

    Message amqpMessage = converter.toMessage(testObject, messageProperties);
    Assert.assertEquals("TESTING", new String(amqpMessage.getBody()));

    Object newObject = converter.fromMessage(amqpMessage);
    Assert.assertEquals("TESTING", newObject);
}

From source file:amqp.spring.camel.component.SpringAMQPMessageTest.java

@Test
public void fromAMQP() throws Exception {
    String body = "Test Message";
    MessageConverter msgConverter = new StringMessageConverter();
    MessageProperties properties = new MessageProperties();
    properties.setHeader("NotSecret", "Popcorn");
    org.springframework.amqp.core.Message message = new org.springframework.amqp.core.Message(body.getBytes(),
            properties);/*  ww w  . ja v a2  s .c o  m*/

    SpringAMQPMessage camelMessage = SpringAMQPMessage.fromAMQPMessage(msgConverter, message);
    Assert.assertEquals(body, camelMessage.getBody(String.class));
    Assert.assertEquals("Popcorn", camelMessage.getHeader("NotSecret"));
}

From source file:amqp.spring.camel.component.SpringAMQPHeaderTest.java

@Test
public void toBasicProperties() throws Exception {
    SpringAMQPMessage camelMessage = new SpringAMQPMessage();
    camelMessage.setHeader("Secret", "My Secret");
    camelMessage.setHeader(SpringAMQPHeader.PRIORITY, 1);
    camelMessage.setHeader(SpringAMQPHeader.REPLY_TO, "BuzzSaw");

    Exchange exchange = new DefaultExchange(new DefaultCamelContext());
    exchange.setIn(camelMessage);/*  ww  w  . jav a  2  s  .c om*/

    Message message = new Message(new byte[] {}, new MessageProperties());
    message = SpringAMQPHeader.setBasicPropertiesFromHeaders(message, camelMessage.getHeaders());
    Assert.assertNull(message.getMessageProperties().getHeaders().get("Secret"));
    Assert.assertEquals(Integer.valueOf(1), message.getMessageProperties().getPriority());
    Assert.assertEquals("BuzzSaw", message.getMessageProperties().getReplyTo());
}

From source file:de.davidbilge.spring.remoting.amqp.client.AmqpClientInterceptor.java

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setHeader(Constants.INVOKED_METHOD_HEADER_NAME,
            methodHeaderNamingStrategy.generateMethodName(invocation.getMethod()));

    Message m = getMessageConverter().toMessage(invocation.getArguments(), messageProperties);

    Message resultMessage;/*  w  w  w  .jav  a  2s . c  om*/
    if (getRoutingKey() == null) {
        // Use the template's default routing key
        resultMessage = amqpTemplate.sendAndReceive(m);
    } else {
        resultMessage = amqpTemplate.sendAndReceive(getRoutingKey(), m);
    }

    Object result = getMessageConverter().fromMessage(resultMessage);

    if (invocation.getMethod().getReturnType().getCanonicalName().equals(Void.class.getCanonicalName())) {
        return null;
    } else if (result instanceof Throwable
            && !invocation.getMethod().getReturnType().isAssignableFrom(result.getClass())) {
        // TODO handle for case where exceptions that are not known to the
        // caller are being thrown (might be nested unchecked exceptions)
        throw (Throwable) result;
    } else {
        return result;
    }
}