Package org.springframework.amqp.rabbit.connection

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


    reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass()));
  }

  @Test
  public void testKitchenSink() throws Exception {
    CachingConnectionFactory connectionFactory = beanFactory.getBean("kitchenSink", CachingConnectionFactory.class);
    assertNotNull(connectionFactory);
    assertEquals(10, connectionFactory.getChannelCacheSize());
    DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
    assertNull(dfa.getPropertyValue("executorService"));
    assertEquals(Boolean.TRUE, dfa.getPropertyValue("publisherConfirms"));
    assertEquals(Boolean.TRUE, dfa.getPropertyValue("publisherReturns"));
    assertEquals(123, TestUtils.getPropertyValue(connectionFactory, "rabbitConnectionFactory.requestedHeartbeat"));
    assertEquals(789,  TestUtils.getPropertyValue(connectionFactory, "rabbitConnectionFactory.connectionTimeout"));
    assertEquals(CachingConnectionFactory.CacheMode.CHANNEL, connectionFactory.getCacheMode());
  }
View Full Code Here


    assertEquals(CachingConnectionFactory.CacheMode.CHANNEL, connectionFactory.getCacheMode());
  }

  @Test
  public void testNative() throws Exception {
    CachingConnectionFactory connectionFactory = beanFactory.getBean("native", CachingConnectionFactory.class);
    assertNotNull(connectionFactory);
    assertEquals(10, connectionFactory.getChannelCacheSize());
  }
View Full Code Here

    assertEquals(10, connectionFactory.getChannelCacheSize());
  }

  @Test
  public void testWithExecutor() throws Exception {
    CachingConnectionFactory connectionFactory = beanFactory.getBean("withExecutor", CachingConnectionFactory.class);
    assertNotNull(connectionFactory);
    Object executor = new DirectFieldAccessor(connectionFactory).getPropertyValue("executorService");
    assertNotNull(executor);
    ThreadPoolTaskExecutor exec = beanFactory.getBean("exec", ThreadPoolTaskExecutor.class);
    assertSame(exec.getThreadPoolExecutor(), executor);
    DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
    assertEquals(Boolean.FALSE, dfa.getPropertyValue("publisherConfirms"));
    assertEquals(Boolean.FALSE, dfa.getPropertyValue("publisherReturns"));
    assertEquals(CachingConnectionFactory.CacheMode.CONNECTION, connectionFactory.getCacheMode());
    assertEquals(0,  TestUtils.getPropertyValue(connectionFactory, "rabbitConnectionFactory.connectionTimeout"));
    assertEquals(10, connectionFactory.getConnectionCachesize());
  }
View Full Code Here

    assertEquals(10, connectionFactory.getConnectionCachesize());
  }

  @Test
  public void testWithExecutorService() throws Exception {
    CachingConnectionFactory connectionFactory = beanFactory.getBean("withExecutorService", CachingConnectionFactory.class);
    assertNotNull(connectionFactory);
    assertEquals(10, connectionFactory.getChannelCacheSize());
    Object executor = new DirectFieldAccessor(connectionFactory).getPropertyValue("executorService");
    assertNotNull(executor);
    ExecutorService exec = beanFactory.getBean("execService", ExecutorService.class);
    assertSame(exec, executor);
  }
View Full Code Here

    assertSame(exec, executor);
  }

  @Test
  public void testMultiHost() throws Exception {
    CachingConnectionFactory connectionFactory = beanFactory.getBean("multiHost", CachingConnectionFactory.class);
    assertNotNull(connectionFactory);
    assertEquals(10, connectionFactory.getChannelCacheSize());
    DirectFieldAccessor dfa =  new DirectFieldAccessor(connectionFactory);
    Address[] addresses = (Address[]) dfa.getPropertyValue("addresses");
    assertEquals(3, addresses.length);
    assertEquals("host1", addresses[0].getHost());
    assertEquals(1234, addresses[0].getPort());
View Full Code Here

        "The acknowledgeMode is NONE (autoack in Rabbit terms) which is not consistent with having an "
            + "external transaction manager. Either use a different AcknowledgeMode or make sure " +
            "the transactionManager is null.");

    if (this.getConnectionFactory() instanceof CachingConnectionFactory) {
      CachingConnectionFactory cf = (CachingConnectionFactory) getConnectionFactory();
      if (cf.getCacheMode() == CacheMode.CHANNEL && cf.getChannelCacheSize() < this.concurrentConsumers) {
        cf.setChannelCacheSize(this.concurrentConsumers);
        logger.warn("CachingConnectionFactory's channelCacheSize can not be less than the number " +
            "of concurrentConsumers so it was reset to match: "  + this.concurrentConsumers);
      }
    }
View Full Code Here

    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    when(mockChannel.isOpen()).thenReturn(true);

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

    txTemplate.execute(new TransactionCallback<Object>() {
      @Override
      public Object doInTransaction(TransactionStatus status) {
View Full Code Here

  private RabbitTemplate template;

  @Before
  public void create() {
    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    connectionFactory.setPort(BrokerTestUtils.getPort());
    template = new RabbitTemplate(connectionFactory);
    template.setSendConnectionFactorySelectorExpression(new LiteralExpression("foo"));
  }
View Full Code Here

    assertEquals(null, result);
  }

  @Test
  public void testAtomicSendAndReceive() 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<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();
          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(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();
    assertEquals(null, reply);
    cachingConnectionFactory.destroy();
  }
View Full Code Here

    cachingConnectionFactory.destroy();
  }

  @Test
  public void testAtomicSendAndReceiveExternalExecutor() throws Exception {
    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
    final String execName = "make-sure-exec-passed-in";
    exec.setBeanName(execName);
    exec.afterPropertiesSet();
    connectionFactory.setExecutor(exec);
    final Field[] fields = new Field[1];
    ReflectionUtils.doWithFields(RabbitTemplate.class, new FieldCallback() {
      @Override
      public void doWith(Field field) throws IllegalArgumentException,
          IllegalAccessException {
        field.setAccessible(true);
        fields[0] = field;
      }
    }, new FieldFilter() {
      @Override
      public boolean matches(Field field) {
        return field.getName().equals("logger");
      }
    });
    Log logger = Mockito.mock(Log.class);
    when(logger.isTraceEnabled()).thenReturn(true);

    final AtomicBoolean execConfiguredOk = new AtomicBoolean();

    doAnswer(new Answer<Object>(){
      @Override
      public Object answer(InvocationOnMock invocation) throws Throwable {
        String log = (String) invocation.getArguments()[0];
        if (log.startsWith("Message received") &&
            Thread.currentThread().getName().startsWith(execName)) {
          execConfiguredOk.set(true);
        }
        return null;
      }
    }).when(logger).trace(Mockito.anyString());
    final RabbitTemplate template = new RabbitTemplate(connectionFactory);
    ReflectionUtils.setField(fields[0], template, logger);
    template.setRoutingKey(ROUTE);
    template.setQueue(ROUTE);
    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();
          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(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();
    assertEquals(null, reply);

    assertTrue(execConfiguredOk.get());
    connectionFactory.destroy();
  }
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.