Example usage for com.google.common.net HostAndPort fromParts

List of usage examples for com.google.common.net HostAndPort fromParts

Introduction

In this page you can find the example usage for com.google.common.net HostAndPort fromParts.

Prototype

public static HostAndPort fromParts(String host, int port) 

Source Link

Document

Build a HostAndPort instance from separate host and port values.

Usage

From source file:com.twitter.aurora.scheduler.http.LeaderRedirect.java

private Optional<HostAndPort> getLeaderHttp() {
    ServiceInstance leadingScheduler = leader.get();
    if (leadingScheduler == null) {
        return Optional.absent();
    }/*from w  ww . java2 s  .co  m*/

    if (leadingScheduler.isSetAdditionalEndpoints()) {
        Endpoint leaderHttp = leadingScheduler.getAdditionalEndpoints().get(HTTP_PORT_NAME);
        if (leaderHttp != null && leaderHttp.isSetHost() && leaderHttp.isSetPort()) {
            return Optional.of(HostAndPort.fromParts(leaderHttp.getHost(), leaderHttp.getPort()));
        }
    }

    LOG.warning("Leader service instance seems to be incomplete: " + leadingScheduler);
    return Optional.absent();
}

From source file:net.myrrix.common.PartitionsUtils.java

/**
 * @param port port the instance is configured to run on
 * @return a simple structure representing one partition, one replica: the local host
 *  and configured instance port/* w w w .j a va2  s . co m*/
 */
public static List<List<HostAndPort>> getDefaultLocalPartition(int port) {
    InetAddress localhost;
    try {
        localhost = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        throw new IllegalStateException(e);
    }
    String host = localhost.getHostName();
    return Collections.singletonList(Collections.singletonList(HostAndPort.fromParts(host, port)));
}

From source file:org.springframework.xd.ec2.cloud.AWSInstanceChecker.java

/**
 * Verfies that XD admin or single node is up and running
 * @param instance The instance where the XD server is deployed
 * @param port the port to monitor.//from www .  j  a  va 2  s.  c  o m
 */
public void checkServerInstance(RunningInstance instance, final int port) {
    Assert.notNull(instance, "instance can not be null");
    RunningInstance localInstance = AWSInstanceProvisioner.findInstanceById(client, instance.getId());
    final SocketOpen socketOpen = computeService.getContext().utils().injector().getInstance(SocketOpen.class);
    final Predicate<HostAndPort> socketTester = retry(socketOpen, 300, 1, TimeUnit.SECONDS);
    LOGGER.info(String.format("Awaiting XD server to start"));
    if (!socketTester.apply(HostAndPort.fromParts(localInstance.getIpAddress(), port))) {
        throw new DeployTimeoutException(
                "timeout waiting for server to start: " + localInstance.getIpAddress());
    }
    LOGGER.info(String.format("Server started%n"));

}

From source file:io.airlift.jmx.JmxAgent.java

@Inject
public JmxAgent(JmxConfig config) throws IOException {
    // first, see if the jmx agent is already started (e.g., via command line properties passed to the jvm)
    HostAndPort address = getRunningAgentAddress(config.getRmiRegistryPort(), config.getRmiServerPort());
    if (address != null) {
        log.info("JMX agent already running and listening on %s", address);
    } else {/*from www .  j  a  v a 2  s.  c om*/
        // otherwise, start it manually
        int registryPort;
        if (config.getRmiRegistryPort() == null) {
            registryPort = NetUtils.findUnusedPort();
        } else {
            registryPort = config.getRmiRegistryPort();
        }

        int serverPort = 0;
        if (config.getRmiServerPort() != null) {
            serverPort = config.getRmiServerPort();
        }

        // This is somewhat of a hack, but the jmx agent in Oracle/OpenJDK doesn't
        // have a programmatic API for starting it and controlling its parameters
        System.setProperty("com.sun.management.jmxremote", "true");
        System.setProperty("com.sun.management.jmxremote.port", Integer.toString(registryPort));
        System.setProperty("com.sun.management.jmxremote.rmi.port", Integer.toString(serverPort));
        System.setProperty("com.sun.management.jmxremote.authenticate", "false");
        System.setProperty("com.sun.management.jmxremote.ssl", "false");

        try {
            Agent.startAgent();
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }

        try {
            // This is how the jdk jmx agent constructs its url
            JMXServiceURL url = new JMXServiceURL("rmi", null, registryPort);
            address = HostAndPort.fromParts(url.getHost(), url.getPort());
        } catch (MalformedURLException e) {
            // should not happen...
            throw new AssertionError(e);
        }

        log.info("JMX agent started and listening on %s", address);
    }

    this.url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi",
            address.getHostText(), address.getPort()));
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

private HostAndPort getServerHostAndPort() {
    ServerAddress address = getServerAddress();
    return HostAndPort.fromParts(address.getHost(), address.getPort());
}

From source file:org.apache.accumulo.server.monitor.ZooKeeperStatus.java

@Override
public void run() {

    while (!stop) {

        TreeSet<ZooKeeperState> update = new TreeSet<ZooKeeperState>();

        String zookeepers[] = ServerConfiguration.getSiteConfiguration().get(Property.INSTANCE_ZK_HOST)
                .split(",");
        for (String keeper : zookeepers) {
            int clients = 0;
            String mode = "unknown";

            String[] parts = keeper.split(":");
            TTransport transport = null;
            try {
                HostAndPort addr;//from  w w  w.j  av  a  2 s.c  om
                if (parts.length > 1)
                    addr = HostAndPort.fromParts(parts[0], Integer.parseInt(parts[1]));
                else
                    addr = HostAndPort.fromParts(parts[0], 2181);

                transport = TTimeoutTransport.create(addr, 10 * 1000l);
                transport.write("stat\n".getBytes(), 0, 5);
                StringBuilder response = new StringBuilder();
                try {
                    transport.flush();
                    byte[] buffer = new byte[1024 * 100];
                    int n = 0;
                    while ((n = transport.read(buffer, 0, buffer.length)) > 0) {
                        response.append(new String(buffer, 0, n));
                    }
                } catch (TTransportException ex) {
                    // happens at EOF
                }
                for (String line : response.toString().split("\n")) {
                    if (line.startsWith(" "))
                        clients++;
                    if (line.startsWith("Mode"))
                        mode = line.split(":")[1];
                }
                update.add(new ZooKeeperState(keeper, mode, clients));
            } catch (Exception ex) {
                log.info("Exception talking to zookeeper " + keeper, ex);
                update.add(new ZooKeeperState(keeper, "Down", -1));
            } finally {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                        log.error(ex, ex);
                    }
                }
            }
        }
        status = update;
        UtilWaitThread.sleep(1000);
    }
}

From source file:org.cfg4j.source.consul.ConsulConfigurationSource.java

/**
 * @throws SourceCommunicationException when unable to connect to Consul client
 *///from w ww .  ja v  a  2  s.co  m
@Override
public void init() {
    try {
        LOG.info("Connecting to Consul client at " + host + ":" + port);

        Consul consul = Consul.builder().withHostAndPort(HostAndPort.fromParts(host, port)).build();

        kvClient = consul.keyValueClient();
    } catch (Exception e) {
        throw new SourceCommunicationException("Can't connect to host " + host + ":" + port, e);
    }

    initialized = true;
}

From source file:com.pinterest.secor.common.LegacyKafkaClient.java

private HostAndPort findLeader(TopicPartition topicPartition) {
    SimpleConsumer consumer = null;//from ww w .  j a  v  a2  s. c o  m
    try {
        LOG.debug("looking up leader for topic {} partition {}", topicPartition.getTopic(),
                topicPartition.getPartition());
        consumer = createConsumer(mConfig.getKafkaSeedBrokerHost(), mConfig.getKafkaSeedBrokerPort(),
                "leaderLookup");
        List<String> topics = new ArrayList<String>();
        topics.add(topicPartition.getTopic());
        TopicMetadataRequest request = new TopicMetadataRequest(topics);
        TopicMetadataResponse response = consumer.send(request);

        List<TopicMetadata> metaData = response.topicsMetadata();
        for (TopicMetadata item : metaData) {
            for (PartitionMetadata part : item.partitionsMetadata()) {
                if (part.partitionId() == topicPartition.getPartition()) {
                    return HostAndPort.fromParts(part.leader().host(), part.leader().port());
                }
            }
        }
    } finally {
        if (consumer != null) {
            consumer.close();
        }
    }
    return null;
}

From source file:io.airlift.jmx.JmxAgent8.java

@Inject
public JmxAgent8(JmxConfig config) throws IOException {
    // first, see if the jmx agent is already started (e.g., via command line properties passed to the jvm)
    HostAndPort address = getRunningAgentAddress(config.getRmiRegistryPort(), config.getRmiServerPort());
    if (address != null) {
        log.info("JMX agent already running and listening on %s", address);
    } else {/*from ww w .  j  av a  2s  .  c  om*/
        // otherwise, start it manually
        int registryPort;
        if (config.getRmiRegistryPort() == null) {
            registryPort = NetUtils.findUnusedPort();
        } else {
            registryPort = config.getRmiRegistryPort();
        }

        int serverPort = 0;
        if (config.getRmiServerPort() != null) {
            serverPort = config.getRmiServerPort();
        }

        // This is somewhat of a hack, but the jmx agent in Oracle/OpenJDK doesn't
        // have a programmatic API for starting it and controlling its parameters
        System.setProperty("com.sun.management.jmxremote", "true");
        System.setProperty("com.sun.management.jmxremote.port", Integer.toString(registryPort));
        System.setProperty("com.sun.management.jmxremote.rmi.port", Integer.toString(serverPort));
        System.setProperty("com.sun.management.jmxremote.authenticate", "false");
        System.setProperty("com.sun.management.jmxremote.ssl", "false");

        try {
            Agent.startAgent();
        } catch (Exception e) {
            throwIfUnchecked(e);
            throw new RuntimeException(e);
        }

        try {
            // This is how the jdk jmx agent constructs its url
            JMXServiceURL url = new JMXServiceURL("rmi", null, registryPort);
            address = HostAndPort.fromParts(url.getHost(), url.getPort());
        } catch (MalformedURLException e) {
            // should not happen...
            throw new AssertionError(e);
        }

        log.info("JMX agent started and listening on %s", address);
    }

    this.url = new JMXServiceURL(
            String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", address.getHost(), address.getPort()));
}

From source file:me.bulat.jivr.consul.manager.SimpleConsulManager.java

public SimpleConsulManager(final String host, final String node, final int port) {
    this.client = Consul.builder().withHostAndPort(HostAndPort.fromParts(host, port)).build();
    this.nodeName = node;
}