Build fault-tolerant network services
—
Method execution timeout controls that prevent hanging operations by enforcing maximum execution time limits.
Standard timeout functionality with configurable time limits and units.
@Timeout(
value = 5,
unit = ChronoUnit.SECONDS
)
public ReturnType timedMethod() throws Exception;value - Timeout duration (default: 1)unit - Time unit for timeout (default: SECONDS)@ApplicationScoped
public class ExternalServiceClient {
// HTTP call with 5-second timeout
@Timeout(value = 5, unit = ChronoUnit.SECONDS)
public ApiResponse callExternalApi(String endpoint) throws TimeoutException {
return httpClient.get(endpoint);
}
// Database query with short timeout
@Timeout(value = 2, unit = ChronoUnit.SECONDS)
public List<Record> quickQuery(String sql) throws SQLException, TimeoutException {
return database.query(sql);
}
// Long-running operation with extended timeout
@Timeout(value = 30, unit = ChronoUnit.SECONDS)
public ProcessingResult processLargeFile(File file) throws TimeoutException {
return fileProcessor.process(file);
}
}Combining timeout with retry, circuit breaker, and fallback patterns.
@Timeout(value = 3, unit = ChronoUnit.SECONDS)
@Retry(maxRetries = 2)
@Fallback(fallbackMethod = "timeoutFallback")
public ReturnType resilientTimedMethod();@ApplicationScoped
public class WeatherService {
// Weather API with timeout, retry, and fallback
@Timeout(value = 4, unit = ChronoUnit.SECONDS)
@Retry(maxRetries = 3, delay = 1000)
@Fallback(fallbackMethod = "getDefaultWeather")
public WeatherData getCurrentWeather(String city) throws TimeoutException {
return weatherApiClient.fetchWeather(city);
}
public WeatherData getDefaultWeather(String city) {
return WeatherData.defaultFor(city);
}
// Payment processing with strict timeout
@Timeout(value = 10, unit = ChronoUnit.SECONDS)
@CircuitBreaker(requestVolumeThreshold = 5, failureRatio = 0.3)
public PaymentResult processPayment(PaymentRequest request) throws TimeoutException {
return paymentGateway.processPayment(request);
}
}// Time units for timeout configuration
enum ChronoUnit {
NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS, DAYS
}
// Timeout exception
class TimeoutException extends Exception {
public TimeoutException(String message);
public TimeoutException(String message, Throwable cause);
}Install with Tessl CLI
npx tessl i tessl/maven-io-quarkus--quarkus-smallrye-fault-tolerance