HHH-6940 - Ability (easier) to run bits of code in a timeout protected way

This commit is contained in:
Steve Ebersole 2012-01-06 08:21:04 -06:00
parent 86f3f3c347
commit 9ca985ccd1
2 changed files with 31 additions and 0 deletions

View File

@ -29,6 +29,7 @@ package org.hibernate.testing.async;
public class ExecutableAdapter implements Runnable { public class ExecutableAdapter implements Runnable {
private final Executable executable; private final Executable executable;
private boolean isDone; private boolean isDone;
private Throwable error;
public ExecutableAdapter(Executable executable) { public ExecutableAdapter(Executable executable) {
this.executable = executable; this.executable = executable;
@ -38,14 +39,38 @@ public class ExecutableAdapter implements Runnable {
return isDone; return isDone;
} }
public void reThrowAnyErrors() {
if ( error != null ) {
if ( RuntimeException.class.isInstance( error ) ) {
throw RuntimeException.class.cast( error );
}
else if ( Error.class.isInstance( error ) ) {
throw Error.class.cast( error );
}
else {
throw new ExceptionWrapper( error );
}
}
}
@Override @Override
public void run() { public void run() {
isDone = false; isDone = false;
error = null;
try { try {
executable.execute(); executable.execute();
} }
catch (Throwable t) {
error = t;
}
finally { finally {
isDone = true; isDone = true;
} }
} }
public static class ExceptionWrapper extends RuntimeException {
public ExceptionWrapper(Throwable cause) {
super( cause );
}
}
} }

View File

@ -25,10 +25,14 @@ package org.hibernate.testing.async;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.jboss.logging.Logger;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class TimedExecutor { public class TimedExecutor {
private static final Logger log = Logger.getLogger( TimedExecutor.class );
private final long timeOut; private final long timeOut;
private final int checkMilliSeconds; private final int checkMilliSeconds;
@ -63,5 +67,7 @@ public class TimedExecutor {
catch (InterruptedException ignore) { catch (InterruptedException ignore) {
} }
} while ( !adapter.isDone() ); } while ( !adapter.isDone() );
adapter.reThrowAnyErrors();
} }
} }