val random = new Random(); def serviceWithBadConnection(): String = { if (random.nextFloat() < 0.8) throw new RuntimeException; "Data" } def call[T](retries: Int)(block: => T): Option[T] = { if (retries == 0) None else { try { Some(block); } catch { case _ => { println("Retrying!"); call(retries - 1)(block); } } } } val result = call(retries = 10) { val data = serviceWithBadConnection(); "Enhanced " + data; } println(result);
Monday, September 24, 2012
Call with Retry
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment