Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.66 KB | None | 0 0
  1. package com.events.util.batchexec
  2.  
  3. /**
  4.  * @author pedro
  5.  */
  6. class BatchExecImpl(retries: Int) extends BatchExec{
  7.   def exec(action: () => Unit) : Unit = {
  8.     retry[Unit](retries, action)
  9.   }
  10.  
  11.   def exec[T](action: () => T) : T = {
  12.     return retry[T](retries, action)
  13.   }
  14.  
  15.   def retry[T](retries: Int, action: () => T) : T = {
  16.     var lastException : Throwable = null
  17.     for(i <- 0 to retries){
  18.       try{
  19.         return action();  
  20.       }
  21.       catch{
  22.         case ex: RetryException => {
  23.           lastException = ex.getCause()
  24.         }
  25.         case default : Throwable => throw default
  26.       }
  27.     }
  28.    
  29.     throw lastException
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement