Example usage for org.springframework.amqp.rabbit.core RabbitTemplate setRoutingKey

List of usage examples for org.springframework.amqp.rabbit.core RabbitTemplate setRoutingKey

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.core RabbitTemplate setRoutingKey.

Prototype

public void setRoutingKey(String routingKey) 

Source Link

Document

The value of a default routing key to use for send operations when none is specified.

Usage

From source file:com.apress.prospringintegration.messaging.rabbitmq.jms.adapter.RabbitmqConfiguration.java

@Bean
public RabbitTemplate amqpTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate();
    rabbitTemplate.setConnectionFactory(connectionFactory());
    rabbitTemplate.setRoutingKey("ticket.queue");
    rabbitTemplate.setQueue("ticket.queue");

    return rabbitTemplate;
}

From source file:be.rufer.playground.amqpclient.config.RabbitConfig.java

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
    //The routing key is set to the name of the queue by the broker for the default exchange.
    rabbitTemplate.setRoutingKey(this.queueName);
    return rabbitTemplate;
}

From source file:io.manasobi.commons.config.AmqpConfig.java

@Bean
public RabbitTemplate rabbitTemplate() {

    RabbitTemplate rabbitTemplate = new RabbitTemplate();

    rabbitTemplate.setQueue("manasobi");
    //rabbitTemplate.setRoutingKey("manasobi.key");
    //rabbitTemplate.setExchange("manasobi.key");
    rabbitTemplate.setRoutingKey("key.manasobi");
    rabbitTemplate.setExchange("manasobi");

    rabbitTemplate.setConnectionFactory(rabbitConnectionFactory());

    return rabbitTemplate;
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java

@Test
public void testAtomicSendAndReceive() throws Exception {
    final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory());
    template.setRoutingKey(ROUTE);
    template.setQueue(ROUTE);//  ww w.  j  av a 2 s .co m
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<Message> received = executor.submit(new Callable<Message>() {

        public Message call() throws Exception {
            Message message = null;
            for (int i = 0; i < 10; i++) {
                message = template.receive();
                if (message != null) {
                    break;
                }
                Thread.sleep(100L);
            }
            assertNotNull("No message received", message);
            template.send(message.getMessageProperties().getReplyTo(), message);
            return message;
        }

    });
    Message message = new Message("test-message".getBytes(), new MessageProperties());
    Message reply = template.sendAndReceive(message);
    assertEquals(new String(message.getBody()),
            new String(received.get(1000, TimeUnit.MILLISECONDS).getBody()));
    assertNotNull("Reply is expected", reply);
    assertEquals(new String(message.getBody()), new String(reply.getBody()));
    // Message was consumed so nothing left on queue
    reply = template.receive();
    assertEquals(null, reply);
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java

@Test
public void testAtomicSendAndReceiveExternalExecutor() throws Exception {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
    final String execName = "make-sure-exec-passed-in";
    exec.setBeanName(execName);/*from  w  w  w  . jav  a  2 s  .  co  m*/
    exec.afterPropertiesSet();
    connectionFactory.setExecutor(exec);
    final Field[] fields = new Field[1];
    ReflectionUtils.doWithFields(RabbitTemplate.class, new FieldCallback() {
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            field.setAccessible(true);
            fields[0] = field;
        }
    }, new FieldFilter() {
        public boolean matches(Field field) {
            return field.getName().equals("logger");
        }
    });
    Log logger = Mockito.mock(Log.class);
    when(logger.isTraceEnabled()).thenReturn(true);

    final AtomicBoolean execConfiguredOk = new AtomicBoolean();

    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String log = (String) invocation.getArguments()[0];
            if (log.startsWith("Message received") && Thread.currentThread().getName().startsWith(execName)) {
                execConfiguredOk.set(true);
            }
            return null;
        }
    }).when(logger).trace(Mockito.anyString());
    final RabbitTemplate template = new RabbitTemplate(connectionFactory);
    ReflectionUtils.setField(fields[0], template, logger);
    template.setRoutingKey(ROUTE);
    template.setQueue(ROUTE);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<Message> received = executor.submit(new Callable<Message>() {

        public Message call() throws Exception {
            Message message = null;
            for (int i = 0; i < 10; i++) {
                message = template.receive();
                if (message != null) {
                    break;
                }
                Thread.sleep(100L);
            }
            assertNotNull("No message received", message);
            template.send(message.getMessageProperties().getReplyTo(), message);
            return message;
        }

    });
    Message message = new Message("test-message".getBytes(), new MessageProperties());
    Message reply = template.sendAndReceive(message);
    assertEquals(new String(message.getBody()),
            new String(received.get(1000, TimeUnit.MILLISECONDS).getBody()));
    assertNotNull("Reply is expected", reply);
    assertEquals(new String(message.getBody()), new String(reply.getBody()));
    // Message was consumed so nothing left on queue
    reply = template.receive();
    assertEquals(null, reply);

    assertTrue(execConfiguredOk.get());
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java

@Test
public void testAtomicSendAndReceiveWithConversion() throws Exception {
    final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory());
    template.setRoutingKey(ROUTE);
    template.setQueue(ROUTE);/* w ww .  j a  v  a 2s  .  c  om*/
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<String> received = executor.submit(new Callable<String>() {

        public String call() throws Exception {
            Message message = null;
            for (int i = 0; i < 10; i++) {
                message = template.receive();
                if (message != null) {
                    break;
                }
                Thread.sleep(100L);
            }
            assertNotNull("No message received", message);
            template.send(message.getMessageProperties().getReplyTo(), message);
            return (String) template.getMessageConverter().fromMessage(message);
        }

    });
    String result = (String) template.convertSendAndReceive("message");
    assertEquals("message", received.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("message", result);
    // Message was consumed so nothing left on queue
    result = (String) template.receiveAndConvert();
    assertEquals(null, result);
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java

@Test
public void testAtomicSendAndReceiveWithConversionAndMessagePostProcessor() throws Exception {
    final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory());
    template.setRoutingKey(ROUTE);
    template.setQueue(ROUTE);//from  w  w w  . ja v  a  2 s. com
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<String> received = executor.submit(new Callable<String>() {

        public String call() throws Exception {
            Message message = null;
            for (int i = 0; i < 10; i++) {
                message = template.receive();
                if (message != null) {
                    break;
                }
                Thread.sleep(100L);
            }
            assertNotNull("No message received", message);
            template.send(message.getMessageProperties().getReplyTo(), message);
            return (String) template.getMessageConverter().fromMessage(message);
        }

    });
    String result = (String) template.convertSendAndReceive((Object) "message", new MessagePostProcessor() {
        public Message postProcessMessage(Message message) throws AmqpException {
            try {
                byte[] newBody = new String(message.getBody(), "UTF-8").toUpperCase().getBytes("UTF-8");
                return new Message(newBody, message.getMessageProperties());
            } catch (Exception e) {
                throw new AmqpException("unexpected failure in test", e);
            }
        }
    });
    assertEquals("MESSAGE", received.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("MESSAGE", result);
    // Message was consumed so nothing left on queue
    result = (String) template.receiveAndConvert();
    assertEquals(null, result);
}