如何使用Akka-Http进行并行Http请求?

问题描述 投票:1回答:1

我是Scala的新手,并尝试实现一个库,我将获得数千个URL。我的工作是从这些URL下载内容。我会选择简单的scalaj-http库,但它不符合我的目的。我附带的代码是这样的:

    class ProxyHttpClient {
      def get(url: String, proxy: ProxySettings,urlDownloaderConfig: 
    UrlDownloaderConfig)(implicit ec: ExecutionContext): Either[HttpError, 
    HttpSuccessResponse] = {
        implicit val system: ActorSystem = ActorSystem()
        implicit val materializer: ActorMaterializer = ActorMaterializer()


        val auth = headers.BasicHttpCredentials(proxy.userName, 
    proxy.secret)
    val httpsProxyTransport = 
      ClientTransport.httpsProxy(InetSocketAddress.createUnresolved(
    proxy.host, proxy.port), auth)
    val settings = 
ConnectionPoolSettings(system).withTransport(httpsProxyTransport)
    val response: Future[HttpResponse] = 

Http().singleRequest(HttpRequest().
withMethod(HttpMethods.GET).withUri(url), settings = settings)

    val data: Future[Either[HttpError, HttpSuccessResponse]] = `response.map {`
      case response@HttpResponse(StatusCodes.OK, _, _, _) => {
        val content: Future[String] = Unmarshal(response.entity).to[String]
        val finalContent = Await.ready(content, timeToWaitForContent).value.get.get.getBytes
        Right(HttpSuccessResponse(url, response.status.intValue(), finalContent))
      }
      case errorResponse@HttpResponse(StatusCodes.GatewayTimeout, _, _, _) => Left(HttpError(url, errorResponse.status.intValue(), errorResponse.entity.toString))
    }
    val result: Try[Either[HttpError, HttpSuccessResponse]] = Await.ready(data, timeToWaitForResponse).value.get
    val pop: Either[HttpError, HttpSuccessResponse] = try {
      result.get
    } catch {
      case e: Exception => Left(HttpError(url, HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage))
    }
    pop
  }
}

我正在使用调用get方法

val forkJoinPool = new scala.concurrent.forkjoin.ForkJoinPool(8)
picList.par.tasksupport = new ForkJoinTaskSupport(forkJoinPool)
picList.par.map(testUrl => {
      val resp = get(url, Option(proxy))

    })

它运行顺利几次但是当我尝试调用1000个url的方法来获取批量大小为100的图像时,它会抛出以下错误。在那之后,即使对于单个URL我也得到相同的错误。

**java.lang.OutOfMemoryError: unable to create new native thread**
  1. 我应该在这里使用演员而不是演员系统并为其专门设置一个调度员吗?
  2. 既然我拿着二进制图像的内容,我必须在服务目的后将其从内存中删除吗?

代码段会更有帮助。提前致谢

我尝试按照人们建议使用的在线建议

val blockingExecutionContext = system.dispatchers.lookup("blocking-dispatcher")

但是当我尝试时,system.dispatchers.lookup正在返回MessageDispacther类型。

implicit val system: ActorSystem = ActorSystem()
    val ex: MessageDispatcher =system.dispatchers.lookup("io-blocking-dispatcher")

是否有任何图书馆或进口缺失?

scala akka akka-http
1个回答
0
投票

您的问题很可能与为每个http调用创建actor系统有关。 Actor系统通常是每个应用程序一个。

做一个小的重构并试一试。

class ProxyHttpClient() {
  private implicit val system: ActorSystem = ActorSystem()
  private implicit val materializer: ActorMaterializer = ActorMaterializer()

  def get(url: String, proxy: ProxySettings,urlDownloaderConfig: 
    UrlDownloaderConfig)(implicit ec: ExecutionContext): Either[HttpError, 
    HttpSuccessResponse] = {???}
}

或者提取actor系统并将其作为隐式参数传递

class ProxyHttpClient() {

  def get(url: String, proxy: ProxySettings,urlDownloaderConfig: 
    UrlDownloaderConfig)(implicit ec: ExecutionContext, system: ActorSystem, materializer: ActorMaterializer): Either[HttpError, 
    HttpSuccessResponse] = {???}
}
© www.soinside.com 2019 - 2024. All rights reserved.