简体   繁体   中英

Scala : Retry with try/catch for exception handling

I am trying to add a retry logic for JSON conversion. When converting an object to json, I am retrying for 3 times if there is any exception. I am doing :

   var mapper = new ObjectMapper() with ScalaObjectMapper
  intializeMapper( )

  def intializeMapper() = {
    // jackson library does not support seralization and deserialization of
    // of scala classes like List and Map, this is needed to support it
    mapper.registerModule( DefaultScalaModule )
    // enables parsing of NaN. Enabling it here as JsonUtil class currently in
    // use supports it.
    mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true )
    mapper.setSerializationInclusion(Include.NON_NULL)
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

  } 


 def getPersonRDD(result: DataFrame): RDD[(String, String)] = {

    val finalValue = result.rdd.map({
      r =>
        val customerId = r.getAs[String](CUSTOMER_ID)
        val itemId = r.getAs[Map[String, Int]](ITEM_ID)
        val itemName = r.getAs[Map[String, Int]](ITEM_NAME)


        val person = Person(itemId, itemName)

        val jsonString = toJson(person)

        (customerId, jsonString)

    })

    return finalValue
  }

  def fromJson(json: String, clazz: Class[_]) = {
    mapper.readValue(json, clazz)
  }

  def toJson(value: Any): String = {

    var jsonString: String = " "
    jsonString = mapper.writeValueAsString(value)

    try {
      fromJson(jsonString, clazz)
      return jsonString
    } catch {
      case Exception => {

        publishMetrics(PARSING_EXCEPTION, 1.0)
        val result = util.Try(retry() {
          jsonString = mapper.writeValueAsString(value)
          val features = fromJson(jsonString, clazz)
        })

        result match {
          case util.Success(value) => jsonString

          case util.Failure(error) => {
            log.error("Error while parsing JSON " + jsonString)
            return jsonString
          }

        }

      }
    }

  }

  // Returning T, throwing the exception on failure
  @annotation.tailrec
  def retry[T](n: Int = 3)(fn: => T): T = {
    util.Try {
      fn
    } match {
      case util.Success(x) => x
      case _ if n > 1 => retry(n - 1)(fn)
      case util.Failure(e) => throw e
    }
  }

case class Person(itemId: Map[String, Int], itemName: Map[String, Int]) extends Serializable

Is this correct ? I am new to Scala. Can someone suggest me if there is any better way for achieving this ? Is there predefined retry logic available in Scala ? The reason I am trying to add retry logic for JSON conversion is due to Jackson version I use(which I can't change for now), sometimes my writeValueAsString results in incomplete JSON.

You retry function seems correct. The only flaw I can think of is that if you expect something would fail it's better just make the return type Try[T] , so you can handle it outside in the scala way.

Here is one of my implementation:

def retry[T](n: Int)(block: => T): Try[T] = {
  val stream = Stream.fill(n)(Try(block))
  stream find (_.isSuccess) getOrElse stream.head
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM