简体   繁体   English

这个实例在我的例子中只创建了一次吗?

[英]Is this instance created only once in my example?

I have the following code.我有以下代码。 In the other class, I tried to create the S3ClientClass2 object as val s3 = new S3ClientClass2() .在另一个 class 中,我尝试将S3ClientClass2 object 创建为val s3 = new S3ClientClass2() After creating the s3, then calling the readFromS3 method for every single request.创建 s3 之后,然后为每个请求调用readFromS3方法。 In this scenario, I am wondering that the amazonS3Client is created only once or created many times for every request.在这种情况下,我想知道amazonS3Client是只创建一次还是为每个请求创建多次。 I think that is is created only once.我认为这是只创建一次。 Is this right?这是正确的吗?

class S3ClientClass2 {
  lazy val amazonS3Client = this.getS3Client()

  private def getS3Client() = {
    AmazonS3ClientBuilder
      .standard()
      .withRegion(Regions.AP_NORTHEAST_1)
      .build()
  }

  def readFromS3(s3Bucket: String, filepath: String): String = {
    var s3object: S3Object = null
    try {
      s3object = amazonS3Client.getObject(s3Bucket, filepath)
      readFromS3(s3object)
    }
    finally {
      if (s3object != null) {
        s3object.close()
      }
    }
  }

  def readFromS3(obj: S3Object): String = {
    val reader = new BufferedReader(new InputStreamReader(obj.getObjectContent))
    reader.lines().collect(Collectors.joining())
  }
}

yes, lazy val is initialised only once when it is first used.是的, lazy val在第一次使用时只初始化一次。 That means, the first time you use amazonS3Client the getS3Client method will be called, every subsequent usage of amazonS3Client will use the cached value.这意味着,第一次使用amazonS3Client时,将调用getS3Client方法,随后每次使用amazonS3Client时,都将使用缓存的值。

Some other hints.其他一些提示。 You are mixing in Java stuff in readFromS3(obj: S3Object) method for no good reason, it could be easily rewritten to pure Scala:您无缘无故在readFromS3(obj: S3Object)方法中混合了 Java 内容,它可以很容易地重写为纯 Scala:

  def readFromS3(obj: S3Object): String = {
    scala.io.Source.fromInputStream(obj.getObjectContent).mkString
  }

Regarding readFromS3(s3Bucket: String, filepath: String) , you should never used null in scala, if you are working with something that might or might not have a value see Option , for things that might crash with some error see scala.util.Either and scala.util.Try .关于readFromS3(s3Bucket: String, filepath: String) ,你不应该在 scala 中使用null ,如果你正在处理可能有或可能没有值的东西,请参阅Option ,对于可能因某些错误而崩溃的东西,请参阅scala.util.Eitherscala.util.Either scala.util.Try Also, what is the expected behaviour of this function when exception is thrown in the try block?此外,当try块中抛出异常时,此 function 的预期行为是什么? In the current design it will rethrow it and escalate up your call stack.在当前的设计中,它将重新抛出它并升级您的调用堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保用户在没有任何用户身份验证的情况下只能在我的 ReactJS 应用程序上投票一次? - How can I Ensure a User can only Vote Once on my ReactJS App Without any User Authentication? 我的 firebase 可调用云 function 被调用了两次(当我只调用一次时),如何避免我的 flutter 应用程序失败? - My firebase callable cloud function is called twice (when I called only once), how can I avoid my flutter app from failure? 如何让 Cloud Tasks 只运行一次? - How to have Cloud Tasks run only once? Python 嵌套for循环只运行一次 - Python nested for loop runs only once 我创建了一个实例组,所有 100 个创建的实例都有这个红色感叹号,表示该实例不存在 - I created an instance group and all of the 100 created instances have this red exclamination mark saying this instance doesn't exist AWS EKS 创建的 ASG 的每个 AZ 配置的多个实例类型 - Multiple instance types per AZ configuration for ASG created by AWS EKS 无法访问使用 terraform 创建的 EC2 实例 - unable to access EC2 instance created using terraform 用户只能查看自己实例的 IAM 权限 - IAM permission for user to only view their own instance 获取使用 Ansible 创建的 AWS 实例的 IP 地址/属性 - Getting the IP address/attributes of the AWS instance created using Ansible 如何使用 Terraform 获取新创建的实例 ID - How to get newly created instance id using Terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM