Package org.springframework.amqp.rabbit.core

Examples of org.springframework.amqp.rabbit.core.RabbitTemplate


    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);
View Full Code Here


      BlockingQueueConsumerIntegrationTests.class);

  @Test
  public void testTransactionalLowLevel() throws Exception {

    RabbitTemplate template = new RabbitTemplate();
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    connectionFactory.setPort(BrokerTestUtils.getPort());
    template.setConnectionFactory(connectionFactory);

    BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory,
        new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(),
        AcknowledgeMode.AUTO, true, 1, queue.getName());
    blockingQueueConsumer.start();

    // TODO: make this into a proper assertion. An exception can be thrown here by the Rabbit client and printed to
    // stderr without being rethrown (so hard to make a test fail).
    blockingQueueConsumer.stop();
    assertNull(template.receiveAndConvert(queue.getName()));
    connectionFactory.destroy();

  }
View Full Code Here

    }
  }

  @Test
  public void testStopStart() throws Exception {
    RabbitTemplate template = context.getBean(RabbitTemplate.class);
    template.convertAndSend("testContainerWithAutoDeleteQueues", "anon", "foo");
    assertNotNull(queue.poll(10, TimeUnit.SECONDS));
    this.listenerContainer1.stop();
    RabbitAdmin admin = spy(TestUtils.getPropertyValue(this.listenerContainer1, "rabbitAdmin", RabbitAdmin.class));
    new DirectFieldAccessor(this.listenerContainer1).setPropertyValue("rabbitAdmin", admin);
    this.listenerContainer1.start();
    template.convertAndSend("testContainerWithAutoDeleteQueues", "anon", "foo");
    assertNotNull(queue.poll(10, TimeUnit.SECONDS));
    verify(admin, times(1)).initialize(); // should only be called by one of the consumers
  }
View Full Code Here

    verify(admin, times(1)).initialize(); // should only be called by one of the consumers
  }

  @Test
  public void testStopStartConditionalDeclarations() throws Exception {
    RabbitTemplate template = context.getBean(RabbitTemplate.class);
    this.listenerContainer2.start();
    template.convertAndSend("otherExchange", "otherAnon", "foo");
    assertNotNull(queue.poll(10, TimeUnit.SECONDS));
    this.listenerContainer2.stop();
    this.listenerContainer2.start();
    template.convertAndSend("otherExchange", "otherAnon", "foo");
    assertNotNull(queue.poll(10, TimeUnit.SECONDS));
  }
View Full Code Here

    assertNotNull(queue.poll(10, TimeUnit.SECONDS));
  }

  @Test
  public void testRedeclareXExpiresQueue() throws Exception {
    RabbitTemplate template = context.getBean(RabbitTemplate.class);
    template.convertAndSend("xExpires", "foo");
    assertNotNull(queue.poll(10, TimeUnit.SECONDS));
    SimpleMessageListenerContainer listenerContainer = context.getBean("container3",
        SimpleMessageListenerContainer.class);
    listenerContainer.stop();
    RabbitAdmin admin = spy(TestUtils.getPropertyValue(listenerContainer, "rabbitAdmin", RabbitAdmin.class));
    new DirectFieldAccessor(listenerContainer).setPropertyValue("rabbitAdmin", admin);
    int n = 0;
    while (admin.getQueueProperties("xExpires") != null && n < 100) {
      Thread.sleep(100);
      n++;
    }
    assertTrue(n < 100);
    listenerContainer.start();
    template.convertAndSend("xExpires", "foo");
    assertNotNull(queue.poll(10, TimeUnit.SECONDS));
    verify(admin, times(1)).initialize(); // should only be called by one of the consumers
  }
View Full Code Here

    verify(admin, times(1)).initialize(); // should only be called by one of the consumers
  }

  @Test
  public void testAutoDeclareFalse() throws Exception {
    RabbitTemplate template = context.getBean(RabbitTemplate.class);
    template.convertAndSend("testContainerWithAutoDeleteQueues", "anon2", "foo");
    assertNotNull(queue.poll(10, TimeUnit.SECONDS));
    SimpleMessageListenerContainer listenerContainer = context.getBean("container4",
        SimpleMessageListenerContainer.class);
    listenerContainer.stop();
    RabbitAdmin admin = spy(TestUtils.getPropertyValue(listenerContainer, "rabbitAdmin", RabbitAdmin.class));
View Full Code Here

  @Test
  public void testListenerSendsMessageAndThenContainerCommits() throws Exception {

    ConnectionFactory connectionFactory = createConnectionFactory();
    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    new RabbitAdmin(connectionFactory).declareQueue(sendQueue);

    acknowledgeMode = AcknowledgeMode.AUTO;
    transactional = true;

    CountDownLatch latch = new CountDownLatch(1);
    container = createContainer(queue.getName(), new ChannelSenderListener(sendQueue.getName(), latch, false),
        connectionFactory);
    template.convertAndSend(queue.getName(), "foo");

    int timeout = getTimeout();
    logger.debug("Waiting for messages with timeout = " + timeout + " (s)");
    boolean waited = latch.await(timeout, TimeUnit.SECONDS);
    assertTrue("Timed out waiting for message", waited);

    // Give message time to reach broker (intermittent test failures)!
    Thread.sleep(500L);
    // All messages committed
    byte[] bytes = (byte[]) template.receiveAndConvert(sendQueue.getName());
    assertNotNull(bytes);
    assertEquals("bar", new String(bytes));
    assertEquals(null, template.receiveAndConvert(queue.getName()));

    ((DisposableBean) connectionFactory).destroy();

  }
View Full Code Here

  @Test
  public void testListenerSendsMessageAndThenRollback() throws Exception {

    ConnectionFactory connectionFactory = createConnectionFactory();
    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    new RabbitAdmin(connectionFactory).declareQueue(sendQueue);

    acknowledgeMode = AcknowledgeMode.AUTO;
    transactional = true;

    CountDownLatch latch = new CountDownLatch(1);
    container = createContainer(queue.getName(), new ChannelSenderListener(sendQueue.getName(), latch, true),
        connectionFactory);
    template.convertAndSend(queue.getName(), "foo");

    int timeout = getTimeout();
    logger.debug("Waiting for messages with timeout = " + timeout + " (s)");
    boolean waited = latch.await(timeout, TimeUnit.SECONDS);
    assertTrue("Timed out waiting for message", waited);

    container.stop();
    Thread.sleep(200L);

    // Foo message is redelivered
    assertEquals("foo", template.receiveAndConvert(queue.getName()));
    // Sending of bar message is also rolled back
    assertNull(template.receiveAndConvert(sendQueue.getName()));

    ((DisposableBean) connectionFactory).destroy();

  }
View Full Code Here

  @Test
  public void testListenerRecoversFromBogusDoubleAck() throws Exception {

    ConnectionFactory connectionFactory1 = createConnectionFactory();
    RabbitTemplate template = new RabbitTemplate(connectionFactory1);

    acknowledgeMode = AcknowledgeMode.MANUAL;

    CountDownLatch latch = new CountDownLatch(messageCount);
    ConnectionFactory connectionFactory2 = createConnectionFactory();
    container = createContainer(queue.getName(), new ManualAckListener(latch), connectionFactory2);
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend(queue.getName(), i + "foo");
    }

    int timeout = getTimeout();
    logger.debug("Waiting for messages with timeout = " + timeout + " (s)");
    boolean waited = latch.await(timeout, TimeUnit.SECONDS);
    assertTrue("Timed out waiting for message", waited);

    assertNull(template.receiveAndConvert(queue.getName()));

    ((DisposableBean) connectionFactory1).destroy();
    ((DisposableBean) connectionFactory2).destroy();
  }
View Full Code Here

  @Test
  public void testListenerRecoversFromClosedChannel() throws Exception {

    ConnectionFactory connectionFactory1 = createConnectionFactory();
    RabbitTemplate template = new RabbitTemplate(connectionFactory1);

    CountDownLatch latch = new CountDownLatch(messageCount);
    ConnectionFactory connectionFactory2 = createConnectionFactory();
    container = createContainer(queue.getName(), new AbortChannelListener(latch), connectionFactory2);
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend(queue.getName(), i + "foo");
    }

    int timeout = getTimeout();
    logger.debug("Waiting for messages with timeout = " + timeout + " (s)");
    boolean waited = latch.await(timeout, TimeUnit.SECONDS);
    assertTrue("Timed out waiting for message", waited);

    assertNull(template.receiveAndConvert(queue.getName()));

    ((DisposableBean) connectionFactory1).destroy();
    ((DisposableBean) connectionFactory2).destroy();
  }
View Full Code Here

TOP

Related Classes of org.springframework.amqp.rabbit.core.RabbitTemplate

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.