Example usage for org.springframework.amqp.rabbit.core RabbitTemplate getConnectionFactory

List of usage examples for org.springframework.amqp.rabbit.core RabbitTemplate getConnectionFactory

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.core RabbitTemplate getConnectionFactory.

Prototype

public ConnectionFactory getConnectionFactory() 

Source Link

Usage

From source file:acromusashi.stream.component.rabbitmq.AmqpTemplateFactoryTest.java

@Theory
public void testGetAmqpTemplate_NormalCase(Fixture fixture) throws RabbitmqCommunicateException {
    // ???// ww w  .j  a  va 2  s . c om
    Assume.assumeTrue(fixture.expectedException == null);

    // 
    String queueName = fixture.queueName;
    RabbitTemplate expected = fixture.expectedTemplate;

    // Mock
    AbstractContextBuilder mockContextBuilder = mock(AbstractContextBuilder.class);
    doReturn(Arrays.asList("localhost:5672", "172.0.0.1:5673")).when(mockContextBuilder)
            .getProcessList(DEFINED_QUEUE_NAME);
    doReturn(expected.getConnectionFactory()).when(mockContextBuilder).getConnectionFactory(DEFINED_QUEUE_NAME);
    doReturn(expected).when(mockContextBuilder).getAmqpTemplate(DEFINED_QUEUE_NAME);

    this.factory = new AmqpTemplateFactory(mockContextBuilder);

    // 
    RabbitTemplate firstActual = (RabbitTemplate) this.factory.getAmqpTemplate(queueName);
    RabbitTemplate secondActual = (RabbitTemplate) this.factory.getAmqpTemplate(queueName);

    // 
    // AmqpTemplateConnectionFactory???equals?????????
    // ?????ContextBuilder?????????
    verify(mockContextBuilder, times(1)).getAmqpTemplate(DEFINED_QUEUE_NAME);

    // ConnectionFactory????????????()??
    // ???????equals????????
    assertThat(((CachingConnectionFactory) firstActual.getConnectionFactory()).getChannelCacheSize(),
            is(((CachingConnectionFactory) expected.getConnectionFactory()).getChannelCacheSize()));

    // ???????(?)?????
    assertTrue(firstActual == secondActual);
}

From source file:org.springframework.integration.amqp.config.AmqpOutboundChannelAdapterParserTests.java

@Test
public void testInt3430FailForNotLazyConnect() {
    RabbitTemplate amqpTemplate = spy(new RabbitTemplate());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    RuntimeException toBeThrown = new RuntimeException("Test Connection Exception");
    doThrow(toBeThrown).when(connectionFactory).createConnection();
    when(amqpTemplate.getConnectionFactory()).thenReturn(connectionFactory);
    AmqpOutboundEndpoint handler = new AmqpOutboundEndpoint(amqpTemplate);
    Log logger = spy(TestUtils.getPropertyValue(handler, "logger", Log.class));
    new DirectFieldAccessor(handler).setPropertyValue("logger", logger);
    doAnswer(new DoesNothing()).when(logger).error("Failed to eagerly establish the connection.", toBeThrown);
    ApplicationContext context = mock(ApplicationContext.class);
    handler.setApplicationContext(context);
    handler.setBeanFactory(context);/*w w  w .  j a  v a  2  s.co  m*/
    handler.afterPropertiesSet();
    handler.start();
    handler.stop();
    verify(logger, never()).error(anyString(), any(RuntimeException.class));
    handler.setLazyConnect(false);
    handler.start();
    verify(logger).error("Failed to eagerly establish the connection.", toBeThrown);
    handler.stop();
}