dNodeInit.catchAndRetry(); return reference.get();
/** * Returns a QNode instance if, after a maximum of X trials, we can find a port to bind it to. * The configuration passed by instance might have been modified accordingly. */ public static QNode getTestQNode(final SploutConfiguration testConfig, final IQNodeHandler handler) throws Throwable { final AtomicReference<QNode> reference = new AtomicReference<QNode>(); CatchAndRetry qNodeInit = new CatchAndRetry(java.net.BindException.class, 50) { @Override public void businessLogic() throws Throwable { QNode qNode = new QNode(); qNode.start(testConfig, handler); reference.set(qNode); } @Override public void retryLogic() { testConfig.setProperty(QNodeProperties.PORT, testConfig.getInt(QNodeProperties.PORT) + 1); } }; qNodeInit.catchAndRetry(); return reference.get(); }
public void catchAndRetry() throws Throwable { int trial = 0; boolean succeeded = false; do { try { businessLogic(); succeeded = true; } catch (Throwable t) { if (exception.isAssignableFrom(t.getClass())) { trial++; retryLogic(); } else { throw t; } } } while (!succeeded && trial < maxRetrials); } }