New to Scala, and now I'm looking for a way to implement the following code on it:
@Override public void store(InputStream source, String destination, long size) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(size); final PutObjectRequest request = new PutObjectRequest( this.configuration.getBucket(), destination, source, metadata); new RetryableService(3) { @Override public void call() throws Exception { getClient().putObject(request); } }; }
What would be the best way to implement the same functionality as RetryableService, but in Scala?
Basically, he calls the method a call N times, if all of them fail, then an exception occurs, if they succeed, he passes. This does not return anything, but then I have another version that allows me to return a value (so I have two classes in Java), and I believe that I can do with one class / function in Scala.
Any ideas?
EDIT
The current implementation in java is as follows:
public abstract class RetryableService { private static final JobsLogger log = JobsLogger .getLogger(RetryableService.class); private int times; public RetryableService() { this(3); } public RetryableService(int times) { this.times = times; this.run(); } private void run() { RuntimeException lastExceptionParent = null; int x = 0; for (; x < this.times; x++) { try { this.call(); lastExceptionParent = null; break; } catch (Exception e) { lastExceptionParent = new RuntimeException(e); log.errorWithoutNotice( e, "Try %d caused exception %s", x, e.getMessage() ); try { Thread.sleep( 5000 ); } catch (InterruptedException e1) { log.errorWithoutNotice( e1, "Sleep inside try %d caused exception %s", x, e1.getMessage() ); } } } try { this.ensure(); } catch (Exception e) { log.error(e, "Failed while ensure inside RetryableService"); } if ( lastExceptionParent != null ) { throw new IllegalStateException( String.format( "Failed on try %d of %s", x, this ), lastExceptionParent); } } public void ensure() throws Exception {
java design coding-style scala functional-programming
Maurício Linhares Oct 28 '11 at 2:45 a.m. 2011-10-28 14:45
source share