本文整理匯總了Java中com.google.common.net.HostAndPort.getPort方法的典型用法代碼示例。如果您正苦於以下問題:Java HostAndPort.getPort方法的具體用法?Java HostAndPort.getPort怎麽用?Java HostAndPort.getPort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.common.net.HostAndPort
的用法示例。
在下文中一共展示了HostAndPort.getPort方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: logThrift
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
private static int logThrift(HostAndPort address, List<LogEntry> messages)
{
try {
TSocket socket = new TSocket(address.getHost(), address.getPort());
socket.open();
try {
TBinaryProtocol tp = new TBinaryProtocol(new TFramedTransport(socket));
assertEquals(new scribe.Client(tp).Log(messages), ResultCode.OK);
}
finally {
socket.close();
}
}
catch (TException e) {
throw new RuntimeException(e);
}
return 1;
}
示例2: createClientSocket
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
private static TSocket createClientSocket(boolean secure, HostAndPort address)
throws TTransportException
{
if (!secure) {
return new TSocket(address.getHost(), address.getPort());
}
try {
SSLContext serverSslContext = ClientTestUtils.getClientSslContext();
SSLSocket clientSocket = (SSLSocket) serverSslContext.getSocketFactory().createSocket(address.getHost(), address.getPort());
// clientSocket.setSoTimeout(timeout);
return new TSocket(clientSocket);
}
catch (Exception e) {
throw new TTransportException("Error initializing secure socket", e);
}
}
示例3: startOstrichService
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
public static void startOstrichService(String tsdbHostPort, int ostrichPort) {
final int TSDB_METRICS_PUSH_INTERVAL_IN_MILLISECONDS = 10 * 1000;
OstrichAdminService ostrichService = new OstrichAdminService(ostrichPort);
ostrichService.startAdminHttpService();
if (tsdbHostPort != null) {
LOG.info("Starting the OpenTsdb metrics pusher");
try {
HostAndPort pushHostPort = HostAndPort.fromString(tsdbHostPort);
MetricsPusher metricsPusher = new MetricsPusher(
pushHostPort.getHostText(),
pushHostPort.getPort(),
new OpenTsdbMetricConverter("KafkaOperator", HostName),
TSDB_METRICS_PUSH_INTERVAL_IN_MILLISECONDS);
metricsPusher.start();
LOG.info("OpenTsdb metrics pusher started!");
} catch (Throwable t) {
// pusher fail is OK, do
LOG.error("Exception when starting stats pusher: ", t);
}
}
}
示例4: bootstrap
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
public boolean bootstrap(HostAndPort seed,
Node localNode) throws SyncException {
this.localNode = localNode;
succeeded = false;
SocketAddress sa =
new InetSocketAddress(seed.getHostText(), seed.getPort());
ChannelFuture future = bootstrap.connect(sa);
future.awaitUninterruptibly();
if (!future.isSuccess()) {
logger.debug("Could not connect to " + seed, future.cause());
return false;
}
Channel channel = future.channel();
logger.debug("[{}] Connected to {}",
localNode != null ? localNode.getNodeId() : null,
seed);
try {
channel.closeFuture().await();
} catch (InterruptedException e) {
logger.debug("Interrupted while waiting for bootstrap");
return succeeded;
}
return succeeded;
}
示例5: bootstrap
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
public boolean bootstrap(HostAndPort seed,
Node localNode) throws SyncException {
this.localNode = localNode;
succeeded = false;
SocketAddress sa =
new InetSocketAddress(seed.getHostText(), seed.getPort());
ChannelFuture future = bootstrap.connect(sa);
future.awaitUninterruptibly();
if (!future.isSuccess()) {
logger.debug("Could not connect to " + seed, future.getCause());
return false;
}
Channel channel = future.getChannel();
logger.debug("[{}] Connected to {}",
localNode != null ? localNode.getNodeId() : null,
seed);
try {
channel.getCloseFuture().await();
} catch (InterruptedException e) {
logger.debug("Interrupted while waiting for bootstrap");
return succeeded;
}
return succeeded;
}
示例6: bootstrap
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
public boolean bootstrap(HostAndPort seed,
Node localNode) throws SyncException {
this.localNode = localNode;
succeeded = false;
SocketAddress sa =
new InetSocketAddress(seed.getHostText(), seed.getPort());
ChannelFuture future = bootstrap.connect(sa);
future.awaitUninterruptibly();
if (!future.isSuccess()) {
logger.debug("Could not connect to " + seed, future.cause());
return false;
}
Channel channel = future.channel();
logger.debug("[{}] Connected to {}",
localNode != null ? localNode.getNodeId() : null,
seed);
try {
channel.closeFuture().await();
} catch (InterruptedException e) {
logger.debug("Interrupted while waiting for bootstrap");
return succeeded;
}
return succeeded;
}
示例7: createServerSocket
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
/** Binds server socket, incrementing port on {@link BindException} if {@code shouldRetry}. */
ServerSocket createServerSocket(HostAndPort bind, boolean tryAlternativePortsOnFailure)
throws IOException {
InetAddress host = InetAddress.getByName(bind.getHost());
int port = bind.getPort();
BindException bindException = null;
for (int n = 0; n < MAX_BIND_ATTEMPTS; n++) {
try {
return serverSocketFactory.createServerSocket(port, CONNECTION_BACKLOG, host);
} catch (BindException e) {
if (port == 0 || !tryAlternativePortsOnFailure) {
throw e;
}
if (bindException == null) {
bindException = e;
} else if (!e.equals(bindException)) {
bindException.addSuppressed(e);
}
port++;
}
}
throw bindException;
}
示例8: fromString
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
@Nonnull
public static Replica fromString(@Nonnull String info) {
try {
checkNotNull(info);
HostAndPort hostAndPort = HostAndPort.fromString(info);
InetAddress addr = InetAddress.getByName(hostAndPort.getHostText());
InetSocketAddress saddr = new InetSocketAddress(addr, hostAndPort.getPort());
return new Replica(saddr);
} catch (UnknownHostException e) {
throw Throwables.propagate(e);
}
}
示例9: createAndGetConfiguredGraphiteReporter
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
private ScheduledReporter createAndGetConfiguredGraphiteReporter(String prefix, String graphiteHost) {
LOG.info("Configuring Graphite reporter. Sendig data to host:port {}", graphiteHost);
HostAndPort addr = HostAndPort.fromString(graphiteHost);
final Graphite graphite = new Graphite(
new InetSocketAddress(addr.getHostText(), addr.getPort()));
return GraphiteReporter.forRegistry(metrics)
.prefixedWith(prefix)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.build(graphite);
}
示例10: createFrom
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
/**
* Parse a {@link SocketAddress} from the given string.
*/
public static SocketAddress createFrom(String value) {
if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) {
// Unix Domain Socket address.
// Create the underlying file for the Unix Domain Socket.
String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length());
File file = new File(filePath);
if (!file.isAbsolute()) {
throw new IllegalArgumentException("File path must be absolute: " + filePath);
}
try {
if (file.createNewFile()) {
// If this application created the file, delete it when the application exits.
file.deleteOnExit();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
// Create the SocketAddress referencing the file.
return new DomainSocketAddress(file);
} else {
// Standard TCP/IP address.
HostAndPort hostAndPort = HostAndPort.fromString(value);
checkArgument(hostAndPort.hasPort(),
"Address must be a unix:// path or be in the form host:port. Got: %s", value);
return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort());
}
}
示例11: extractServerAddresses
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
private ServerAddress[] extractServerAddresses(MongoClientURI mongoClientURI) {
final List<String> hosts = mongoClientURI.getHosts();
final List<ServerAddress> serverAddresses = new ArrayList<>(hosts.size());
for (String host : hosts) {
final HostAndPort hostAndPort = HostAndPort.fromString(host).withDefaultPort(ServerAddress.defaultPort());
final ServerAddress serverAddress = new ServerAddress(hostAndPort.getHostText(), hostAndPort.getPort());
serverAddresses.add(serverAddress);
}
return serverAddresses.toArray(new ServerAddress[serverAddresses.size()]);
}
示例12: forSeedsAndPort
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
/** Note: <code>seeds</code> may be a single host or comma-delimited list. */
public static CassandraThriftFacade forSeedsAndPort(String seeds, int defaultPort) {
final String seed = seeds.contains(",") ? seeds.substring(0, seeds.indexOf(',')) : seeds;
HostAndPort host = HostAndPort.fromString(seed).withDefaultPort(defaultPort);
return new CassandraThriftFacade(new TFramedTransport(new TSocket(host.getHostText(), host.getPort())));
}
示例13: newCqlDriverBuilder
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
private com.datastax.driver.core.Cluster.Builder newCqlDriverBuilder(ConnectionPoolConfiguration poolConfig,
MetricRegistry metricRegistry) {
performHostDiscovery(metricRegistry);
String[] seeds = _seeds.split(",");
List<String> contactPoints = Lists.newArrayListWithCapacity(seeds.length);
// Each seed may be a host name or a host name and port (e.g.; "1.2.3.4" or "1.2.3.4:9160"). These need
// to be converted into host names only.
for (String seed : seeds) {
HostAndPort hostAndPort = HostAndPort.fromString(seed);
seed = hostAndPort.getHostText();
if (hostAndPort.hasPort()) {
if (hostAndPort.getPort() == _thriftPort) {
_log.debug("Seed {} found using RPC port; swapping for native port {}", seed, _cqlPort);
} else if (hostAndPort.getPort() != _cqlPort) {
throw new IllegalArgumentException(String.format(
"Seed %s found with invalid port %s. The port must match either the RPC (thrift) port %s " +
"or the native (CQL) port %s", seed, hostAndPort.getPort(), _thriftPort, _cqlPort));
}
}
contactPoints.add(seed);
}
PoolingOptions poolingOptions = new PoolingOptions();
if (poolConfig.getMaxConnectionsPerHost().or(getMaxConnectionsPerHost()).isPresent()) {
poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, poolConfig.getMaxConnectionsPerHost().or(getMaxConnectionsPerHost()).get());
}
if (poolConfig.getCoreConnectionsPerHost().or(getCoreConnectionsPerHost()).isPresent()) {
poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, poolConfig.getCoreConnectionsPerHost().or(getCoreConnectionsPerHost()).get());
}
SocketOptions socketOptions = new SocketOptions();
if (poolConfig.getConnectTimeout().or(getConnectTimeout()).isPresent()) {
socketOptions.setConnectTimeoutMillis(poolConfig.getConnectTimeout().or(getConnectTimeout()).get());
}
if (poolConfig.getSocketTimeout().or(getSocketTimeout()).isPresent()) {
socketOptions.setReadTimeoutMillis(poolConfig.getSocketTimeout().or(getSocketTimeout()).get());
}
AuthProvider authProvider = _authenticationCredentials != null
? new PlainTextAuthProvider(_authenticationCredentials.getUsername(), _authenticationCredentials.getPassword())
: AuthProvider.NONE;
return com.datastax.driver.core.Cluster.builder()
.addContactPoints(contactPoints.toArray(new String[contactPoints.size()]))
.withPort(_cqlPort)
.withPoolingOptions(poolingOptions)
.withSocketOptions(socketOptions)
.withRetryPolicy(Policies.defaultRetryPolicy())
.withAuthProvider(authProvider);
}
示例14: removeMatchingPort
import com.google.common.net.HostAndPort; //導入方法依賴的package包/類
/**
* Removes a port from a host+port if the string contains the specified port. If the host+port does not contain
* a port, or contains another port, the string is returned unaltered. For example, if hostWithPort is the
* string {@code www.website.com:443}, this method will return {@code www.website.com}.
*
* <b>Note:</b> The hostWithPort string is not a URI and should not contain a scheme or resource. This method does
* not attempt to validate the specified host; it <i>might</i> throw IllegalArgumentException if there was a problem
* parsing the hostname, but makes no guarantees. In general, it should be validated externally, if necessary.
*
* @param hostWithPort string containing a hostname and optional port
* @param portNumber port to remove from the string
* @return string with the specified port removed, or the original string if it did not contain the portNumber
*/
public static String removeMatchingPort(String hostWithPort, int portNumber) {
HostAndPort parsedHostAndPort = HostAndPort.fromString(hostWithPort);
if (parsedHostAndPort.hasPort() && parsedHostAndPort.getPort() == portNumber) {
// HostAndPort.getHostText() strips brackets from ipv6 addresses, so reparse using fromHost
return HostAndPort.fromHost(parsedHostAndPort.getHostText()).toString();
} else {
return hostWithPort;
}
}