Package org.springframework.amqp.rabbit.connection

Examples of org.springframework.amqp.rabbit.connection.SingleConnectionFactory


  @Rule
  public BrokerRunning brokerRunning = BrokerRunning.isRunning();

  @Test
  public void testChangeConsumerCount() throws Exception {
    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new MessageListenerAdapter(this));
    container.setQueueNames("foo");
    container.setAutoStartup(false);
    container.setConcurrentConsumers(2);
    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();
  }
View Full Code Here


    singleConnectionFactory.destroy();
  }

  @Test
  public void testAddQueuesAndStartInCycle() throws Exception {
    final SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener(new MessageListener() {

      @Override
      public void onMessage(Message message) {
      }
    });
    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();
  }
View Full Code Here

    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    final Channel onlyChannel = mock(Channel.class);
    when(onlyChannel.isOpen()).thenReturn(true);

    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(mockConnectionFactory);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);

    final AtomicReference<Exception> tooManyChannels = new AtomicReference<Exception>();
View Full Code Here

    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    final Channel onlyChannel = mock(Channel.class);
    when(onlyChannel.isOpen()).thenReturn(true);

    final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(mockConnectionFactory);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);

    final AtomicReference<Exception> tooManyChannels = new AtomicReference<Exception>();
View Full Code Here

    }
  }

  @Test
  public void testNoFailOnStartupWithMissingBroker() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory("foo");
    connectionFactory.setPort(434343);
    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();
  }
View Full Code Here

    connectionFactory.destroy();
  }

  @Test
  public void testFailOnFirstUseWithMissingBroker() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory("foo");
    connectionFactory.setPort(434343);
    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();
  }
View Full Code Here

    connectionFactory.destroy();
  }

  @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 {
      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();
    }
  }
View Full Code Here

  /**
   * @param args
   */
  public static void main(String[] args) {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");

    RabbitTemplate template = new RabbitTemplate();
    template.setConnectionFactory(connectionFactory);
    template.setChannelTransacted(true);
    template.afterPropertiesSet();
View Full Code Here

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory));
    Queue replyQueue = new Queue("new.replyTo");
    template.setReplyQueue(replyQueue);
    if (!standardHeader) {
      template.setCorrelationKey(CORRELATION_HEADER);
    }
View Full Code Here

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory));
    Queue replyQueue = new Queue("new.replyTo");
    template.setReplyQueue(replyQueue);
    template.setReplyTimeout(60000);

    MessageProperties messageProperties = new MessageProperties();
View Full Code Here

TOP

Related Classes of org.springframework.amqp.rabbit.connection.SingleConnectionFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.