Example usage for org.springframework.amqp.rabbit.connection SingleConnectionFactory SingleConnectionFactory

List of usage examples for org.springframework.amqp.rabbit.connection SingleConnectionFactory SingleConnectionFactory

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.connection SingleConnectionFactory SingleConnectionFactory.

Prototype

public SingleConnectionFactory() 

Source Link

Document

Create a new SingleConnectionFactory initializing the hostname to be the value returned from InetAddress.getLocalHost(), or "localhost" if getLocalHost() throws an exception.

Usage

From source file:org.springframework.amqp.rabbit.admin.RabbitBrokerAdminIntegrationTests.java

@Test
public void testGetQueues() throws Exception {
    ConnectionFactory connectionFactory = new SingleConnectionFactory();
    new RabbitAdmin(connectionFactory).declareQueue(new Queue("test.queue"));
    assertEquals("/", connectionFactory.getVirtualHost());
    List<QueueInfo> queues = brokerAdmin.getQueues();
    assertEquals("test.queue", queues.get(0).getName());
}

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

@Test
public void testProperties() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
    connectionFactory.setHost("localhost");
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    String queueName = "test.properties." + System.currentTimeMillis();
    try {//from w w w . j  a  v  a  2 s. c o  m
        rabbitAdmin.declareQueue(new Queue(queueName));
        new RabbitTemplate(connectionFactory).convertAndSend(queueName, "foo");
        int n = 0;
        while (n++ < 100 && messageCount(rabbitAdmin, queueName) == 0) {
            Thread.sleep(100);
        }
        assertTrue("Message count = 0", n < 100);
        Channel channel = connectionFactory.createConnection().createChannel(false);
        DefaultConsumer consumer = new DefaultConsumer(channel);
        channel.basicConsume(queueName, true, consumer);
        n = 0;
        while (n++ < 100 && messageCount(rabbitAdmin, queueName) > 0) {
            Thread.sleep(100);
        }
        assertTrue("Message count > 0", n < 100);
        Properties props = rabbitAdmin.getQueueProperties(queueName);
        assertNotNull(props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT));
        assertEquals(1, props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT));
        channel.close();
    } finally {
        rabbitAdmin.deleteQueue(queueName);
        connectionFactory.destroy();
    }
}

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

@Test
public void testTemporaryLogs() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
    connectionFactory.setHost("localhost");
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    try {//from  w  w w  . ja va2  s .c o  m
        ApplicationContext ctx = mock(ApplicationContext.class);
        Map<String, Queue> queues = new HashMap<String, Queue>();
        queues.put("nonDurQ", new Queue("testq.nonDur", false, false, false));
        queues.put("adQ", new Queue("testq.ad", true, false, true));
        queues.put("exclQ", new Queue("testq.excl", true, true, false));
        queues.put("allQ", new Queue("testq.all", false, true, true));
        when(ctx.getBeansOfType(Queue.class)).thenReturn(queues);
        Map<String, Exchange> exchanges = new HashMap<String, Exchange>();
        exchanges.put("nonDurEx", new DirectExchange("testex.nonDur", false, false));
        exchanges.put("adEx", new DirectExchange("testex.ad", true, true));
        exchanges.put("allEx", new DirectExchange("testex.all", false, true));
        when(ctx.getBeansOfType(Exchange.class)).thenReturn(exchanges);
        rabbitAdmin.setApplicationContext(ctx);
        rabbitAdmin.afterPropertiesSet();
        Log logger = spy(TestUtils.getPropertyValue(rabbitAdmin, "logger", Log.class));
        doReturn(true).when(logger).isInfoEnabled();
        doAnswer(new DoesNothing()).when(logger).info(anyString());
        new DirectFieldAccessor(rabbitAdmin).setPropertyValue("logger", logger);
        connectionFactory.createConnection().close(); // force declarations
        ArgumentCaptor<String> log = ArgumentCaptor.forClass(String.class);
        verify(logger, times(7)).info(log.capture());
        List<String> logs = log.getAllValues();
        Collections.sort(logs);
        assertThat(logs.get(0), Matchers.containsString("(testex.ad) durable:true, auto-delete:true"));
        assertThat(logs.get(1), Matchers.containsString("(testex.all) durable:false, auto-delete:true"));
        assertThat(logs.get(2), Matchers.containsString("(testex.nonDur) durable:false, auto-delete:false"));
        assertThat(logs.get(3),
                Matchers.containsString("(testq.ad) durable:true, auto-delete:true, exclusive:false"));
        assertThat(logs.get(4),
                Matchers.containsString("(testq.all) durable:false, auto-delete:true, exclusive:true"));
        assertThat(logs.get(5),
                Matchers.containsString("(testq.excl) durable:true, auto-delete:false, exclusive:true"));
        assertThat(logs.get(6),
                Matchers.containsString("(testq.nonDur) durable:false, auto-delete:false, exclusive:false"));
    } finally {
        cleanQueuesAndExchanges(rabbitAdmin);
        connectionFactory.destroy();
    }
}

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

@Test
public void testSendAndReceiveTransactedWithUncachedConnection() throws Exception {
    RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory());
    template.setChannelTransacted(true);
    template.convertAndSend(ROUTE, "message");
    String result = (String) template.receiveAndConvert(ROUTE);
    assertEquals("message", result);
    result = (String) template.receiveAndConvert(ROUTE);
    assertEquals(null, result);//from w  w  w  .j a v a2s. co m
}