我是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**
我应该在这里使用actor而不是actorsystem并为其专门分配一个调度程序吗?
由于我保存的是二进制图像,因此在达到其目的后,我必须注意将其从内存中删除吗?
代码段将更有帮助。预先感谢
我尝试遵循人们建议使用的在线建议
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")
我是否缺少任何库或导入文件?
答案 0 :(得分: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] = {???}
}