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(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) 

Source Link

Document

Create a new SingleConnectionFactory for the given target ConnectionFactory.

Usage

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

@Bean
public SingleConnectionFactory connectionFactory() {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
    connectionFactory.setPort(5672);/* w  ww  .j  a va 2 s  . co  m*/
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");

    return connectionFactory;
}

From source file:com.jbrisbin.vpc.jobsched.QueueConfiguration.java

@Bean
public ConnectionFactory connectionFactory() {
    if (null == connectionFactory) {
        com.rabbitmq.client.ConnectionFactory rabbitFactory = new com.rabbitmq.client.ConnectionFactory();
        rabbitFactory.setHost(brokerHost);
        rabbitFactory.setPort(brokerPort);
        rabbitFactory.setUsername(brokerUser);
        rabbitFactory.setPassword(brokerPassword);
        rabbitFactory.setVirtualHost(brokerVirtualHost);
        connectionFactory = new SingleConnectionFactory(rabbitFactory);
    }// w  ww  . j  a v  a 2 s  .  co m
    return connectionFactory;
}

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

@Test
public void testNoFailOnStartupWithMissingBroker() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory("foo");
    connectionFactory.setPort(434343);/*from w ww . j a  v a  2  s . co  m*/
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.getBeanFactory().registerSingleton("foo", new Queue("queue"));
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    rabbitAdmin.setApplicationContext(applicationContext);
    rabbitAdmin.setAutoStartup(true);
    rabbitAdmin.afterPropertiesSet();
    connectionFactory.destroy();
}

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

@Test
public void testFailOnFirstUseWithMissingBroker() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
    connectionFactory.setPort(434343);/*w  ww . j  a v  a2  s  .c  om*/
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.getBeanFactory().registerSingleton("foo", new Queue("queue"));
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    rabbitAdmin.setApplicationContext(applicationContext);
    rabbitAdmin.setAutoStartup(true);
    rabbitAdmin.afterPropertiesSet();
    exception.expect(IllegalArgumentException.class);
    rabbitAdmin.declareQueue();
    connectionFactory.destroy();
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerLongTests.java

private void testChangeConsumerCountGuts(boolean transacted) throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    try {//from  w w w.  j ava 2  s  .  c om
        container.setMessageListener(new MessageListenerAdapter(this));
        container.setQueueNames("foo");
        container.setAutoStartup(false);
        container.setConcurrentConsumers(2);
        container.setChannelTransacted(transacted);
        container.afterPropertiesSet();
        assertEquals(2, ReflectionTestUtils.getField(container, "concurrentConsumers"));
        container.start();
        waitForNConsumers(container, 2);
        container.setConcurrentConsumers(1);
        waitForNConsumers(container, 1);
        container.setMaxConcurrentConsumers(3);
        RabbitTemplate template = new RabbitTemplate(singleConnectionFactory);
        for (int i = 0; i < 20; i++) {
            template.convertAndSend("foo", "foo");
        }
        waitForNConsumers(container, 2); // increased consumers due to work
        waitForNConsumers(container, 1, 20000); // should stop the extra consumer after 10 seconds idle
        container.setConcurrentConsumers(3);
        waitForNConsumers(container, 3);
        container.stop();
        waitForNConsumers(container, 0);
        singleConnectionFactory.destroy();
    } finally {
        container.stop();
    }
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerLongTests.java

@Test
public void testAddQueuesAndStartInCycle() throws Exception {
    final SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener((MessageListener) message -> {
    });//from   ww w. ja v  a  2 s.  c o  m
    container.setConcurrentConsumers(2);
    container.afterPropertiesSet();

    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    for (int i = 0; i < 20; i++) {
        AnonymousQueue anonymousQueue = new AnonymousQueue();
        admin.declareQueue(anonymousQueue);
        container.addQueueNames(anonymousQueue.getName());
        if (!container.isRunning()) {
            container.start();
        }
    }

    int n = 0;
    while (n++ < 100 && container.getActiveConsumerCount() != 2) {
        Thread.sleep(100);
    }
    assertEquals(2, container.getActiveConsumerCount());
    container.stop();
    connectionFactory.destroy();
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java

@Test
public void testChannelTransactedOverriddenWhenTxManager() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setChannelTransacted(false);
    container.setTransactionManager(new TestTransactionManager());
    container.afterPropertiesSet();/*from w ww  . ja v  a  2 s .c om*/
    assertTrue(TestUtils.getPropertyValue(container, "transactional", Boolean.class));
    container.stop();
    singleConnectionFactory.destroy();
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java

@Test
public void testInconsistentTransactionConfiguration() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setChannelTransacted(false);
    container.setAcknowledgeMode(AcknowledgeMode.NONE);
    container.setTransactionManager(new TestTransactionManager());
    expectedException.expect(IllegalStateException.class);
    container.afterPropertiesSet();//from w ww .  j a  v  a 2s . c  o  m
    container.stop();
    singleConnectionFactory.destroy();
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java

@Test
public void testInconsistentAcknowledgeConfiguration() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setChannelTransacted(true);
    container.setAcknowledgeMode(AcknowledgeMode.NONE);
    expectedException.expect(IllegalStateException.class);
    container.afterPropertiesSet();/*  w w w  .j  a v a2s  .  co m*/
    container.stop();
    singleConnectionFactory.destroy();
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java

@Test
public void testDefaultConsumerCount() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setAutoStartup(false);//from  www. j  a va2 s  .  c  om
    container.afterPropertiesSet();
    assertEquals(1, ReflectionTestUtils.getField(container, "concurrentConsumers"));
    container.stop();
    singleConnectionFactory.destroy();
}