com.rabbitmq.client.Address Java Examples

The following examples show how to use com.rabbitmq.client.Address. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectionFactoryInvocationTest.java    From lyra with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that invocation failures are rethrown when a retry policy is not set.
 */
public void shouldThrowOnInvocationFailureWithNoRetryPolicy() throws Throwable {
  config = new Config().withRetryPolicy(RetryPolicies.retryNever());
  connectionFactory = mock(ConnectionFactory.class);
  connection = mock(Connection.class);
  when(connectionFactory.newConnection(any(ExecutorService.class), any(Address[].class), anyString())).thenAnswer(
      failNTimes(3, new ConnectException("fail"), connection, connectionHandler));

  try {
    mockConnection();
    fail();
  } catch (Exception expected) {
  }

  verifyCxnCreations(1);
}
 
Example #2
Source File: ConnectionConfigurationTest.java    From rabbitmq-cdi with MIT License 6 votes vote down vote up
private void assertConnection(String username, String password, String virtualHost,
    boolean secure, List<Address> addresses) throws Exception {
  if (addresses == null) {
    configuration.addHost(expectedAddress);
  }
  configuration.createConnection(connectionFactory);

  ArgumentCaptor<SslContextFactory> sslFactoryCapture =
      ArgumentCaptor.forClass(SslContextFactory.class);

  verify(connectionFactory).setUsername(username == null ? "guest" : username);
  verify(connectionFactory).setPassword(password == null ? "guest" : password);
  if (secure) {
    verify(connectionFactory).setSslContextFactory(sslFactoryCapture.capture());
    assertEquals(SSLContext.getDefault(), sslFactoryCapture.getValue().create(null));
  }
  if (virtualHost != null) {
    verify(connectionFactory).setVirtualHost(virtualHost);
  }
  verify(connectionFactory)
      .newConnection(addresses == null ? asList(expectedAddress) : addresses);
}
 
Example #3
Source File: ConnectionOptions.java    From lyra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the addresses to attempt connections to, in round-robin order.
 * 
 * @see #withAddresses(Address...)
 * @see #withAddresses(String)
 * @see #withHost(String)
 * @see #withHosts(String...)
 */
public Address[] getAddresses() {
  if (addresses != null)
    return addresses;

  if (hosts != null) {
    addresses = new Address[hosts.length];
    for (int i = 0; i < hosts.length; i++)
      addresses[i] = new Address(hosts[i], factory.getPort());
    return addresses;
  }

  Address address = factory == null ? new Address("localhost", -1) : new Address(
      factory.getHost(), factory.getPort());
  return new Address[] { address };
}
 
Example #4
Source File: AMQPObservableQueue.java    From conductor with Apache License 2.0 6 votes vote down vote up
AMQPObservableQueue(final ConnectionFactory factory, final Address[] addresses, final boolean useExchange,
		final AMQPSettings settings, final int batchSize, final int pollTimeInMS) {
	if (factory == null) {
		throw new IllegalArgumentException("Connection factory is undefined");
	}
	if (addresses == null || addresses.length == 0) {
		throw new IllegalArgumentException("Addresses are undefined");
	}
	if (settings == null) {
		throw new IllegalArgumentException("Settings are undefined");
	}
	if (batchSize <= 0) {
		throw new IllegalArgumentException("Batch size must be greater than 0");
	}
	if (pollTimeInMS <= 0) {
		throw new IllegalArgumentException("Poll time must be greater than 0 ms");
	}
	this.factory = factory;
	this.addresses = addresses;
	this.useExchange = useExchange;
	this.settings = settings;
	this.batchSize = batchSize;
	this.setPollTimeInMS(pollTimeInMS);
}
 
Example #5
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#setConnectionUri(java.net.URI)}.
 */
@Test
public void testSetConnectionUri_secured_server_user_port() {
  assertSame(binderConfig,
      binderConfig.setConnectionUri(URI.create("Amqps://[email protected]:1234")));

  verify(config).setUsername("test");
  verify(config).setHosts(Collections.singleton(new Address("flamingo.rmq.cloudamqp.com", 1234)));
  verify(config).setSecure(true);
}
 
Example #6
Source File: ConnectionConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link ConnectionConfiguration#addHost(Address)}.
 */
@Test
public void testAddHost() throws Exception {
  Address hostAddress = new Address("host.somedomain", 5671);
  configuration.addHost(hostAddress);
  assertConnection(null, null, null, false, asList(hostAddress));
}
 
Example #7
Source File: ConnectionConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link ConnectionConfiguration#setHosts(Set)}.
 */
@Test
public void testSetHosts() throws Exception {
  Address host1 = new Address("somehost1.somedomain", 5672);
  Address host2 = new Address("somehost2.somedomain", 5672);
  Set<Address> hosts = new LinkedHashSet<>();
  hosts.add(host1);
  hosts.add(host2);
  configuration.setHosts(hosts);
  assertConnection(null, null, null, false, asList(host1, host2));
}
 
Example #8
Source File: ConnectionConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link ConnectionConfiguration#hashCode()}.
 */
@Test
public void testHashCode() {
  assertEquals(Arrays.asList().hashCode(), configuration.hashCode());
  configuration.addHost(expectedAddress);
  assertEquals(Arrays.asList(expectedAddress).hashCode(), configuration.hashCode());
  Address hostAddress = new Address("host.somedomain", 5671);
  configuration.addHost(hostAddress);
  assertEquals(Arrays.asList(expectedAddress, hostAddress).hashCode(), configuration.hashCode());
}
 
Example #9
Source File: ConnectionConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link ConnectionConfiguration#equals(Object)}.
 */
@Test
public void testEquals() {
  assertNotEquals(configuration, null);
  assertNotEquals(configuration, new Object());

  assertEquals(configuration, configuration);

  ConnectionConfiguration configuration1 = new ConnectionConfiguration();
  assertEquals(configuration, configuration1);
  assertEquals(configuration1, configuration);

  ConnectionConfiguration configuration2 = new ConnectionConfiguration();
  assertEquals(configuration, configuration2);

  assertEquals(configuration1, configuration2);

  assertEquals(configuration.hashCode(), configuration1.hashCode());
  assertEquals(configuration.hashCode(), configuration2.hashCode());
  assertEquals(configuration1.hashCode(), configuration2.hashCode());

  // remaining false cases
  assertNotEquals(configuration, connectionConfiguration(c -> c.addHost(new Address("host"))));
  assertNotEquals(configuration, connectionConfiguration(c -> c.setPassword("password")));
  assertNotEquals(configuration, connectionConfiguration(c -> c.setUsername("username")));
  assertNotEquals(configuration, connectionConfiguration(c -> c.setVirtualHost("virtualHost")));
  assertNotEquals(configuration, connectionConfiguration(c -> c.setSecure(true)));
}
 
Example #10
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#setHost(String)}.
 */
@SuppressWarnings({"deprecation", "javadoc"})
@Test
public void testSetHost() {
  assertSame(binderConfig, binderConfig.setHost("hostName"));

  verify(config).addHost(new Address("hostName"));
}
 
Example #11
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#addHost(String)}.
 */
@Test
public void testAddHostString() {
  assertSame(binderConfig, binderConfig.addHost("hostName:123"));

  verify(config).addHost(new Address("hostName", 123));
}
 
Example #12
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#addHost(com.rabbitmq.client.Address)}.
 */
@Test
public void testAddHostAddress() {
  Address host = new Address("hostName", 1234);

  assertSame(binderConfig, binderConfig.addHost(host));

  verify(config).addHost(host);
}
 
Example #13
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#setConnectionUri(java.net.URI)}.
 */
@Test
public void testSetConnectionUri_unkown_host() {
  assertSame(binderConfig, binderConfig.setConnectionUri(URI.create("amqp://?")));

  verify(config).setHosts(Collections.singleton(new Address("127.0.0.1", 5672)));
}
 
Example #14
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#setConnectionUri(java.net.URI)}.
 */
@Test
public void testSetConnectionUri_server_only() {
  assertSame(binderConfig,
      binderConfig.setConnectionUri(URI.create("Amqp://flamingo.rmq.cloudamqp.com/")));

  verify(config).setHosts(Collections.singleton(new Address("flamingo.rmq.cloudamqp.com", 5672)));
}
 
Example #15
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#setConnectionUri(java.net.URI)}.
 */
@Test
public void testSetConnectionUri_server_and_port() {
  assertSame(binderConfig,
      binderConfig.setConnectionUri(URI.create("amqp://flamingo.rmq.cloudamqp.com:1234")));

  verify(config).setHosts(Collections.singleton(new Address("flamingo.rmq.cloudamqp.com", 1234)));
}
 
Example #16
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#setConnectionUri(java.net.URI)}.
 */
@Test
public void testSetConnectionUri_secured_server_only() {
  assertSame(binderConfig,
      binderConfig.setConnectionUri(URI.create("amqps://flamingo.rmq.cloudamqp.com")));

  verify(config).setHosts(Collections.singleton(new Address("flamingo.rmq.cloudamqp.com", 5671)));
  verify(config).setSecure(true);
}
 
Example #17
Source File: ConnectionConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link ConnectionConfiguration#toString()}.
 */
@Test
public void testToString() {
  assertEquals("broker hosts: [], connect user: guest", configuration.toString());
  Set<Address> hosts = new LinkedHashSet<>();
  hosts.add(expectedAddress);
  hosts.add(new Address("somehost2.somedomain", 5672));
  configuration.setHosts(hosts);
  configuration.setUsername("username");
  assertEquals(
      "broker hosts: [somehost.somedomain:5672, somehost2.somedomain:5672], connect user: username",
      configuration.toString());
}
 
Example #18
Source File: BinderConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link BinderConfiguration#setConnectionUri(java.net.URI)}.
 */
@Test
public void testSetConnectionUri_secured() {
  assertSame(binderConfig, binderConfig
      .setConnectionUri(URI.create("amqps://user:[email protected]/nkjoriiy")));

  verify(config).setUsername("user");
  verify(config).setPassword("password");
  verify(config).setHosts(Collections.singleton(new Address("flamingo.rmq.cloudamqp.com", 5671)));
  verify(config).setSecure(true);
  verify(config).setVirtualHost("nkjoriiy");
}
 
Example #19
Source File: RabbitMQExamples.java    From vertx-rabbitmq-client with Apache License 2.0 5 votes vote down vote up
public void createClientWithMultipleHost(Vertx vertx) {
  RabbitMQOptions config = new RabbitMQOptions();
  config.setUser("user1");
  config.setPassword("password1");
  config.setVirtualHost("vhost1");

  config.setAddresses(Arrays.asList(Address.parseAddresses("firstHost,secondHost:5672")));

  RabbitMQClient client = RabbitMQClient.create(vertx, config);
}
 
Example #20
Source File: RabbitConnectionFactoryCreatorTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
private void assertConnectorPropertiesMatchHosts(ConnectionFactory connector, List<String> uriStrings) throws Exception {
	Address[] addresses = (Address[]) ReflectionTestUtils.getField(connector, "addresses");
	assertNotNull(addresses);
	assertEquals(uriStrings.size(), addresses.length);

	for (int i = 0; i < uriStrings.size(); i++) {
		URI uri = new URI(uriStrings.get(i));
		assertEquals(uri.getHost(), addresses[i].getHost());
		assertEquals(uri.getPort(), addresses[i].getPort());
	}
}
 
Example #21
Source File: Addresses.java    From lyra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an array of Addresses for the {@code hosts} and {@code port}.
 */
public static Address[] addressesFor(String[] hosts, int port) {
  Address[] hostAddresses = new Address[hosts.length];
  for (int i = 0; i < hosts.length; i++)
    hostAddresses[i] = new Address(hosts[i].trim(), port);
  return hostAddresses;
}
 
Example #22
Source File: AbstractFunctionalTest.java    From lyra with Apache License 2.0 5 votes vote down vote up
protected void mockConnection() throws IOException, TimeoutException {
  if (connectionFactory == null) {
    mockConnectionOnly();
    connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.getVirtualHost()).thenReturn("/");
    when(connectionFactory.newConnection(any(ExecutorService.class), any(Address[].class), anyString()))
        .thenReturn(connection);
  }

  if (options == null)
    options = new ConnectionOptions().withHost("test-host");
  options.withConnectionFactory(connectionFactory);
  if (config == null)
    config =
        new Config().withRetryPolicy(
            RetryPolicies.retryAlways().withInterval(Duration.millis(10))).withRecoveryPolicy(
            RecoveryPolicies.recoverAlways());

  if (connectionHandler == null) {
    connectionHandler = new ConnectionHandler(options, config, Connection.class.getClassLoader());
    connectionProxy =
        (ConfigurableConnection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
            new Class<?>[] {ConfigurableConnection.class}, connectionHandler);
    connectionHandler.createConnection(connectionProxy);
    channels = new HashMap<Integer, MockChannel>();
  }
}
 
Example #23
Source File: ConnectionFactoryInvocationTest.java    From lyra with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that a retryable connect failure results in the connection eventually succeeding.
 */
public void shouldHandleRetryableConnectFailure() throws Throwable {
  mockConnectionOnly();
  connectionFactory = mock(ConnectionFactory.class);
  when(connectionFactory.newConnection(any(ExecutorService.class), any(Address[].class), anyString())).thenAnswer(
      failNTimes(3, new ConnectException("fail"), connection, connectionHandler));
  mockConnection();
  verifyCxnCreations(4);
}
 
Example #24
Source File: ConnectionFactoryInvocationTest.java    From lyra with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that an non-retryable connect failure results in the connection being rethrown.
 */
public void shouldHandleNonRetryableConnectFailure() throws Throwable {
  connectionFactory = mock(ConnectionFactory.class);
  connection = mock(Connection.class);
  when(connectionFactory.newConnection(any(ExecutorService.class), any(Address[].class), anyString())).thenAnswer(
      failNTimes(3, new RuntimeException(), connection, connectionHandler));

  try {
    mockConnection();
    fail();
  } catch (Exception expected) {
  }

  verifyCxnCreations(1);
}
 
Example #25
Source File: ConfigAvailableHosts.java    From storm-rabbitmq with MIT License 5 votes vote down vote up
/**
 * 
 * @return The {@link Map} of RabbitMQ hosts, converted to the necessary
 *         {@link Address} array
 */
public Address[] toAddresses() {
    final Address[] addresses = new Address[hostsMap.size()];
    int i = 0;
    for (final Entry<String, Integer> entry : hostsMap.entrySet()) {
        if (entry.getKey() != null) {
            addresses[i++] = entry.getValue() == null ? new Address(entry.getKey()) : new Address(entry.getKey(), entry.getValue());
        }
    }
    return addresses;
}
 
Example #26
Source File: ConnectionConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Test
public void testSetRequestedConnectionHeartbeatTimeout() throws Exception {
  configuration.setRequestedConnectionHeartbeatTimeout(300);
  Address hostAddress = new Address("host.somedomain", 5671);
  configuration.addHost(hostAddress);
  configuration.createConnection(connectionFactory);
  verify(connectionFactory).setRequestedHeartbeat(300);
}
 
Example #27
Source File: ConnectionConfigurationTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Test
public void testSetConnectTimeout() throws Exception {
  configuration.setConnectTimeout(300);
  Address hostAddress = new Address("host.somedomain", 5671);
  configuration.addHost(hostAddress);
  configuration.createConnection(connectionFactory);
  verify(connectionFactory).setConnectionTimeout(300);
}
 
Example #28
Source File: MockConnectionFactoryTest.java    From rabbitmq-mock with Apache License 2.0 5 votes vote down vote up
@Test
void use_alternate_factory() throws IOException, TimeoutException {
    ConnectionFactory factory = new MockConnectionFactoryWithoutAddressResolver();

    Connection connection = factory.newConnection(null, (List<Address>) null, null);

    assertThat(connection).isInstanceOf(MockConnection.class);
}
 
Example #29
Source File: QueueClient.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void connect() throws IOException {
    if(config.getAddresses().isEmpty()) {
        logger.warning("Skipping AMQP connection because no addresses are configured");
    } else {
        logger.info("Connecting to AMQP API at " + Joiners.onCommaSpace.join(config.getAddresses()));
        this.connection = this.createConnectionFactory().newConnection(this.config.getAddresses().toArray(new Address[0]));
        this.channel = this.connection.createChannel();
    }
}
 
Example #30
Source File: AMQPObservableQueue.java    From conductor with Apache License 2.0 5 votes vote down vote up
private Address[] buildAddressesFromHosts() {
	// Read hosts from config
	final String hosts = config.getProperty(
			String.format(AMQPConstants.PROPERTY_KEY_TEMPLATE, AMQPConfigurations.PROPERTY_HOSTS),
			ConnectionFactory.DEFAULT_HOST);
	if (StringUtils.isEmpty(hosts)) {
		throw new IllegalArgumentException("Hosts are undefined");
	}
	return Address.parseAddresses(hosts);
}