Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put dispatch.Http.shutdown()

where to place the call to dispatch.Http.shutdown() if there are n independent Http calls like, for example:

import com.typesafe.scalalogging.slf4j.Logging
import org.json4s._
import org.json4s.native.JsonMethods._
import scala.util.{ Failure, Success }

object Main extends App with Logging {
  logger.debug("github.cli")

  // GET /users/defunkt: `curl https://api.github.com/users/defunkt`
  val host: dispatch.Req    = dispatch.host("api.github.com").secure
  val request: dispatch.Req = host / "users" / "defunkt"
  logger.debug(s"Request URL: ${request.url}")

  import dispatch.Defaults.executor

  dispatch.Http(request > dispatch.as.Response(_.getHeaders())) onComplete {
    case Success(h) => logger.debug(h.toString())
    case Failure(e) => logger.debug(s"Error: $e")
  }
  dispatch.Http(request OK dispatch.as.json4s.Json) onComplete {
    case Success(j) => logger.debug(j.toString())
    case Failure(e) => logger.debug(s"Error: $e")
  }

  //dispatch.Http.shutdown()    // <<<<< ?????
}

Thanks, /nm

Edit: Could that be an option, say, is that a "proper" way to do it?

val headers = dispatch.Http(request > dispatch.as.Response(_.getHeaders()))
val user    = dispatch.Http(request OK dispatch.as.json4s.Json)
val all     = dispatch.Future.sequence(headers :: user :: Nil)

headers onComplete {
  case Success(h) => logger.debug(s"Header: ${h.toString()}")
  case Failure(e) => logger.debug(s"Error: $e")
}
user onComplete {
  case Success(j) => logger.debug(s"User: ${j.toString()}")
  case Failure(e) => logger.debug(s"Error: $e")
}
all onComplete { case _ => dispatch.Http.shutdown() }
like image 436
nemron Avatar asked Oct 20 '22 14:10

nemron


1 Answers

AFAIK you only do it at the end of the application (or, strictly speaking, when you are done with a specific thread), because once it is done, all the existing connections are gone and you cannot create any new ones.

like image 155
Ashalynd Avatar answered Oct 23 '22 10:10

Ashalynd