Home > other >  Is this instance created only once in my example?
Is this instance created only once in my example?

Time:01-10

I have the following code. In the other class, I tried to create the S3ClientClass2 object as val s3 = new S3ClientClass2(). After creating the s3, then calling the readFromS3 method for every single request. In this scenario, I am wondering that the amazonS3Client is created only once or created many times for every request. 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())
  }
}

CodePudding user response:

yes, lazy val is initialised only once when it is first used. That means, the first time you use amazonS3Client the getS3Client method will be called, every subsequent usage of amazonS3Client will use the cached value.

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:

  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. Also, what is the expected behaviour of this function when exception is thrown in the try block? In the current design it will rethrow it and escalate up your call stack.

  • Related