Java Code Examples for org.springframework.amqp.rabbit.connection.CachingConnectionFactory

The following are top voted examples for showing how to use org.springframework.amqp.rabbit.connection.CachingConnectionFactory. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testDefaultRabbitConfiguration() {
	load(TestConfiguration.class);
	RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
	RabbitMessagingTemplate messagingTemplate = this.context
			.getBean(RabbitMessagingTemplate.class);
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
	RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class);
	assertThat(rabbitTemplate.getConnectionFactory()).isEqualTo(connectionFactory);
	assertThat(getMandatory(rabbitTemplate)).isFalse();
	assertThat(messagingTemplate.getRabbitTemplate()).isEqualTo(rabbitTemplate);
	assertThat(amqpAdmin).isNotNull();
	assertThat(connectionFactory.getHost()).isEqualTo("localhost");
	assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(false);
	assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(false);
	assertThat(this.context.containsBean("rabbitListenerContainerFactory"))
			.as("Listener container factory should be created by default").isTrue();
}
 
Example 2
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testConnectionFactoryWithOverrides() {
	load(TestConfiguration.class, "spring.rabbitmq.host:remote-server",
			"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
			"spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost",
			"spring.rabbitmq.connection-timeout:123");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getHost()).isEqualTo("remote-server");
	assertThat(connectionFactory.getPort()).isEqualTo(9000);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("/vhost");
	DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
	com.rabbitmq.client.ConnectionFactory rcf = (com.rabbitmq.client.ConnectionFactory) dfa
			.getPropertyValue("rabbitConnectionFactory");
	assertThat(rcf.getConnectionTimeout()).isEqualTo(123);
	assertThat((Address[]) dfa.getPropertyValue("addresses")).hasSize(1);

}
 
Example 3
Project: spring-cloud-stream-binder-rabbit   File: RabbitServiceAutoConfiguration.java   Source Code and License 6 votes vote down vote up
/**
 * Creates a {@link ConnectionFactory} using the singleton service connector.
 * @param cloud {@link Cloud} instance to be used for accessing services.
 * @param connectorConfigObjectProvider the {@link ObjectProvider} for the
 * {@link RabbitConnectionFactoryConfig}.
 * @return the {@link ConnectionFactory} used by the binder.
 */
@Bean
@Primary
ConnectionFactory rabbitConnectionFactory(Cloud cloud,
		ObjectProvider<RabbitConnectionFactoryConfig> connectorConfigObjectProvider,
		ConfigurableApplicationContext applicationContext,
		RabbitProperties rabbitProperties) throws Exception {

	ConnectionFactory connectionFactory = cloud.getSingletonServiceConnector(ConnectionFactory.class,
			connectorConfigObjectProvider.getIfUnique());

	configureCachingConnectionFactory((CachingConnectionFactory) connectionFactory,
			applicationContext, rabbitProperties);

	return connectionFactory;
}
 
Example 4
Project: spring-cloud-stream-binder-rabbit   File: RabbitMessageChannelBinder.java   Source Code and License 6 votes vote down vote up
private void checkConnectionFactoryIsErrorCapable() {
	if (!(this.connectionFactory instanceof CachingConnectionFactory)) {
		logger.warn("Unknown connection factory type, cannot determine error capabilities: "
				+ this.connectionFactory.getClass());
	}
	else {
		CachingConnectionFactory ccf = (CachingConnectionFactory) this.connectionFactory;
		if (!ccf.isPublisherConfirms() && !ccf.isPublisherReturns()) {
			logger.warn("Producer error channel is enabled, but the connection factory is not configured for "
					+ "returns or confirms; the error channel will receive no messages");
		}
		else if (!ccf.isPublisherConfirms()) {
			logger.info("Producer error channel is enabled, but the connection factory is only configured to "
					+ "handle returned messages; negative acks will not be reported");
		}
		else if (!ccf.isPublisherReturns()) {
			logger.info("Producer error channel is enabled, but the connection factory is only configured to "
					+ "handle negatively acked messages; returned messages will not be reported");
		}
	}
}
 
Example 5
Project: resthooks   File: AmqpConfig.java   Source Code and License 6 votes vote down vote up
@Bean
ConnectionFactory getAmqpConnectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
    LOGGER.info("Creating AMQP connection factory on host {}:{}", host, port);
    if (!username.isEmpty()) {
        connectionFactory.setUsername(username);
        LOGGER.info("Logging to AMQP with username ", username);
    } else {
        LOGGER.warn("Logging to AMQP with NO username");
    }
    if (!password.isEmpty()) {
        connectionFactory.setPassword(password);
        LOGGER.info("Logging to AMQP with password {}", password.replaceAll(".", "*"));
    } else {
        LOGGER.warn("Logging to AMQP with NO password");
    }
    return connectionFactory;
}
 
Example 6
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testDefaultRabbitConfiguration() {
	load(TestConfiguration.class);
	RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
	RabbitMessagingTemplate messagingTemplate = this.context
			.getBean(RabbitMessagingTemplate.class);
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
	RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class);
	assertThat(rabbitTemplate.getConnectionFactory()).isEqualTo(connectionFactory);
	assertThat(getMandatory(rabbitTemplate)).isFalse();
	assertThat(messagingTemplate.getRabbitTemplate()).isEqualTo(rabbitTemplate);
	assertThat(amqpAdmin).isNotNull();
	assertThat(connectionFactory.getHost()).isEqualTo("localhost");
	assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(false);
	assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(false);
	assertThat(this.context.containsBean("rabbitListenerContainerFactory"))
			.as("Listener container factory should be created by default").isTrue();
}
 
Example 7
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testConnectionFactoryWithOverrides() {
	load(TestConfiguration.class, "spring.rabbitmq.host:remote-server",
			"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
			"spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost",
			"spring.rabbitmq.connection-timeout:123");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getHost()).isEqualTo("remote-server");
	assertThat(connectionFactory.getPort()).isEqualTo(9000);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("/vhost");
	DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
	com.rabbitmq.client.ConnectionFactory rcf = (com.rabbitmq.client.ConnectionFactory) dfa
			.getPropertyValue("rabbitConnectionFactory");
	assertThat(rcf.getConnectionTimeout()).isEqualTo(123);
}
 
Example 8
Project: generic-vnfm   File: EMSConfiguration.java   Source Code and License 6 votes vote down vote up
@PostConstruct
private void init() {
  log.info("Initialization of RabbitConfiguration");

  emsConnectionFactory = new CachingConnectionFactory();
  ((CachingConnectionFactory) emsConnectionFactory).setHost(brokerIp);
  ((CachingConnectionFactory) emsConnectionFactory).setPort(rabbitPort);
  ((CachingConnectionFactory) emsConnectionFactory).setUsername(emsRabbitUsername);
  ((CachingConnectionFactory) emsConnectionFactory).setPassword(emsRabbitPassword);

  rabbitAdmin = new RabbitAdmin(emsConnectionFactory);
  TopicExchange topicExchange = new TopicExchange("openbaton-exchange");
  rabbitAdmin.declareExchange(topicExchange);
  log.info("exchange declared");

  queueName_emsRegistrator = "ems." + vnfmHelper.getVnfmEndpoint() + ".register";
  rabbitAdmin.declareQueue(new Queue(queueName_emsRegistrator, durable, exclusive, autodelete));
}
 
Example 9
Project: contestparser   File: RabbitAutoConfigurationTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testDefaultRabbitConfiguration() {
	load(TestConfiguration.class);
	RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
	RabbitMessagingTemplate messagingTemplate = this.context
			.getBean(RabbitMessagingTemplate.class);
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class);
	assertEquals(connectionFactory, rabbitTemplate.getConnectionFactory());
	assertEquals(rabbitTemplate, messagingTemplate.getRabbitTemplate());
	assertNotNull(amqpAdmin);
	assertEquals("localhost", connectionFactory.getHost());
	assertTrue("Listener container factory should be created by default",
			this.context.containsBean("rabbitListenerContainerFactory"));
}
 
Example 10
Project: hawkbit   File: AmqpTestConfiguration.java   Source Code and License 6 votes vote down vote up
@Bean
ConnectionFactory rabbitConnectionFactory(final RabbitMqSetupService rabbitmqSetupService) {
    final CachingConnectionFactory factory = new CachingConnectionFactory();
    factory.setHost(rabbitmqSetupService.getHostname());
    factory.setPort(5672);
    factory.setUsername(rabbitmqSetupService.getUsername());
    factory.setPassword(rabbitmqSetupService.getPassword());
    try {
        factory.setVirtualHost(rabbitmqSetupService.createVirtualHost());
        // All exception are catched. The BrokerRunning decide if the
        // test should break or not
    } catch (@SuppressWarnings("squid:S2221") final Exception e) {
        Throwables.propagateIfInstanceOf(e, AlivenessException.class);
        LOG.error("Cannot create virtual host {}", e.getMessage());
    }
    return factory;
}
 
Example 11
Project: cf-service-tester   File: StandaloneConfig.java   Source Code and License 6 votes vote down vote up
@Bean
public ConnectionFactory rabbitConnectionFactory() {
	com.rabbitmq.client.ConnectionFactory cf = new com.rabbitmq.client.ConnectionFactory();
	cf.setNetworkRecoveryInterval(rabbitNetworkRecoveryInterval);
	cf.setRequestedHeartbeat(rabbitHeartbeatInterval);
	
	CachingConnectionFactory connectionFactory = new CachingConnectionFactory(cf);
	connectionFactory.setVirtualHost(rabbitVirtualHost);
	connectionFactory.setAddresses(rabbitAddresses);
	connectionFactory.setUsername(rabbitUsername);
    connectionFactory.setPassword(rabbitPassword);
    connectionFactory.setPublisherConfirms(publisherConfirms);
    connectionFactory.setConnectionTimeout(rabbitConnectionTimeout);
    connectionFactory.setChannelCheckoutTimeout(rabbitChannelCheckoutTimeout);
    connectionFactory.setCloseTimeout(rabbitCloseTimeout);
    return connectionFactory;
}
 
Example 12
Project: elpaaso-core   File: DatasourceCloudContext.java   Source Code and License 6 votes vote down vote up
@Bean(name="rabbitMQConnectionFactory")
public ConnectionFactory rabbitConnectionFactory() {
	CloudFactory cloudFactory = new CloudFactory();
	Cloud cloud = cloudFactory.getCloud();

	AmqpServiceInfo amqpServiceInfo = (AmqpServiceInfo) cloud.getServiceInfo("activationAmqpBroker");

	String serviceID = amqpServiceInfo.getId();
	ConnectionFactory connectionFactory = cloud.getServiceConnector(serviceID, ConnectionFactory.class, null);
	try{
		LOGGER.info("Setting CachingConnectionFactory specific properties");
		((CachingConnectionFactory)connectionFactory).setChannelCacheSize(channelCacheSize);
		((CachingConnectionFactory)connectionFactory).setRequestedHeartBeat(requestedHeartbeat);
		((CachingConnectionFactory)connectionFactory).setConnectionTimeout(connectionTimeout);
	}   catch (ClassCastException cce){
		throw new TechnicalException("Cannot customize CachingConnectionFactory for rabbitConnectionFactory");
	}
	return connectionFactory;
}
 
Example 13
Project: cukes   File: RabbitMQConfiguration.java   Source Code and License 6 votes vote down vote up
@Bean
@DependsOn("amqpBroker")
CachingConnectionFactory connectionFactory() {
    log.info("Creating connection factory for MQ broker...");
    ConnectionFactory cf = new ConnectionFactory();
    cf.setHost(hostname);
    cf.setPort(port);
    cf.setUsername(userName);
    cf.setPassword(password);
    cf.setVirtualHost(virtualHost);
    cf.setAutomaticRecoveryEnabled(false);
    if (useSSL) {
        try {
            cf.useSslProtocol();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            //TODO don't throw raw exceptions
            throw new RuntimeException(e);
        }
    }

    log.info("Connection factory created.");
    return new CachingConnectionFactory(cf);
}
 
Example 14
Project: async-amqp-messaging   File: ReceiveController.java   Source Code and License 6 votes vote down vote up
/**
 * Creates a new connection to an AMQP broker with the given information.
 */
private void connectToBroker()
{
  if (this.objConnection == null)
  {
    this.objConnection = new CachingConnectionFactory();
    String host = this.objResources.getConfiguration().getHost();
    String addresses = "";
    for (int port : this.objResources.getConfiguration().getPorts())
    {
      addresses = addresses + host + ":" + port + ",";
    }
    this.objConnection.setAddresses(addresses);
    this.objConnection.setUsername(this.objResources.getConfiguration().getUsername());
    this.objConnection.setPassword(this.objResources.getConfiguration().getPassword());
    this.objConnection.setChannelCacheSize(25);
  }
}
 
Example 15
Project: async-amqp-messaging   File: SendController.java   Source Code and License 6 votes vote down vote up
/**
 * Creates a new connection to an AMQP broker with the given information.
 */
private void connectToBroker()
{
  if (this.objConnection == null)
  {
    this.objConnection = new CachingConnectionFactory();

    Configuration config = this.objResources.getConfiguration();
    String host = config.getHost();
    String addresses = "";
    for (int port : config.getPorts())
    {
      addresses = addresses + host + ":" + port + ", ";
    }
    this.objConnection.setAddresses(addresses);
    this.objConnection.setUsername(config.getUsername());
    this.objConnection.setPassword(config.getPassword());
    this.objConnection.setChannelCacheSize(25);
  }
}
 
Example 16
Project: bulbs-core   File: NotificationConfig.java   Source Code and License 6 votes vote down vote up
@Bean
public ConnectionFactory rabbitConnectionFactory(
        @Value("${rabbitMq.connection.host}") String host,
        @Value("${rabbitMq.connection.port}") Integer port,
        @Value("${rabbitMq.connection.username:}") String username,
        @Value("${rabbitMq.connection.password:}") String password
) {
    CachingConnectionFactory connectionFactory =
        new CachingConnectionFactory(host, port);
    connectionFactory.setUsername(username);
    connectionFactory.setPassword(password);
    connectionFactory.setChannelCacheSize(1);
    List<ChannelListener> listeners = new LinkedList<>();
    listeners.add(
            (channel, transactional) -> log.info("Channel created; Number:  " + channel.getChannelNumber())
    );
    connectionFactory.setChannelListeners(listeners);
    this.connectionFactory = connectionFactory;
    return connectionFactory;
}
 
Example 17
Project: spring-cloud-connectors   File: RabbitConnectionFactoryCreator.java   Source Code and License 6 votes vote down vote up
private CachingConnectionFactory createSpringConnectionFactory(AmqpServiceInfo serviceInfo,
															   ServiceConnectorConfig serviceConnectorConfiguration,
															   com.rabbitmq.client.ConnectionFactory connectionFactory) {
	CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory);

	if (serviceInfo.getUris() != null) {
		cachingConnectionFactory.setAddresses(getAddresses(serviceInfo));
	}

	if (serviceConnectorConfiguration != null) {
		Integer channelCacheSize = ((RabbitConnectionFactoryConfig) serviceConnectorConfiguration).getChannelCacheSize();
		if (channelCacheSize != null) {
			cachingConnectionFactory.setChannelCacheSize(channelCacheSize);
		}
	}

	return cachingConnectionFactory;
}
 
Example 18
Project: cloud-pos-router   File: RabbitConfiguration.java   Source Code and License 6 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory() {
    final URI ampqUrl;
    try {
        ampqUrl = new URI(getEnvOrThrow("CLOUDAMQP_URL"));
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    final CachingConnectionFactory factory = new CachingConnectionFactory();
    factory.setUsername(ampqUrl.getUserInfo().split(":")[0]);
    factory.setPassword(ampqUrl.getUserInfo().split(":")[1]);
    factory.setHost(ampqUrl.getHost());
    factory.setPort(ampqUrl.getPort());
    factory.setVirtualHost(ampqUrl.getPath().substring(1));
    factory.setPublisherReturns(true);

    return factory;
}
 
Example 19
Project: microservices-spring   File: RabbitMqConfiguration.java   Source Code and License 5 votes vote down vote up
@Bean
ConnectionFactory connectionFactory() {
    CachingConnectionFactory factory = new CachingConnectionFactory();
    factory.setUsername(username);
    factory.setPassword(password);
    factory.setAddresses(addresses);
    return factory;
}
 
Example 20
Project: microservices-spring   File: RabbitMqConfiguration.java   Source Code and License 5 votes vote down vote up
@Bean
ConnectionFactory connectionFactory() {
    CachingConnectionFactory factory = new CachingConnectionFactory();
    factory.setUsername(username);
    factory.setPassword(password);
    factory.setAddresses(addresses);
    return factory;
}
 
Example 21
Project: microservices-spring   File: RabbitMqConfiguration.java   Source Code and License 5 votes vote down vote up
@Bean
ConnectionFactory connectionFactory() {
    CachingConnectionFactory factory = new CachingConnectionFactory();
    factory.setUsername(username);
    factory.setPassword(password);
    factory.setAddresses(addresses);
    return factory;
}
 
Example 22
Project: springboot-sample   File: RabbitmqConfiguration.java   Source Code and License 5 votes vote down vote up
@Bean(name = "connectionFactory")
public ConnectionFactory getConnectionFactory() {
    CachingConnectionFactory factory = new CachingConnectionFactory();
    factory.setHost(rabbitMQProperties.getHost());
    factory.setUsername(rabbitMQProperties.getUsername());
    factory.setPassword(rabbitMQProperties.getPassword());
    factory.setVirtualHost(rabbitMQProperties.getVhost());
    factory.setPublisherConfirms(true);
    factory.setPublisherReturns(true);
    return factory;
}
 
Example 23
Project: java-learn   File: SpringClientDemo.java   Source Code and License 5 votes vote down vote up
public CachingConnectionFactory cachingConnectionFactory(){
    com.rabbitmq.client.ConnectionFactory connectionFactory = new com.rabbitmq.client.ConnectionFactory();
    connectionFactory.setHost("dev");
    connectionFactory.setPort(5672);
    connectionFactory.setUsername("admin");
    connectionFactory.setPassword("admin");
    connectionFactory.setVirtualHost("/learn");
    return new CachingConnectionFactory(connectionFactory);
}
 
Example 24
Project: dataProvider   File: SingletonConnectionFactory.java   Source Code and License 5 votes vote down vote up
public static CachingConnectionFactory getConnection(String host, String username, String password){

        if (connectionFactory == null) {
            createFactory(host, username, password);
        }

        return connectionFactory;
    }
 
Example 25
Project: spring-integration-dsl-samle   File: AbstractNotificationFlowConfiguration.java   Source Code and License 5 votes vote down vote up
@Bean
public CachingConnectionFactory rabbitMQConnection(){
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("127.0.0.1");
    connectionFactory.setPort(5672);
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    return connectionFactory;
}
 
Example 26
Project: spring-integration-dsl-samle   File: AbstractOrderFlowConfiguration.java   Source Code and License 5 votes vote down vote up
@Bean
public CachingConnectionFactory rabbitMQConnection(){
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("127.0.0.1");
    connectionFactory.setPort(5672);
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    return connectionFactory;
}
 
Example 27
Project: spring-integration-dsl-samle   File: AbstractWarehouseFlowConfiguration.java   Source Code and License 5 votes vote down vote up
@Bean
public CachingConnectionFactory rabbitMQConnection(){
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("127.0.0.1");
    connectionFactory.setPort(5672);
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    return connectionFactory;
}
 
Example 28
Project: acme-solution   File: CommandBusConfigurer.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory commandBusConnectionFactory() {

    log.info("Creating command bus connection on hostname: {" + this.hostname + "}");

    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.hostname, this.port);
    connectionFactory.setUsername(this.username);
    connectionFactory.setPassword(this.password);

    return connectionFactory;
}
 
Example 29
Project: acme-solution   File: EventBusConfigurer.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory eventBusConnectionFactory() {

    log.info("Creating query event bus connection on hostname: {" + this.hostname + "}");

    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.hostname, this.port);
    connectionFactory.setUsername(this.username);
    connectionFactory.setPassword(this.password);

    return connectionFactory;
}
 
Example 30
Project: acme-solution   File: RabbitConfigurer.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory getConnectionFactory() {

    log.info("Creating rabbit connection for command bus, publishing, on host: {" + this.hostname + "}");

    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.hostname, this.port);
    connectionFactory.setUsername(this.username);
    connectionFactory.setPassword(this.password);

    return connectionFactory;
}
 
Example 31
Project: acme-solution   File: EventBusConfigurer.java   Source Code and License 5 votes vote down vote up
@Bean
@Primary
public ConnectionFactory eventBusConnectionFactory() {

    log.info("Creating event bus connection on hostname: {" + this.hostname + "}");

    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.hostname, this.port);
    connectionFactory.setUsername(this.username);
    connectionFactory.setPassword(this.password);

    return connectionFactory;
}
 
Example 32
Project: spring-rabbitmq-actuator   File: QueueCheckTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testConstructor()
{
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);

    Queue queue = new Queue("test");
    queue.setAdminsThatShouldDeclare(rabbitAdmin);

    QueueCheck queueCheck = new QueueCheck(queue, 10000, 2);
    Assert.assertEquals(10000, queueCheck.getMaxMessageCount());
    Assert.assertEquals(2, queueCheck.getMinConsumerCount());
    Assert.assertNotNull(queueCheck.getQueue());
    Assert.assertNotNull(queueCheck.getRabbitAdmin());
}
 
Example 33
Project: spring-rabbitmq-actuator   File: QueueUtil.java   Source Code and License 5 votes vote down vote up
public static Queue generateQueue(String name)
{
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);

    Queue queue = new Queue(name);
    queue.setAdminsThatShouldDeclare(rabbitAdmin);

    return queue;
}
 
Example 34
Project: Microservices-Building-Scalable-Software   File: TurbineApp.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory() {
    //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost);

    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost);
    return cachingConnectionFactory;
}
 
Example 35
Project: Microservices-Building-Scalable-Software   File: TurbineApp.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory() {
    //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost);

    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost);
    return cachingConnectionFactory;
}
 
Example 36
Project: Microservices-Building-Scalable-Software   File: TurbineApp.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory() {
    //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost);

    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost);
    return cachingConnectionFactory;
}
 
Example 37
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryEmptyVirtualHost() {
	load(TestConfiguration.class, "spring.rabbitmq.virtual_host:");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("/");
}
 
Example 38
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryVirtualHostNoLeadingSlash() {
	load(TestConfiguration.class, "spring.rabbitmq.virtual_host:foo");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("foo");
}
 
Example 39
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryVirtualHostMultiLeadingSlashes() {
	load(TestConfiguration.class, "spring.rabbitmq.virtual_host:///foo");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("///foo");
}
 
Example 40
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryDefaultVirtualHost() {
	load(TestConfiguration.class, "spring.rabbitmq.virtual_host:/");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("/");
}
 
Example 41
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryPublisherSettings() {
	load(TestConfiguration.class, "spring.rabbitmq.publisher-confirms=true",
			"spring.rabbitmq.publisher-returns=true");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
	DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
	assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(true);
	assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(true);
	assertThat(getMandatory(rabbitTemplate)).isTrue();
}
 
Example 42
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryBackOff() {
	load(TestConfiguration2.class);
	RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory).isEqualTo(rabbitTemplate.getConnectionFactory());
	assertThat(connectionFactory.getHost()).isEqualTo("otherserver");
	assertThat(connectionFactory.getPort()).isEqualTo(8001);
}
 
Example 43
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryCacheSettings() {
	load(TestConfiguration.class, "spring.rabbitmq.cache.channel.size=23",
			"spring.rabbitmq.cache.channel.checkoutTimeout=1000",
			"spring.rabbitmq.cache.connection.mode=CONNECTION",
			"spring.rabbitmq.cache.connection.size=2");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
	assertThat(dfa.getPropertyValue("channelCacheSize")).isEqualTo(23);
	assertThat(dfa.getPropertyValue("cacheMode")).isEqualTo(CacheMode.CONNECTION);
	assertThat(dfa.getPropertyValue("connectionCacheSize")).isEqualTo(2);
	assertThat(dfa.getPropertyValue("channelCheckoutTimeout")).isEqualTo(1000L);
}
 
Example 44
Project: spring-cloud-stream-binder-rabbit   File: RabbitTestSupport.java   Source Code and License 5 votes vote down vote up
@Override
protected void obtainResource() throws Exception {
	resource = new CachingConnectionFactory("localhost");
	resource.createConnection().close();
	if (management) {
		Socket socket = SocketFactory.getDefault().createSocket("localhost", 15672);
		socket.close();
	}
}
 
Example 45
Project: spring-cloud-stream-binder-rabbit   File: RabbitServiceAutoConfiguration.java   Source Code and License 5 votes vote down vote up
static void configureCachingConnectionFactory(CachingConnectionFactory connectionFactory,
		ConfigurableApplicationContext applicationContext, RabbitProperties rabbitProperties) throws Exception {

	if (StringUtils.hasText(rabbitProperties.getAddresses())) {
		connectionFactory.setAddresses(rabbitProperties.determineAddresses());
	}

	connectionFactory.setPublisherConfirms(rabbitProperties.isPublisherConfirms());
	connectionFactory.setPublisherReturns(rabbitProperties.isPublisherReturns());
	if (rabbitProperties.getCache().getChannel().getSize() != null) {
		connectionFactory.setChannelCacheSize(rabbitProperties.getCache().getChannel().getSize());
	}
	if (rabbitProperties.getCache().getConnection().getMode() != null) {
		connectionFactory.setCacheMode(rabbitProperties.getCache().getConnection().getMode());
	}
	if (rabbitProperties.getCache().getConnection().getSize() != null) {
		connectionFactory.setConnectionCacheSize(
				rabbitProperties.getCache().getConnection().getSize());
	}
	if (rabbitProperties.getCache().getChannel().getCheckoutTimeout() != null) {
		connectionFactory.setChannelCheckoutTimeout(
				rabbitProperties.getCache().getChannel().getCheckoutTimeout());
	}
	connectionFactory.setApplicationContext(applicationContext);
	applicationContext.addApplicationListener(connectionFactory);
	connectionFactory.afterPropertiesSet();
}
 
Example 46
Project: spring-cloud-stream-binder-rabbit   File: LocalizedQueueConnectionFactoryIntegrationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnect() {
	RabbitAdmin admin = new RabbitAdmin(this.lqcf);
	Queue queue = new Queue(UUID.randomUUID().toString(), false, false, true);
	admin.declareQueue(queue);
	ConnectionFactory targetConnectionFactory = this.lqcf.getTargetConnectionFactory("[" + queue.getName() + "]");
	RabbitTemplate template = new RabbitTemplate(targetConnectionFactory);
	template.convertAndSend("", queue.getName(), "foo");
	assertThat(template.receiveAndConvert(queue.getName())).isEqualTo("foo");
	((CachingConnectionFactory) targetConnectionFactory).destroy();
	admin.deleteQueue(queue.getName());
}
 
Example 47
Project: spring-cloud-stream-binder-rabbit   File: RabbitBinderModuleTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testParentConnectionFactoryInheritedByDefault() {
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE)
			.run("--server.port=0");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	assertThat(binderConnectionFactory).isInstanceOf(CachingConnectionFactory.class);
	ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
	assertThat(binderConnectionFactory).isSameAs(connectionFactory);

	ConnectionFactory producerConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("producerConnectionFactory");
	assertThat(producerConnectionFactory).isNotSameAs(connectionFactory);

	CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator",
			CompositeHealthIndicator.class);
	DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator);
	assertThat(bindersHealthIndicator).isNotNull();
	@SuppressWarnings("unchecked")
	Map<String, HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor
			.getPropertyValue("indicators");
	assertThat(healthIndicators).containsKey(("rabbit"));
	assertThat(healthIndicators.get("rabbit").health().getStatus()).isEqualTo((Status.UP));
}
 
Example 48
Project: spring-cloud-stream-binder-rabbit   File: RabbitBinderModuleTests.java   Source Code and License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testParentConnectionFactoryInheritedByDefaultAndRabbitSettingsPropagated() {
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE)
			.run("--server.port=0",
					"--spring.cloud.stream.rabbit.bindings.input.consumer.transacted=true",
					"--spring.cloud.stream.rabbit.bindings.output.producer.transacted=true");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	BindingService bindingService = context.getBean(BindingService.class);
	DirectFieldAccessor channelBindingServiceAccessor = new DirectFieldAccessor(bindingService);
	Map<String, List<Binding<MessageChannel>>> consumerBindings = (Map<String, List<Binding<MessageChannel>>>) channelBindingServiceAccessor
			.getPropertyValue("consumerBindings");
	Binding<MessageChannel> inputBinding = consumerBindings.get("input").get(0);
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(inputBinding,
			"lifecycle.messageListenerContainer", SimpleMessageListenerContainer.class);
	assertThat(TestUtils.getPropertyValue(container, "transactional", Boolean.class)).isTrue();
	Map<String, Binding<MessageChannel>> producerBindings = (Map<String, Binding<MessageChannel>>) TestUtils
			.getPropertyValue(bindingService, "producerBindings");
	Binding<MessageChannel> outputBinding = producerBindings.get("output");
	assertThat(TestUtils.getPropertyValue(outputBinding, "lifecycle.amqpTemplate.transactional",
			Boolean.class)).isTrue();
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	assertThat(binderConnectionFactory).isInstanceOf(CachingConnectionFactory.class);
	ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
	assertThat(binderConnectionFactory).isSameAs(connectionFactory);
	CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator",
			CompositeHealthIndicator.class);
	DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator);
	assertThat(bindersHealthIndicator).isNotNull();
	Map<String, HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor
			.getPropertyValue("indicators");
	assertThat(healthIndicators).containsKey("rabbit");
	assertThat(healthIndicators.get("rabbit").health().getStatus()).isEqualTo(Status.UP);
}
 
Example 49
Project: Mastering-Microservices-with-Java   File: TurbineApp.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory() {
    //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost);

    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost);
    return cachingConnectionFactory;
}
 
Example 50
Project: Mastering-Microservices-with-Java   File: TurbineApp.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory() {
    //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost);

    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost);
    return cachingConnectionFactory;
}
 
Example 51
Project: Mastering-Microservices-with-Java   File: TurbineApp.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory() {
    //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost);

    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost);
    return cachingConnectionFactory;
}
 
Example 52
Project: av-service   File: AmqpBridgeOutputConfig.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory outConnectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
    connectionFactory.setUsername(userName);
    connectionFactory.setPassword(userPassword);
    connectionFactory.setVirtualHost(virtualHost);

    return connectionFactory;
}
 
Example 53
Project: av-service   File: AmqpCommonServerConfig.java   Source Code and License 5 votes vote down vote up
@Bean
public ConnectionFactory serverConnectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
    connectionFactory.setUsername(userName);
    connectionFactory.setPassword(userPassword);
    connectionFactory.setVirtualHost(virtualHost);

    return connectionFactory;
}
 
Example 54
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryEmptyVirtualHost() {
	load(TestConfiguration.class, "spring.rabbitmq.virtual_host:");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("/");
}
 
Example 55
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryVirtualHostNoLeadingSlash() {
	load(TestConfiguration.class, "spring.rabbitmq.virtual_host:foo");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("foo");
}
 
Example 56
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryVirtualHostMultiLeadingSlashes() {
	load(TestConfiguration.class, "spring.rabbitmq.virtual_host:///foo");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("///foo");
}
 
Example 57
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryDefaultVirtualHost() {
	load(TestConfiguration.class, "spring.rabbitmq.virtual_host:/");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory.getVirtualHost()).isEqualTo("/");
}
 
Example 58
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryPublisherSettings() {
	load(TestConfiguration.class, "spring.rabbitmq.publisher-confirms=true",
			"spring.rabbitmq.publisher-returns=true");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
	DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
	assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(true);
	assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(true);
	assertThat(getMandatory(rabbitTemplate)).isTrue();
}
 
Example 59
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryBackOff() {
	load(TestConfiguration2.class);
	RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	assertThat(connectionFactory).isEqualTo(rabbitTemplate.getConnectionFactory());
	assertThat(connectionFactory.getHost()).isEqualTo("otherserver");
	assertThat(connectionFactory.getPort()).isEqualTo(8001);
}
 
Example 60
Project: spring-boot-concourse   File: RabbitAutoConfigurationTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testConnectionFactoryCacheSettings() {
	load(TestConfiguration.class, "spring.rabbitmq.cache.channel.size=23",
			"spring.rabbitmq.cache.channel.checkoutTimeout=1000",
			"spring.rabbitmq.cache.connection.mode=CONNECTION",
			"spring.rabbitmq.cache.connection.size=2");
	CachingConnectionFactory connectionFactory = this.context
			.getBean(CachingConnectionFactory.class);
	DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
	assertThat(dfa.getPropertyValue("channelCacheSize")).isEqualTo(23);
	assertThat(dfa.getPropertyValue("cacheMode")).isEqualTo(CacheMode.CONNECTION);
	assertThat(dfa.getPropertyValue("connectionCacheSize")).isEqualTo(2);
	assertThat(dfa.getPropertyValue("channelCheckoutTimeout")).isEqualTo(1000L);
}