本文介绍了WSClient-打开的文件太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在CentOS 6上使用Play Framework 2.4,并且我的应用程序抛出此异常:

I'm working with Play Framework 2.4 on CentOS 6 and my application is throwing this exception:

java.net.SocketException: Too many open files

我搜索了很多有关Stack Overflow的主题,并尝试了解决方案:

I've searched a lot of topics on Stack Overflow and tried the solutions:

  • 将打开的文件数增加到65535;
  • 更改/etc/security/limits.conf的硬性限制和软性限制;
  • 更改/etc/sysctl.conf上的fs.file-max的值;
  • 减少了文件/proc/sys/net/ipv4/tcp_fin_timeout的超时;

错误不断发生.在另一个站点上,我发现人们面临着同样的问题,因为他们没有从WSClient调用方法close(),但就我而言,我正在使用依赖项注入:

The error keeps happening. On another sites, i've found people that are facing the same problem because they weren't calling the method close() from WSClient but in my case, i'm working with dependency injection:

@Singleton
class RabbitService @Inject()(ws:WSClient) {

  def myFunction() {
    ws.url("url").withHeaders(
      "Content-type" -> "application/json",
      "Authorization" -> ("Bearer " + authorization))
      .post(message)
      .map(r => {
      r.status match {
        case 201 => Logger.debug("It Rocks")
        case _ => Logger.error(s"It sucks")
      }
    })

  }

}

如果我更改实现以等待结果,它的工作就像一个魅力,但是性能却很差-我想使用map函数代替等待结果:

If i change my implementation to await the result, it work's like a charm but the performance is very poor - and i would like to use map function instead wait the result:

@Singleton
class RabbitService @Inject()(ws:WSClient) {

  def myFunction() {
    val response = ws.url("url")
      .withHeaders(
        "Content-type" -> "application/json",
        "Authorization" -> ("Bearer " + authorization))
      .post(message)

    Try(Await.result(response, 1 seconds)) match {
      case Success(r) =>
        if(r.status == 201) {
          Logger.debug(s"It rocks")
        } else {
          Logger.error(s"It sucks")
        }
      case Failure(e) => Logger.error(e.getMessage, e)
    }

  }

}

任何人都知道如何解决此错误?我已经尝试了一切,但没有成功.

Anyone have an idea how can i fix this error? I've tried everything but without success.

推荐答案

如果有人遇到相同的问题,则需要在application.conf上配置WSClient-需要设置maxConnectionsTotalmaxConnectionsPerHost.

If anyone is facing the same problem, you need to configure WSClient on application.conf - need to set maxConnectionsTotal and maxConnectionsPerHost.

这就是我解决这个问题的方式.

This is how I solved this problem.

https://www.playframework.com/documentation/2.5.x/ScalaWS

这篇关于WSClient-打开的文件太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:46