Example usage for com.rabbitmq.client Address Address

List of usage examples for com.rabbitmq.client Address Address

Introduction

In this page you can find the example usage for com.rabbitmq.client Address Address.

Prototype

public Address(String host, int port) 

Source Link

Document

Construct an address from a host name and port number.

Usage

From source file:org.mule.transport.amqp.AmqpConnector.java

License:Open Source License

@Override
public void doConnect() throws Exception {
    final List<Address> brokerAddresses = new ArrayList<Address>();
    brokerAddresses.add(new Address(host, port));
    addFallbackAddresses(brokerAddresses);
    connection = connectionFactory.newConnection(brokerAddresses.toArray(new Address[0]));

    configureDefaultReturnListener();//w w w.j  av  a  2  s  .  com
    // clear any connector connections that could have been created in a previous connect() operation
    connectorConnectionPool.clear();
}

From source file:org.mule.transport.amqp.AmqpConnector.java

License:Open Source License

private void addFallbackAddresses(final List<Address> brokerAddresses) {
    if (fallbackAddresses == null)
        return;//  w ww . j a v a 2 s  .c o  m

    for (final String fallbackAddress : fallbackAddresses) {
        final String[] fallbackAddressElements = StringUtils.splitAndTrim(fallbackAddress, ":");

        if (fallbackAddressElements.length == 2) {
            brokerAddresses.add(
                    new Address(fallbackAddressElements[0], NumberUtils.toInt(fallbackAddressElements[1])));
        } else if (fallbackAddressElements.length == 1) {
            brokerAddresses.add(new Address(fallbackAddressElements[0]));
        } else {
            logger.warn("Ignoring unparseable fallback address: " + fallbackAddress);
        }
    }
}

From source file:org.mule.transport.amqp.internal.connector.AmqpConnector.java

License:Open Source License

@Override
public void doConnect() throws Exception {
    final List<Address> brokerAddresses = new ArrayList<Address>();
    brokerAddresses.add(new Address(host, port));
    addFallbackAddresses(brokerAddresses);

    connectToFirstResponsiveBroker(brokerAddresses);
    configureDefaultReturnListener();//from  ww  w  .ja  v  a  2  s. c  om
}

From source file:org.mule.transport.amqp.internal.connector.AmqpConnector.java

License:Open Source License

protected void addFallbackAddresses(final List<Address> brokerAddresses) {
    if (fallbackAddresses == null)
        return;//from  www. j ava 2 s  .c om

    for (final String fallbackAddress : fallbackAddresses) {
        final String[] fallbackAddressElements = StringUtils.splitAndTrim(fallbackAddress, ":");

        if (fallbackAddressElements.length == 2) {
            brokerAddresses.add(
                    new Address(fallbackAddressElements[0], NumberUtils.toInt(fallbackAddressElements[1])));
        } else if (fallbackAddressElements.length == 1) {
            brokerAddresses.add(new Address(fallbackAddressElements[0]));
        } else {
            logger.warn("Ignoring unparseable fallback address: " + fallbackAddress);
        }
    }
}

From source file:org.ow2.chameleon.fuchsia.importer.mqtt.MQTTOutputRouter.java

License:Apache License

@Validate
public void start() throws IOException {

    ConnectionFactory factory = new ConnectionFactory();
    connection = factory.newConnection(new Address[] { new Address(serverHost, serverPort) });
    channel = connection.createChannel();
    channel.queueDeclare(queue, false, false, false, null);

    eventArrivalMonitor = new Thread(this);
    eventArrivalMonitor.start();/*w  ww.j ava 2  s .c  o  m*/

    isMonitoringArrivals = true;

}

From source file:org.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQConnectionFactory.java

License:Open Source License

/**
 * Initialize connection factory//  w  w  w.j  a  va2 s. co  m
 */
public void initConnectionFactory() {
    connectionFactory = new ConnectionFactory();
    String hostName = parameters.get(RabbitMQConstants.SERVER_HOST_NAME);
    String portValue = parameters.get(RabbitMQConstants.SERVER_PORT);
    String serverRetryIntervalS = parameters.get(RabbitMQConstants.SERVER_RETRY_INTERVAL);
    String retryIntervalS = parameters.get(RabbitMQConstants.RETRY_INTERVAL);
    String retryCountS = parameters.get(RabbitMQConstants.RETRY_COUNT);
    String heartbeat = parameters.get(RabbitMQConstants.HEARTBEAT);
    String connectionTimeout = parameters.get(RabbitMQConstants.CONNECTION_TIMEOUT);
    String sslEnabledS = parameters.get(RabbitMQConstants.SSL_ENABLED);
    String userName = parameters.get(RabbitMQConstants.SERVER_USER_NAME);
    String password = parameters.get(RabbitMQConstants.SERVER_PASSWORD);
    String virtualHost = parameters.get(RabbitMQConstants.SERVER_VIRTUAL_HOST);

    if (!StringUtils.isEmpty(heartbeat)) {
        try {
            int heartbeatValue = Integer.parseInt(heartbeat);
            connectionFactory.setRequestedHeartbeat(heartbeatValue);
        } catch (NumberFormatException e) {
            //proceeding with rabbitmq default value
            log.warn("Number format error in reading heartbeat value. Proceeding with default");
        }
    }
    if (!StringUtils.isEmpty(connectionTimeout)) {
        try {
            int connectionTimeoutValue = Integer.parseInt(connectionTimeout);
            connectionFactory.setConnectionTimeout(connectionTimeoutValue);
        } catch (NumberFormatException e) {
            //proceeding with rabbitmq default value
            log.warn("Number format error in reading connection timeout value. Proceeding with default");
        }
    }

    if (!StringUtils.isEmpty(sslEnabledS)) {
        try {
            boolean sslEnabled = Boolean.parseBoolean(sslEnabledS);
            if (sslEnabled) {
                String keyStoreLocation = parameters.get(RabbitMQConstants.SSL_KEYSTORE_LOCATION);
                String keyStoreType = parameters.get(RabbitMQConstants.SSL_KEYSTORE_TYPE);
                String keyStorePassword = parameters.get(RabbitMQConstants.SSL_KEYSTORE_PASSWORD);
                String trustStoreLocation = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_LOCATION);
                String trustStoreType = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_TYPE);
                String trustStorePassword = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_PASSWORD);
                String sslVersion = parameters.get(RabbitMQConstants.SSL_VERSION);

                if (StringUtils.isEmpty(keyStoreLocation) || StringUtils.isEmpty(keyStoreType)
                        || StringUtils.isEmpty(keyStorePassword) || StringUtils.isEmpty(trustStoreLocation)
                        || StringUtils.isEmpty(trustStoreType) || StringUtils.isEmpty(trustStorePassword)) {
                    log.warn(
                            "Truststore and keystore information is not provided correctly. Proceeding with default SSL configuration");
                    connectionFactory.useSslProtocol();
                } else {
                    char[] keyPassphrase = keyStorePassword.toCharArray();
                    KeyStore ks = KeyStore.getInstance(keyStoreType);
                    ks.load(new FileInputStream(keyStoreLocation), keyPassphrase);

                    KeyManagerFactory kmf = KeyManagerFactory
                            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    kmf.init(ks, keyPassphrase);

                    char[] trustPassphrase = trustStorePassword.toCharArray();
                    KeyStore tks = KeyStore.getInstance(trustStoreType);
                    tks.load(new FileInputStream(trustStoreLocation), trustPassphrase);

                    TrustManagerFactory tmf = TrustManagerFactory
                            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    tmf.init(tks);

                    SSLContext c = SSLContext.getInstance(sslVersion);
                    c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

                    connectionFactory.useSslProtocol(c);
                }
            }
        } catch (Exception e) {
            log.warn("Format error in SSL enabled value. Proceeding without enabling SSL", e);
        }
    }

    if (!StringUtils.isEmpty(retryCountS)) {
        try {
            retryCount = Integer.parseInt(retryCountS);
        } catch (NumberFormatException e) {
            log.warn("Number format error in reading retry count value. Proceeding with default value (3)", e);
        }
    }

    // Resolving hostname(s) and port(s)
    if (!StringUtils.isEmpty(hostName) && !StringUtils.isEmpty(portValue)) {
        String[] hostNames = hostName.split(",");
        String[] portValues = portValue.split(",");
        if (hostNames.length == portValues.length) {
            addresses = new Address[hostNames.length];
            for (int i = 0; i < hostNames.length; i++) {
                if (!hostNames[i].isEmpty() && !portValues[i].isEmpty()) {
                    try {
                        addresses[i] = new Address(hostNames[i].trim(), Integer.parseInt(portValues[i].trim()));
                    } catch (NumberFormatException e) {
                        handleException("Number format error in port number", e);
                    }
                }
            }
        }
    } else {
        handleException("Host name(s) and port(s) are not correctly defined");
    }

    if (!StringUtils.isEmpty(userName)) {
        connectionFactory.setUsername(userName);
    }

    if (!StringUtils.isEmpty(password)) {
        connectionFactory.setPassword(password);
    }

    if (!StringUtils.isEmpty(virtualHost)) {
        connectionFactory.setVirtualHost(virtualHost);
    }

    if (!StringUtils.isEmpty(retryIntervalS)) {
        try {
            retryInterval = Integer.parseInt(retryIntervalS);
        } catch (NumberFormatException e) {
            log.warn(
                    "Number format error in reading retry interval value. Proceeding with default value (30000ms)",
                    e);
        }
    }

    if (!StringUtils.isEmpty(serverRetryIntervalS)) {
        try {
            int serverRetryInterval = Integer.parseInt(serverRetryIntervalS);
            connectionFactory.setNetworkRecoveryInterval(serverRetryInterval);
        } catch (NumberFormatException e) {
            log.warn(
                    "Number format error in reading server retry interval value. Proceeding with default value",
                    e);
        }
    }

    connectionFactory.setAutomaticRecoveryEnabled(true);
    connectionFactory.setTopologyRecoveryEnabled(false);
}

From source file:play.modules.rabbitmq.RabbitMQPlugin.java

License:Apache License

/**
 * Gets address using conf rabbitmq.seeds=host1[:port1];host2[:port2]...
 * /*from  w ww.j  a  va2s .  co  m*/
 * @return 
 */
public Address[] getAddress(String seeds) {
    List<Address> addresses = new ArrayList<>();
    if (seeds == null || seeds.isEmpty()) {
        addresses.add(new Address("localhost", 5672));
        return addresses.toArray(new Address[0]);
    }
    String[] stringArray = seeds.split("[;,\\s]");
    for (String s : stringArray) {
        String[] hostPort = s.split(":");
        if (0 == hostPort.length) {
            continue;
        }
        String host = hostPort[0];
        int port = 5672;
        if (hostPort.length > 1) {
            port = Integer.parseInt(hostPort[1]);
        }
        addresses.add(new Address(host, port));
    }
    return addresses.toArray(new Address[0]);
}

From source file:zipkin2.collector.rabbitmq.RabbitMQCollector.java

License:Apache License

static Address[] convertAddresses(List<String> addresses) {
    Address[] addressArray = new Address[addresses.size()];
    for (int i = 0; i < addresses.size(); i++) {
        String[] splitAddress = addresses.get(i).split(":");
        String host = splitAddress[0];
        Integer port = null;/*from  ww  w .  j av a 2s. c  om*/
        try {
            if (splitAddress.length == 2)
                port = Integer.parseInt(splitAddress[1]);
        } catch (NumberFormatException ignore) {
        }
        addressArray[i] = (port != null) ? new Address(host, port) : new Address(host);
    }
    return addressArray;
}

From source file:zipkin2.reporter.amqp.RabbitMQSender.java

License:Apache License

static List<Address> convertAddresses(String addresses) {
    String[] addressStrings = addresses.split(",");
    Address[] addressArray = new Address[addressStrings.length];
    for (int i = 0; i < addressStrings.length; i++) {
        String[] splitAddress = addressStrings[i].split(":");
        String host = splitAddress[0];
        Integer port = null;/*from w  ww .  j  a  v  a 2s  . c om*/
        try {
            if (splitAddress.length == 2)
                port = Integer.parseInt(splitAddress[1]);
        } catch (NumberFormatException ignore) {
        }
        addressArray[i] = (port != null) ? new Address(host, port) : new Address(host);
    }
    return Arrays.asList(addressArray);
}