Package org.springframework.amqp.rabbit.core

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


  private RetryTemplate retryTemplate;

  private MessageConverter messageConverter;

  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);
    if (messageConverter == null) {
      SimpleMessageConverter messageConverter = new SimpleMessageConverter();
      messageConverter.setCreateMessageIds(true);
      this.messageConverter = messageConverter;
    }
    template.setMessageConverter(messageConverter);
    return template;
  }
View Full Code Here


      return connectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
      return new RabbitTemplate(rabbitConnectionFactory());
    }
View Full Code Here

  @Test
  public void testWithNoId() throws Exception {
    // 2 messsages; each retried once by missing id interceptor
    this.latch = new CountDownLatch(4);
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml", this.getClass());
    RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
    ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener(new MessageListenerAdapter(new POJO()));
    container.setQueueNames("retry.test.queue");

    StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean();

    // use an external template so we can share his cache
    RetryTemplate retryTemplate = new RetryTemplate();
    RetryContextCache cache = new MapRetryContextCache();
    retryTemplate.setRetryContextCache(cache);
    fb.setRetryOperations(retryTemplate);

    // give him a reference to the retry cache so he can clean it up
    MissingMessageIdAdvice missingIdAdvice = new MissingMessageIdAdvice(cache);

    Advice retryInterceptor = fb.getObject();
    // add both advices
    container.setAdviceChain(new Advice[] {missingIdAdvice, retryInterceptor});
    container.start();

    template.convertAndSend("retry.test.exchange", "retry.test.binding", "Hello, world!");
    template.convertAndSend("retry.test.exchange", "retry.test.binding", "Hello, world!");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    Thread.sleep(2000);
    assertEquals(0, ((Map) new DirectFieldAccessor(cache).getPropertyValue("map")).size());
    container.stop();
    ctx.close();
View Full Code Here

  @Test
  public void testWithId() throws Exception {
    // 2 messsages; each retried twice by retry interceptor
    this.latch = new CountDownLatch(6);
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("retry-context.xml", this.getClass());
    RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
    ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener(new MessageListenerAdapter(new POJO()));
    container.setQueueNames("retry.test.queue");

    StatefulRetryOperationsInterceptorFactoryBean fb = new StatefulRetryOperationsInterceptorFactoryBean();

    // use an external template so we can share his cache
    RetryTemplate retryTemplate = new RetryTemplate();
    RetryContextCache cache = new MapRetryContextCache();
    retryTemplate.setRetryContextCache(cache);
    fb.setRetryOperations(retryTemplate);
    fb.setMessageRecoverer(new RejectAndDontRequeueRecoverer());

    // give him a reference to the retry cache so he can clean it up
    MissingMessageIdAdvice missingIdAdvice = new MissingMessageIdAdvice(cache);

    Advice retryInterceptor = fb.getObject();
    // add both advices
    container.setAdviceChain(new Advice[] {missingIdAdvice, retryInterceptor});
    container.start();

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("text/plain");
    messageProperties.setMessageId("foo");
    Message message = new Message("Hello, world!".getBytes(), messageProperties);
    template.send("retry.test.exchange", "retry.test.binding", message);
    template.send("retry.test.exchange", "retry.test.binding", message);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    Thread.sleep(2000);
    assertEquals(0, ((Map) new DirectFieldAccessor(cache).getPropertyValue("map")).size());
    container.stop();
    ctx.close();
View Full Code Here

    final CountDownLatch latch = new CountDownLatch(1);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cachingConnectionFactory);
    container.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(Message message) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
        rabbitTemplate.setChannelTransacted(true);
        // should use same channel as container
        rabbitTemplate.convertAndSend("foo", "bar", "baz");
        latch.countDown();
      }
    });
    container.setQueueNames("queue");
    container.setChannelTransacted(true);
View Full Code Here

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new ChannelAwareMessageListener() {
      @Override
      public void onMessage(Message message, Channel channel) {
        exposed.set(channel);
        RabbitTemplate rabbitTemplate = new RabbitTemplate(singleConnectionFactory);
        rabbitTemplate.setChannelTransacted(true);
        // should use same channel as container
        rabbitTemplate.convertAndSend("foo", "bar", "baz");
        latch.countDown();
      }

    });
    container.setQueueNames("queue");
View Full Code Here

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory);
    container.setMessageListener(new ChannelAwareMessageListener() {
      @Override
      public void onMessage(Message message, Channel channel) {
        exposed.set(channel);
        RabbitTemplate rabbitTemplate = new RabbitTemplate(singleConnectionFactory);
        rabbitTemplate.setChannelTransacted(true);
        // should use same channel as container
        rabbitTemplate.convertAndSend("foo", "bar", "baz");
        latch.countDown();
      }

    });
    container.setQueueNames("queue");
View Full Code Here

  @Repeat(1000)
  public void testListenerRecoversFromClosedConnection() throws Exception {

    // logger.info("Testing...");

    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    CountDownLatch latch = new CountDownLatch(messageCount);
    listener.setLatch(latch);

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

  }
View Full Code Here

    List<QueueInfo> queues = brokerAdmin.getQueues();
    logger.info("Queues: " + queues);
    assertEquals(1, queues.size());
    assertTrue(queues.get(0).isDurable());

    RabbitTemplate template = new RabbitTemplate(connectionFactory);

    CountDownLatch latch = new CountDownLatch(messageCount);
    assertEquals("No more messages to receive before even sent!", messageCount, latch.getCount());
    container = createContainer(queue.getName(), new VanillaListener(latch), connectionFactory);
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend(queue.getName(), i + "foo");
    }

    assertTrue("No more messages to receive before broker stopped", latch.getCount() > 0);
    brokerAdmin.stopBrokerApplication();
    assertTrue("No more messages to receive after broker stopped", latch.getCount() > 0);
    boolean waited = latch.await(500, TimeUnit.MILLISECONDS);
    assertFalse("Did not time out waiting for message", waited);

    container.stop();
    assertEquals(0, container.getActiveConsumerCount());

    brokerAdmin.startBrokerApplication();
    queues = brokerAdmin.getQueues();
    logger.info("Queues: " + queues);
    container.start();
    assertEquals(concurrentConsumers, container.getActiveConsumerCount());

    int timeout = Math.min(4 + messageCount / (4 * concurrentConsumers), 30);
    logger.debug("Waiting for messages with timeout = " + timeout + " (s)");
    waited = latch.await(timeout, TimeUnit.SECONDS);
    assertTrue("Timed out waiting for message", waited);

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

  }
View Full Code Here

  @Before
  public void init() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    template = new RabbitTemplate(connectionFactory);
    template.setChannelTransacted(true);
    RabbitTransactionManager transactionManager = new RabbitTransactionManager(connectionFactory);
    transactionTemplate = new TransactionTemplate(transactionManager);
  }
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.