java.net.BindException Scala Examples
The following examples show how to use java.net.BindException.
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.
Example 1
Source File: GrpcServerOwner.scala From daml with Apache License 2.0 | 6 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.apiserver import java.io.IOException import java.net.{BindException, InetAddress, InetSocketAddress} import java.util.concurrent.TimeUnit.SECONDS import com.daml.metrics.Metrics import com.daml.platform.apiserver.GrpcServerOwner._ import com.daml.ports.Port import com.daml.resources.{Resource, ResourceOwner} import com.google.protobuf.Message import io.grpc.netty.NettyServerBuilder import io.grpc._ import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.ssl.SslContext import scala.concurrent.{ExecutionContext, Future} import scala.util.control.NoStackTrace final class GrpcServerOwner( address: Option[String], desiredPort: Port, maxInboundMessageSize: Int, sslContext: Option[SslContext] = None, interceptors: List[ServerInterceptor] = List.empty, metrics: Metrics, eventLoopGroups: ServerEventLoopGroups, services: Iterable[BindableService], ) extends ResourceOwner[Server] { override def acquire()(implicit executionContext: ExecutionContext): Resource[Server] = { val host = address.map(InetAddress.getByName).getOrElse(InetAddress.getLoopbackAddress) Resource(Future { val builder = NettyServerBuilder.forAddress(new InetSocketAddress(host, desiredPort.value)) builder.sslContext(sslContext.orNull) builder.channelType(classOf[NioServerSocketChannel]) builder.permitKeepAliveTime(10, SECONDS) builder.permitKeepAliveWithoutCalls(true) builder.directExecutor() builder.maxInboundMessageSize(maxInboundMessageSize) interceptors.foreach(builder.intercept) builder.intercept(new MetricsInterceptor(metrics)) eventLoopGroups.populate(builder) services.foreach { service => builder.addService(service) toLegacyService(service).foreach(builder.addService) } val server = builder.build() try { server.start() } catch { case e: IOException if e.getCause != null && e.getCause.isInstanceOf[BindException] => throw new UnableToBind(desiredPort, e.getCause) } server })(server => Future(server.shutdown().awaitTermination())) } // This exposes the existing services under com.daml also under com.digitalasset. // This is necessary to allow applications built with an earlier version of the SDK // to still work. // The "proxy" services will not show up on the reflection service, because of the way it // processes service definitions via protobuf file descriptors. private def toLegacyService(service: BindableService): Option[ServerServiceDefinition] = { val `com.daml` = "com.daml" val `com.digitalasset` = "com.digitalasset" val damlDef = service.bindService() val damlDesc = damlDef.getServiceDescriptor // Only add "proxy" services if it actually contains com.daml in the service name. // There are other services registered like the reflection service, that doesn't need the special treatment. if (damlDesc.getName.contains(`com.daml`)) { val digitalassetName = damlDesc.getName.replace(`com.daml`, `com.digitalasset`) val digitalassetDef = ServerServiceDefinition.builder(digitalassetName) damlDef.getMethods.forEach { methodDef => val damlMethodDesc = methodDef.getMethodDescriptor val digitalassetMethodName = damlMethodDesc.getFullMethodName.replace(`com.daml`, `com.digitalasset`) val digitalassetMethodDesc = damlMethodDesc.toBuilder.setFullMethodName(digitalassetMethodName).build() val _ = digitalassetDef.addMethod( digitalassetMethodDesc.asInstanceOf[MethodDescriptor[Message, Message]], methodDef.getServerCallHandler.asInstanceOf[ServerCallHandler[Message, Message]] ) } Option(digitalassetDef.build()) } else None } } object GrpcServerOwner { final class UnableToBind(port: Port, cause: Throwable) extends RuntimeException( s"The API server was unable to bind to port $port. Terminate the process occupying the port, or choose a different one.", cause) with NoStackTrace }
Example 2
Source File: Utils.scala From DataXServer with Apache License 2.0 | 5 votes |
package org.tianlangstudio.data.hamal.yarn.util import java.net.BindException import java.util.Calendar import org.apache.hadoop.yarn.api.records.{ApplicationAttemptId, ApplicationId, ContainerId, NodeId} import org.tianlangstudio.data.hamal.common.exp.DataHamalException import org.tianlangstudio.data.hamal.core.HamalConf private[hamal] object Utils { def isBindCollision(exception: Throwable): Boolean = { exception match { case e: BindException => if (e.getMessage != null) { return true } isBindCollision(e.getCause) case e: Exception => isBindCollision(e.getCause) case _ => false } } def containerIdNodeId2ExecutorId(containerId:ContainerId,nodeId:NodeId): String = { val appAttId= containerId.getApplicationAttemptId val applicationId = appAttId.getApplicationId val appClusterTs = applicationId.getClusterTimestamp val appId = applicationId.getId val attId = appAttId.getAttemptId val conId = containerId.getContainerId val nodeHost = nodeId.getHost val nodePort = nodeId.getPort s"$appClusterTs:$appId:$attId:$conId:$nodeHost:$nodePort" } def executorId2ContainerIdNodeId(executorId:String) = { executorId.split(":") match { case Array(appClusterTs,appId,attId,conId,nodeHost,nodePort) => val appAttId = ApplicationAttemptId.newInstance(ApplicationId.newInstance(appClusterTs.toLong,appId.toInt),attId.toInt) val containerId = ContainerId.newContainerId(appAttId,conId.toInt) val nodeId = NodeId.newInstance(nodeHost,nodePort.toInt); Some(containerId,nodeId) case _ => None } } private val taskIdLock = new Object private val preId = ""; def genTaskId():String = { val now = Calendar.getInstance val hour = now.get(Calendar.HOUR_OF_DAY) val min = now.get(Calendar.MINUTE) val seconds = now.get(Calendar.SECOND) val ms = now.get(Calendar.MILLISECOND) val id = (hour * 3600 * 1000 + min * 60 * 1000 + seconds * 1000 + ms) + "" taskIdLock.synchronized( if(id.equals(preId)) { genTaskId() }else { id } ) } }
Example 3
Source File: EventDrivenFetcher.scala From Mycat-spider with Apache License 2.0 | 5 votes |
package turbo.crawler.power import java.io.IOException import java.io.StringReader import java.net.BindException import java.net.SocketException import java.net.SocketTimeoutException import org.apache.commons.httpclient.ConnectTimeoutException import org.apache.commons.httpclient.Header import org.cyberneko.html.parsers.DOMParser import org.w3c.dom.Document import org.xml.sax.InputSource import turbo.crawler.FetchRejectedException import turbo.crawler.Fetchable import turbo.crawler.IO import turbo.crawler.Logable import turbo.crawler.ResourceHasAlreadyBeenFetchedException import turbo.crawler.StringAdapter import turbo.crawler.io.HttpReturns /** * Event driven fetcher * @author mclaren * */ class EventDrivenFetcher[T <: Fetchable](eventId: String) extends Logable with MessageDriven with IO with StringAdapter { def fetch(fetchUrl: String , contentFilter: String => String , parseDocument: Document => List[T])(hasRejected: Document => Boolean) = { val _retry = (msg: String) => { logger.info("Retry " + msg) Thread.sleep(3000) this.fetch(fetchUrl, contentFilter, parseDocument)(hasRejected)(howToContinue)(referer) } var httpReturns: HttpReturns = null try { val dom = new DOMParser httpReturns = this.fromUrl(fetchUrl, Array[Header](new Header("Referer", referer(fetchUrl)))) dom.parse(new InputSource(new StringReader(contentFilter(httpReturns.body)))) var document = dom.getDocument //检查是否被屏蔽 if (hasRejected(document)) throw new FetchRejectedException(fetchUrl) parseDocument(document).foreach(x => fireEvent(new Evt(eventId + "_COMPLETION", x))) } catch { case e: SocketTimeoutException => _retry(e.getMessage) case e: SocketException => _retry(e.getMessage) case e: ConnectTimeoutException => _retry(e.getMessage) case e: IOException => { logger.info("Oh网络错误with代理:" + httpReturns.proxy.ip + ":" + httpReturns.proxy.port) howToContinue(fetchUrl, httpReturns.proxy) //10秒之内只允许出现一次重拨 _retry(e.getMessage) } case e: BindException => _retry(e.getMessage) case e: FetchRejectedException => { logger.info("Oh 惨遭屏蔽~") howToContinue(e.getFetchUrl, httpReturns.proxy) //10秒之内只允许出现一次重拨 _retry(e.getMessage) } case e: ResourceHasAlreadyBeenFetchedException => case e: Exception => { logger.error("Unknown exception has been occurred", e) } } } }