Package org.springframework.amqp.rabbit.connection

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


    connectionFactory.destroy();
  }

  @Test
  public void testAtomicSendAndReceiveWithRoutingKey() throws Exception {
    final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setHost("localhost");
    final RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<Message> received = executor.submit(new Callable<Message>() {

      @Override
      public Message call() throws Exception {
        Message message = null;
        for (int i = 0; i < 10; i++) {
          message = template.receive(ROUTE);
          if (message != null) {
            break;
          }
          Thread.sleep(100L);
        }
        assertNotNull("No message received", message);
        template.send(message.getMessageProperties().getReplyTo(), message);
        return message;
      }

    });
    Message message = new Message("test-message".getBytes(), new MessageProperties());
    Message reply = template.sendAndReceive(ROUTE, message);
    assertEquals(new String(message.getBody()), new String(received.get(1000, TimeUnit.MILLISECONDS).getBody()));
    assertNotNull("Reply is expected", reply);
    assertEquals(new String(message.getBody()), new String(reply.getBody()));
    // Message was consumed so nothing left on queue
    reply = template.receive(ROUTE);
    assertEquals(null, reply);
    cachingConnectionFactory.destroy();
  }
View Full Code Here


    cachingConnectionFactory.destroy();
  }

  @Test
  public void testAtomicSendAndReceiveWithExchangeAndRoutingKey() throws Exception {
    final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setHost("localhost");
    final RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<Message> received = executor.submit(new Callable<Message>() {

      @Override
      public Message call() throws Exception {
        Message message = null;
        for (int i = 0; i < 10; i++) {
          message = template.receive(ROUTE);
          if (message != null) {
            break;
          }
          Thread.sleep(100L);
        }
        assertNotNull("No message received", message);
        template.send(message.getMessageProperties().getReplyTo(), message);
        return message;
      }

    });
    Message message = new Message("test-message".getBytes(), new MessageProperties());
    Message reply = template.sendAndReceive("", ROUTE, message);
    assertEquals(new String(message.getBody()), new String(received.get(1000, TimeUnit.MILLISECONDS).getBody()));
    assertNotNull("Reply is expected", reply);
    assertEquals(new String(message.getBody()), new String(reply.getBody()));
    // Message was consumed so nothing left on queue
    reply = template.receive(ROUTE);
    assertEquals(null, reply);
    cachingConnectionFactory.destroy();
  }
View Full Code Here

    cachingConnectionFactory.destroy();
  }

  @Test
  public void testAtomicSendAndReceiveWithConversion() throws Exception {
    final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setHost("localhost");
    final RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory);
    template.setRoutingKey(ROUTE);
    template.setQueue(ROUTE);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<String> received = executor.submit(new Callable<String>() {

      @Override
      public String call() throws Exception {
        Message message = null;
        for (int i = 0; i < 10; i++) {
          message = template.receive();
          if (message != null) {
            break;
          }
          Thread.sleep(100L);
        }
        assertNotNull("No message received", message);
        template.send(message.getMessageProperties().getReplyTo(), message);
        return (String) template.getMessageConverter().fromMessage(message);
      }

    });
    String result = (String) template.convertSendAndReceive("message");
    assertEquals("message", received.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("message", result);
    // Message was consumed so nothing left on queue
    result = (String) template.receiveAndConvert();
    assertEquals(null, result);
    cachingConnectionFactory.destroy();
  }
View Full Code Here

    assertEquals(null, result);
  }

  @Test
  public void testAtomicSendAndReceiveWithConversionAndMessagePostProcessor() throws Exception {
    final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setHost("localhost");
    final RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory);
    template.setRoutingKey(ROUTE);
    template.setQueue(ROUTE);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<String> received = executor.submit(new Callable<String>() {

      @Override
      public String call() throws Exception {
        Message message = null;
        for (int i = 0; i < 10; i++) {
          message = template.receive();
          if (message != null) {
            break;
          }
          Thread.sleep(100L);
        }
        assertNotNull("No message received", message);
        template.send(message.getMessageProperties().getReplyTo(), message);
        return (String) template.getMessageConverter().fromMessage(message);
      }

    });
    String result = (String) template.convertSendAndReceive((Object) "message", new MessagePostProcessor() {
      @Override
      public Message postProcessMessage(Message message) throws AmqpException {
        try {
          byte[] newBody = new String(message.getBody(), "UTF-8").toUpperCase().getBytes("UTF-8");
          return new Message(newBody, message.getMessageProperties());
        }
        catch (Exception e) {
          throw new AmqpException("unexpected failure in test", e);
        }
      }
    });
    assertEquals("MESSAGE", received.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("MESSAGE", result);
    // Message was consumed so nothing left on queue
    result = (String) template.receiveAndConvert();
    assertEquals(null, result);
    cachingConnectionFactory.destroy();
  }
View Full Code Here

  @Rule
  public BrokerRunning brokerIsRunning = BrokerRunning.isRunningWithEmptyQueues(queue);

  @Before
  public void setup() {
    connectionFactory = new CachingConnectionFactory(BrokerTestUtils.getPort());
    connectionFactory.setHost("localhost");
    template = new RabbitTemplate(connectionFactory);
  }
View Full Code Here

    admin.declareExchange(exchange);
    template.setExchange(exchange.getName());

    admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("*.end"));

    final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setHost("localhost");
    final RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory);
    template.setExchange(exchange.getName());

    BlockingQueueConsumer consumer = template.execute(new ChannelCallback<BlockingQueueConsumer>() {
      @Override
      public BlockingQueueConsumer doInRabbit(Channel channel) throws Exception {

        BlockingQueueConsumer consumer = createConsumer(template);
        String tag = consumer.getConsumerTag();
        assertNotNull(tag);

        return consumer;

      }
    });

    template.convertAndSend("foo", "message");
    String result = getResult(consumer);
    assertEquals(null, result);

    template.convertAndSend("foo.end", "message");
    result = getResult(consumer);
    assertEquals("message", result);

    consumer.stop();
    cachingConnectionFactory.destroy();

  }
View Full Code Here

  private RabbitTemplate templateWithReturnsEnabled;

  @Before
  public void create() {
    connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    connectionFactory.setChannelCacheSize(1);
    connectionFactory.setPort(BrokerTestUtils.getPort());
    connectionFactoryWithConfirmsEnabled = new CachingConnectionFactory();
    connectionFactoryWithConfirmsEnabled.setHost("localhost");
    // When using publisher confirms, the cache size needs to be large enough
    // otherwise channels can be closed before confirms are received.
    connectionFactoryWithConfirmsEnabled.setChannelCacheSize(10);
    connectionFactoryWithConfirmsEnabled.setPort(BrokerTestUtils.getPort());
    connectionFactoryWithConfirmsEnabled.setPublisherConfirms(true);
    templateWithConfirmsEnabled = new RabbitTemplate(connectionFactoryWithConfirmsEnabled);
    connectionFactoryWithReturnsEnabled = new CachingConnectionFactory();
    connectionFactoryWithReturnsEnabled.setHost("localhost");
    connectionFactoryWithReturnsEnabled.setChannelCacheSize(1);
    connectionFactoryWithReturnsEnabled.setPort(BrokerTestUtils.getPort());
    connectionFactoryWithReturnsEnabled.setPublisherReturns(true);
    templateWithReturnsEnabled = new RabbitTemplate(connectionFactoryWithReturnsEnabled);
View Full Code Here

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

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setPublisherConfirms(true);
    final RabbitTemplate template = new RabbitTemplate(ccf);

    final AtomicBoolean confirmed = new AtomicBoolean();
    template.setConfirmCallback(new ConfirmCallback() {
View Full Code Here

    when(mockConnection.isOpen()).thenReturn(true);
    PublisherCallbackChannelImpl channel1 = new PublisherCallbackChannelImpl(mockChannel1);
    PublisherCallbackChannelImpl channel2 = new PublisherCallbackChannelImpl(mockChannel2);
    when(mockConnection.createChannel()).thenReturn(channel1).thenReturn(channel2);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setPublisherConfirms(true);
    ccf.setChannelCacheSize(3);
    final RabbitTemplate template = new RabbitTemplate(ccf);

    final AtomicBoolean confirmed = new AtomicBoolean();
    template.setConfirmCallback(new ConfirmCallback() {

      @Override
      public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        confirmed.set(true);
      }
    });

    // Hold up the first thread so we get two channels
    final CountDownLatch threadLatch = new CountDownLatch(1);
    final CountDownLatch threadSentLatch = new CountDownLatch(1);
    //Thread 1
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(new Runnable() {

      @Override
      public void run() {
        template.execute(new ChannelCallback<Object>() {
          @Override
          public Object doInRabbit(Channel channel) throws Exception {
            try {
              threadLatch.await(10, TimeUnit.SECONDS);
            }
            catch (InterruptedException e) {
              Thread.currentThread().interrupt();
            }
            template.doSend(channel, "", ROUTE,
              new SimpleMessageConverter().toMessage("message", new MessageProperties()),
              new CorrelationData("def"));
            threadSentLatch.countDown();
            return null;
          }
        });
      }
    });

    // Thread 2
    template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("abc")); // channel y
    threadLatch.countDown();
    assertTrue(threadSentLatch.await(5, TimeUnit.SECONDS));
    Collection<CorrelationData> unconfirmed = template.getUnconfirmed(-1);
    assertEquals(2, unconfirmed.size());
    Set<String> ids = new HashSet<String>();
    Iterator<CorrelationData> iterator = unconfirmed.iterator();
    ids.add(iterator.next().getId());
    ids.add(iterator.next().getId());
    assertTrue(ids.remove("abc"));
    assertTrue(ids.remove("def"));
    assertFalse(confirmed.get());
    DirectFieldAccessor dfa = new DirectFieldAccessor(template);
    Map<?, ?> pendingConfirms = (Map<?, ?>) dfa.getPropertyValue("pendingConfirms");
    assertThat(pendingConfirms.size(), greaterThan(0)); // might use 2 or only 1 channel
    exec.shutdown();
    assertTrue(exec.awaitTermination(10, TimeUnit.SECONDS));
    ccf.destroy();
    assertEquals(0, pendingConfirms.size());
  }
View Full Code Here

      @Override
      public Object answer(InvocationOnMock invocation) throws Throwable {
        return count.incrementAndGet();
      }}).when(mockChannel).getNextPublishSeqNo();

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setPublisherConfirms(true);
    final RabbitTemplate template = new RabbitTemplate(ccf);

    final AtomicBoolean confirmed = new AtomicBoolean();
    template.setConfirmCallback(new ConfirmCallback() {
View Full Code Here

TOP

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

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.