如何检查 Scala Spray Bind 是否实际绑定到端口

How to check if Scala Spray Bind actually binds to port

从 Scala Spray 文档中不清楚如何检查它是否无法绑定到特定端口

implicit val system = ActorSystem("mediaiton")
    implicit  val timeout = Timeout(5, TimeUnit.SECONDS)
    val service = system.actorOf(Props[IotRestNB], "mediaiton")
    println(s"Going to start the REST NB at $host $port")

    IO(Http) ! Http.Bind(service, interface = host, port = port)

花一天时间尝试从不同的 post

中找出答案
import java.util.concurrent.TimeUnit

import akka.actor.{ActorSystem, Props}
import akka.io.IO
import akka.util.Timeout
import nb.rest.IotRestNB
import spray.can.Http

implicit val system = ActorSystem("lwm2m-mediaiton")
    implicit  val timeout = Timeout(5, TimeUnit.SECONDS)
    val service = system.actorOf(Props[IotClassNB], "lwm2m-mediaiton")
    println(s"Going to start the REST NB at $host $port")

    IO(Http).tell(Http.Bind(service, interface = host, port = port), sender = service)

现演员Class - IotClassNB

import java.util.concurrent.Executors

import akka.actor.Actor
import lwm2m.server.BootUp
import org.eclipse.leshan.core.request.ContentFormat
import spray.can.Http._
import spray.http.MediaTypes
import spray.routing.HttpService

import scala.concurrent.{ExecutionContext, Future}

class IotClassNBextends Actor with MediationRoute {
  //mixin class

  def actorRefFactory = context

  def receive = handleConnection orElse runRoute(route)

  def handleConnection: Receive  =  {
    case b: Bound =>
      println("***REST Server Started***")
      Future.successful(b)
    case failed: CommandFailed =>
      println("***REST Server Could not be Started***")//this is what we want
      Future.failed(new
          RuntimeException("Binding failed"))


  }

}


trait MediationRoute extends HttpService {

  // Execution Context for blocking ops
  val blockingExecutionContext = {
    ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))
  }
  val route = {
    pathPrefix("v1") {
      pathPrefix("mediation") {
        respondWithMediaType(MediaTypes.`application/json`) {
          pathPrefix("get_connected_clients") {
            pathEnd {
              get {

                complete(
                  //  Future.apply {
                  get_registered_clients())
                // }(blockingExecutionContext))

              }
            }.....

下面是如何通过 Spray Client 测试您的 Spary 服务器

@Test def test_RestNB(): Unit = {

    implicit val system = ActorSystem("test")
    import system.dispatcher
    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
    implicit val timeout = Timeout(25, TimeUnit.SECONDS) 

    val server_url = s"http://${host}:${port}/xxx/"
    val response: Future[HttpResponse] = pipeline(Get(server_url))
    val result = Await.result(response, timeout.duration) //wait for timeout
    // println(s"Await Result is ${result.entity.asString}")
    response.onComplete {
      case Success(result: HttpResponse) =>
        logger.info("Result: " + result.entity.asString)
        assert(result.entity.asString === xxxxx")

      case Failure(error) =>
        logger.error(error + "Couldn't get list of items")
      case _ =>
        assert(false)

    }