Objective:- Retrieve json object from aws s3 bucket. Retry on failure. If retry attempts are exhausted, send the error message to azure's event hub.
//Retry function
def retry[T](n: Int, id: String)(fn: => T): Option[T] = {
Try(fn) match {
case Success(x) => Some(x)
case Failure(e) => {
Thread.sleep(1000)
if (n > 1) {
retry(n - 1, id)(fn)
} else {
val eMessage = "TransactionId =" id " : ".concat(e.toString())
SendMessage(eMessage, event_hub_client)
None
}
}
}
}
//Main function (with above retry) to retrieve objects from aws s3 bucket
def getS3Object(s3ObjectName: String, s3Client: AmazonS3, evtClient: EventHubClient): String = {
retry(2,s3ObjectName) {
val inputS3Stream = s3Client.getObject("my_s3_bucket", s3ObjectName).getObjectContent
val inputS3String = IOUtils.toString(inputS3Stream, "UTF-8")
return inputS3String
}
}
I get the below compilation failed message:-
error: type mismatch
found : Option[Nothing]
required: String
retry2(2,s3ObjectName)
How to declare the return types of the retry and main functions so that they match?
CodePudding user response:
In terms of types retry
look's okay. You need to adapt your other method.
Either make it return a Option[String]
or use getOrElse(..)
if you want to fallback to some other value.
def getS3Object(s3ObjectName: String, s3Client: AmazonS3, evtClient: EventHubClient): Option[String] = {
retry(2,s3ObjectName) {
val inputS3Stream = s3Client.getObject("my_s3_bucket", s3ObjectName).getObjectContent
val inputS3String = IOUtils.toString(inputS3Stream, "UTF-8")
inputS3String
}
}
Notice the removal of return
keyword which is useless and doesn't do what you think it does (I'll let you search on this topic by yourself).