
com.xceptance.common.io.IoActionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xlt Show documentation
Show all versions of xlt Show documentation
XLT (Xceptance LoadTest) is an extensive load and performance test tool developed and maintained by Xceptance.
package com.xceptance.common.io;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An {@link IoActionHandler} runs an {@link IoAction}, and if that action fails with an {@link IOException} (or a
* subclass), repeats the action until the action eventually succeeds or the maximum number of retries is reached.
*/
public class IoActionHandler
{
/**
* Functional interface describing an action that performs I/O and may fail with {@link IOException}.
*
* @param
* the type of the result of the action
*/
@FunctionalInterface
public interface IoAction
{
public T run() throws IOException;
}
private static final Logger log = LoggerFactory.getLogger(IoActionHandler.class);
/**
* The maximum number of retries.
*/
private final int maxRetries;
/**
* Creates a new {@link IoActionHandler} initialized with the given number of retries.
*
* @param maxRetries
* the maximum number of retries
*/
public IoActionHandler(final int maxRetries)
{
this.maxRetries = maxRetries;
}
/**
* Runs the passed {@link IoAction} performing retries if needed.
*
* @param action
* the action to perform
* @return the result of the action upon successful attempt
* @throws IOException
* the exception thrown by the underlying action if the maximum number of retries was reached
*/
public T run(final IoAction action) throws IOException
{
int remainingRetries = maxRetries;
while (true)
{
try
{
return action.run();
}
catch (final IOException e)
{
if (remainingRetries > 0)
{
log.debug("Retry action because of: {}", e.toString());
remainingRetries--;
}
else
{
throw e;
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy