Package org.springframework.amqp.rabbit.core

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


  @Test
  public void testListenerRecoversFromClosedChannelAndStop() 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);
    int n = 0;
    while (n++ < 100 && container.getActiveConsumerCount() != concurrentConsumers) {
      Thread.sleep(50L);
    }
    assertEquals(concurrentConsumers, container.getActiveConsumerCount());

    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()));

    assertEquals(concurrentConsumers, container.getActiveConsumerCount());
    container.stop();
    assertEquals(0, container.getActiveConsumerCount());
View Full Code Here


  @Test
  public void testListenerRecoversFromClosedConnection() throws Exception {

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

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

    int timeout = Math.min(4 + messageCount / (4 * concurrentConsumers), 30);
    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 testListenerRecoversAndTemplateSharesConnectionFactory() throws Exception {

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

    acknowledgeMode = AcknowledgeMode.MANUAL;

    CountDownLatch latch = new CountDownLatch(messageCount);
    container = createContainer(queue.getName(), new ManualAckListener(latch), connectionFactory);
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend(queue.getName(), i + "foo");
      // Give the listener container a chance to steal the connection from the template
      Thread.sleep(200);
    }

    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) connectionFactory).destroy();

  }
View Full Code Here

    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    // queue doesn't exist during startup - verify we started, create queue and verify recovery
    Thread.sleep(5000);
    assertEquals(messageCount, latch.getCount());
    admin.declareQueue(new Queue("nonexistent"));
    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend("nonexistent", "foo" + i);
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    Map<?,?> consumers = TestUtils.getPropertyValue(container, "consumers", Map.class);
    assertEquals(1, consumers.size());
    Object consumer = consumers.keySet().iterator().next();

    // delete the queue and verify we recover again when it is recreated.
    admin.deleteQueue("nonexistent");
    Thread.sleep(3000);
    latch = new CountDownLatch(messageCount);
    container.setMessageListener(new MessageListenerAdapter(new VanillaListener(latch)));
    assertEquals(messageCount, latch.getCount());
    admin.declareQueue(new Queue("nonexistent"));
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend("nonexistent", "foo" + i);
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, consumers.size());
    assertNotSame(consumer, consumers.keySet().iterator().next());
  }
View Full Code Here

    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setConcurrentConsumers(20);
    container.setMessageListener(new MessageListenerAdapter(new SimpleAdapter(),messageConverter));
    container.start();

    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    template.setMessageConverter(messageConverter);
    List<BlockingQueue<?>> queues = getQueues(container);

    Thread.sleep(10000);
    int n = 0;
    while(true){
      for(int i=1; i<=200;i++){

        template.send("foo", "", new Message("foo # ID: id".replace("#", String.valueOf(i)).replace("id", java.util.UUID.randomUUID().toString()).getBytes(), messageProperties));

      }
      Thread.sleep(1000);
      if (++n % 10 == 0) {
        logger.warn(count(queues));
View Full Code Here

  }


  private void doTest(int concurrentConsumers, ContainerConfigurer configurer) {
    int messageCount = 10;
    RabbitTemplate template = new RabbitTemplate();
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    connectionFactory.setChannelCacheSize(concurrentConsumers);
    connectionFactory.setPort(BrokerTestUtils.getPort());
    template.setConnectionFactory(connectionFactory);
    SimpleMessageConverter messageConverter = new SimpleMessageConverter();
    messageConverter.setCreateMessageIds(true);
    template.setMessageConverter(messageConverter);
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend(queue1.getName(), new Integer(i));
      template.convertAndSend(queue2.getName(), new Integer(i));
    }
    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    final CountDownLatch latch = new CountDownLatch(messageCount * 2);
    PojoListener listener = new PojoListener(latch);
    container.setMessageListener(new MessageListenerAdapter(listener));
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setChannelTransacted(true);
    container.setConcurrentConsumers(concurrentConsumers);
    configurer.configure(container);
    container.afterPropertiesSet();
    container.start();
    try {
      int timeout = Math.min(1 + messageCount / concurrentConsumers, 30);
      boolean waited = latch.await(timeout, TimeUnit.SECONDS);
      logger.info("All messages recovered: " + waited);
      assertEquals(concurrentConsumers, container.getActiveConsumerCount());
      assertTrue("Timed out waiting for messages", waited);
    }
    catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new IllegalStateException("unexpected interruption");
    }
    finally {
      container.shutdown();
      assertEquals(0, container.getActiveConsumerCount());
    }
    assertNull(template.receiveAndConvert(queue1.getName()));
    assertNull(template.receiveAndConvert(queue2.getName()));

    connectionFactory.destroy();
  }
View Full Code Here

  public Log4jLevelAdjuster logLevels = new Log4jLevelAdjuster(Level.INFO, RabbitTemplate.class,
      SimpleMessageListenerContainer.class, BlockingQueueConsumer.class,
      MessageListenerContainerLifecycleIntegrationTests.class);

  private RabbitTemplate createTemplate(int concurrentConsumers) {
    RabbitTemplate template = new RabbitTemplate();
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    connectionFactory.setChannelCacheSize(concurrentConsumers);
    connectionFactory.setPort(BrokerTestUtils.getPort());
    template.setConnectionFactory(connectionFactory);
    return template;
  }
View Full Code Here

    doTest(MessageCount.HIGH, Concurrency.HIGH, TransactionMode.PREFETCH_NO_TX);
  }

  @Test
  public void testBadCredentials() throws Exception {
    RabbitTemplate template = createTemplate(1);
    com.rabbitmq.client.ConnectionFactory cf = new com.rabbitmq.client.ConnectionFactory();
    cf.setUsername("foo");
    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(cf);
    try {
      this.doTest(MessageCount.LOW, Concurrency.LOW, TransactionMode.OFF, template, connectionFactory);
View Full Code Here

      fail("expected FatalListenerStartupException:" + t.getClass() + ":" + t.getMessage());
    }
  }

  private void doTest(MessageCount level, Concurrency concurrency, TransactionMode transactionMode) throws Exception {
    RabbitTemplate template = createTemplate(concurrency.value);
    this.doTest(level, concurrency, transactionMode, template, template.getConnectionFactory());
  }
View Full Code Here

  public void testShutDownWithPrefetch() throws Exception {

    int messageCount = 10;
    int concurrentConsumers = 1;

    RabbitTemplate template = createTemplate(concurrentConsumers);

    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend(queue.getName(), i + "foo");
    }

    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(template.getConnectionFactory());
    final CountDownLatch prefetched = new CountDownLatch(1);
    final CountDownLatch awaitStart1 = new CountDownLatch(1);
    final CountDownLatch awaitStart2 = new CountDownLatch(6);
    final CountDownLatch awaitStop = new CountDownLatch(1);
    final AtomicInteger received = new AtomicInteger();
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.