当前位置: 首页>>代码示例>>Scala>>正文


Scala SocketException类代码示例

本文整理汇总了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")
  }
} 
开发者ID:ysden123,项目名称:poc,代码行数:39,代码来源:CheckConnection.scala

示例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
} 
开发者ID:gphat,项目名称:censorinus,代码行数:34,代码来源:UDPSender.scala


注:本文中的java.net.SocketException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。