本文整理汇总了Scala中java.net.SocketException类的典型用法代码示例。如果您正苦于以下问题:Scala SocketException类的具体用法?Scala SocketException怎么用?Scala SocketException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SocketException类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: CheckConnection
//设置package包名称以及导入依赖的类
package com.stulsoft.pkafka.scala
import java.net.{ConnectException, HttpURLConnection, SocketException, URL}
import org.slf4j.Logger
import org.slf4j.LoggerFactory
object CheckConnection {
val logger: Logger = LoggerFactory.getLogger(CheckConnection.getClass)
def checkConnection(): Boolean = {
var available: Boolean = false
val urlString: String = "http://localhost:9092"
try {
val url: URL = new URL(urlString)
val con: HttpURLConnection = url.openConnection.asInstanceOf[HttpURLConnection]
con.setRequestMethod("GET")
val responseCode: Int = con.getResponseCode
logger.debug("responseCode={}", responseCode)
}
catch {
case e: ConnectException => logger.error("No connection with Kafka server. Error: {}", e.getMessage)
case e: SocketException =>
logger.info("Kafka server is available. {}", e.getMessage)
available = true
case e: Exception => logger.error("Failure", e)
}
available
}
def main(args: Array[String]): Unit = {
logger.debug("==>CheckConnection")
logger.debug("Is Kafka server available: {}", checkConnection())
logger.debug("<==CheckConnection")
}
}
示例2: UDPSender
//设置package包名称以及导入依赖的类
package github.gphat.censorinus
import java.net.{InetSocketAddress,SocketException}
import java.nio.ByteBuffer
import java.nio.channels.UnresolvedAddressException
import java.nio.channels.DatagramChannel
import java.nio.charset.StandardCharsets
class UDPSender(
hostname: String = "localhost",
port: Int = MetricSender.DEFAULT_STATSD_PORT,
allowExceptions: Boolean = false
) extends MetricSender {
lazy val clientSocket = DatagramChannel.open.connect(new InetSocketAddress(hostname, port))
def send(message: String): Unit = {
try {
clientSocket.write(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)))
} catch {
case se @ (_ : SocketException | _ : UnresolvedAddressException) => {
// Check if we're allowing exceptions and rethrow if so. We didn't use
// a guard on the case because then we'd need a second case to catch
// the !allowExceptions case!
if(allowExceptions) {
throw se
}
}
}
}
def shutdown: Unit = clientSocket.close
}