2016-08-17 1 views
0

Из Spray документации Scala не понятно, как проверить, если он не способен связываться с определенным портомКак проверить, если Scala Spray Bind на самом деле связывает порт

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) 

ответ

0

Проведите день, пытаясь понять из различного поста

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) 

Теперь актер класса - 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)) 

       } 
      }..... 

И вот как протестировать ваш Spary-сервер через Spray Client

@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) 

    } 
Смежные вопросы