0
# Timeout Strategies
1
2
Method execution timeout controls that prevent hanging operations by enforcing maximum execution time limits.
3
4
## Capabilities
5
6
### Basic Timeout
7
8
Standard timeout functionality with configurable time limits and units.
9
10
```java { .api }
11
@Timeout(
12
value = 5,
13
unit = ChronoUnit.SECONDS
14
)
15
public ReturnType timedMethod() throws Exception;
16
```
17
18
#### Parameters
19
20
- `value` - Timeout duration (default: 1)
21
- `unit` - Time unit for timeout (default: SECONDS)
22
23
#### Usage Example
24
25
```java
26
@ApplicationScoped
27
public class ExternalServiceClient {
28
29
// HTTP call with 5-second timeout
30
@Timeout(value = 5, unit = ChronoUnit.SECONDS)
31
public ApiResponse callExternalApi(String endpoint) throws TimeoutException {
32
return httpClient.get(endpoint);
33
}
34
35
// Database query with short timeout
36
@Timeout(value = 2, unit = ChronoUnit.SECONDS)
37
public List<Record> quickQuery(String sql) throws SQLException, TimeoutException {
38
return database.query(sql);
39
}
40
41
// Long-running operation with extended timeout
42
@Timeout(value = 30, unit = ChronoUnit.SECONDS)
43
public ProcessingResult processLargeFile(File file) throws TimeoutException {
44
return fileProcessor.process(file);
45
}
46
}
47
```
48
49
### Timeout with Other Strategies
50
51
Combining timeout with retry, circuit breaker, and fallback patterns.
52
53
```java { .api }
54
@Timeout(value = 3, unit = ChronoUnit.SECONDS)
55
@Retry(maxRetries = 2)
56
@Fallback(fallbackMethod = "timeoutFallback")
57
public ReturnType resilientTimedMethod();
58
```
59
60
#### Usage Example
61
62
```java
63
@ApplicationScoped
64
public class WeatherService {
65
66
// Weather API with timeout, retry, and fallback
67
@Timeout(value = 4, unit = ChronoUnit.SECONDS)
68
@Retry(maxRetries = 3, delay = 1000)
69
@Fallback(fallbackMethod = "getDefaultWeather")
70
public WeatherData getCurrentWeather(String city) throws TimeoutException {
71
return weatherApiClient.fetchWeather(city);
72
}
73
74
public WeatherData getDefaultWeather(String city) {
75
return WeatherData.defaultFor(city);
76
}
77
78
// Payment processing with strict timeout
79
@Timeout(value = 10, unit = ChronoUnit.SECONDS)
80
@CircuitBreaker(requestVolumeThreshold = 5, failureRatio = 0.3)
81
public PaymentResult processPayment(PaymentRequest request) throws TimeoutException {
82
return paymentGateway.processPayment(request);
83
}
84
}
85
```
86
87
## Types
88
89
### Timeout Core Types
90
91
```java { .api }
92
// Time units for timeout configuration
93
enum ChronoUnit {
94
NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS, DAYS
95
}
96
97
// Timeout exception
98
class TimeoutException extends Exception {
99
public TimeoutException(String message);
100
public TimeoutException(String message, Throwable cause);
101
}
102
```