Skip to content

Delivery Strategies

The Java SDK supports two delivery strategies for receiving emails. This guide covers when to use each strategy and how to configure them for optimal performance.

StrategyDescription
SSEReal-time via Server-Sent Events (default)
POLLINGPeriodic HTTP polling
AspectSSEPolling
LatencyInstant (~0ms)Poll interval (1-30s)
ConnectionPersistentPer request
Resource usageLowerHigher
Firewall friendlySometimesAlways
RecoveryAuto-reconnectNatural
Best forReal-time needsCI/CD, firewalls

Real-time email delivery via Server-Sent Events. Uses a persistent HTTP connection for instant notifications.

ClientConfig config = ClientConfig.builder()
.apiKey(apiKey)
.strategy(StrategyType.SSE)
.sseReconnectInterval(Duration.ofSeconds(2))
.sseMaxReconnectAttempts(10)
.build();
  • Instant email notification (~0ms latency)
  • Lower server load (single persistent connection)
  • Efficient resource usage
  • Automatic reconnection on temporary failures
  • May be blocked by some firewalls/proxies
  • Requires persistent connection
  • May not work in all CI environments
  • Connection limits in some environments
OptionTypeDefaultDescription
sseReconnectIntervalDuration2sTime between reconnection attempts
sseMaxReconnectAttemptsint10Max reconnection attempts before failure
  • Development environments
  • Real-time monitoring applications
  • Interactive testing
  • Environments with stable connections
  • When minimal latency is critical

SSE automatically reconnects on connection failures with exponential backoff:

ClientConfig.builder()
.apiKey(apiKey)
.strategy(StrategyType.SSE)
.sseReconnectInterval(Duration.ofSeconds(2)) // Base interval
.sseMaxReconnectAttempts(10) // Fails after 10 attempts
.build();

Reconnection Flow:

  1. Connection lost
  2. Wait sseReconnectInterval * 2^attempts (exponential backoff)
  3. Attempt reconnection
  4. If failed, increment attempt counter
  5. Reset counter on successful reconnection
  6. Throw SseException after sseMaxReconnectAttempts failures

Periodic polling for new emails. Uses standard HTTP requests at regular intervals.

ClientConfig config = ClientConfig.builder()
.apiKey(apiKey)
.strategy(StrategyType.POLLING)
.pollInterval(Duration.ofSeconds(2))
.maxBackoff(Duration.ofSeconds(30))
.backoffMultiplier(1.5)
.jitterFactor(0.3)
.build();
  • Works everywhere (no firewall issues)
  • Firewall friendly (standard HTTP requests)
  • Simple request/response model
  • Natural recovery from failures
  • No persistent connections required
  • Higher latency (depends on poll interval)
  • More API requests
  • Slightly higher resource usage
  • Trade-off between latency and request frequency
OptionTypeDefaultDescription
pollIntervalDuration2sBase polling interval
maxBackoffDuration30sMaximum backoff duration
backoffMultiplierdouble1.5Exponential backoff multiplier
jitterFactordouble0.3Random jitter factor (0-1)
  • CI/CD environments
  • Behind restrictive firewalls
  • When reliability is critical
  • Environments with connection limits
  • Corporate networks with proxy restrictions

Polling uses exponential backoff with jitter to prevent thundering herd:

nextInterval = min(
pollInterval * (backoffMultiplier ^ attempts),
maxBackoff
) * (1 + random(-jitterFactor, jitterFactor))

Example sequence (2s base, 1.5x multiplier, 30s max):

AttemptInterval
12s
23s
34.5s
46.75s
510.1s
Max30s

Jitter adds randomness (±30% by default) to spread out requests.

┌─────────────────────────────────────┐
│ Choose Strategy │
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│ Need real-time (<100ms latency)? │
└─────────────────────────────────────┘
│ │
Yes No
│ │
▼ ▼
┌───────────────┐ ┌───────────────────┐
│ Environment │ │ Use POLLING │
│ supports SSE? │ │ │
└───────────────┘ └───────────────────┘
│ │
Yes No
│ │
▼ ▼
┌───────┐ ┌───────────────────────────┐
│ SSE │ │ Use POLLING │
│ │ │ │
└───────┘ └───────────────────────────┘

Quick Decision:

EnvironmentRecommended Strategy
DevelopmentSSE
CI/CDPOLLING
Production testsSSE
Behind firewallPOLLING
Real-time requirementsSSE
// Fastest feedback with real-time updates
ClientConfig config = ClientConfig.builder()
.apiKey(apiKey)
.strategy(StrategyType.SSE)
.sseReconnectInterval(Duration.ofSeconds(2))
.build();
// Most reliable in containerized/restricted environments
ClientConfig config = ClientConfig.builder()
.apiKey(System.getenv("VAULTSANDBOX_API_KEY"))
.strategy(StrategyType.POLLING)
.pollInterval(Duration.ofSeconds(2))
.waitTimeout(Duration.ofSeconds(60))
.build();
// SSE for real-time delivery
ClientConfig config = ClientConfig.builder()
.apiKey(apiKey)
.strategy(StrategyType.SSE)
.sseReconnectInterval(Duration.ofSeconds(2))
.sseMaxReconnectAttempts(5)
.build();
// Frequent polling for high email volume scenarios
ClientConfig config = ClientConfig.builder()
.apiKey(apiKey)
.strategy(StrategyType.POLLING)
.pollInterval(Duration.ofSeconds(1)) // Frequent checks
.maxBackoff(Duration.ofSeconds(5)) // Quick recovery
.build();
// Fast reconnection for low-latency requirements
ClientConfig config = ClientConfig.builder()
.apiKey(apiKey)
.strategy(StrategyType.SSE)
.sseReconnectInterval(Duration.ofSeconds(1))
.sseMaxReconnectAttempts(5)
.build();
import com.vaultsandbox.client.exception.SseException;
try {
Email email = inbox.waitForEmail(Duration.ofSeconds(30));
} catch (SseException e) {
// SSE connection permanently failed
System.err.println("SSE failed: " + e.getMessage());
// Consider switching to POLLING strategy
}
import com.vaultsandbox.client.exception.TimeoutException;
try {
Email email = inbox.waitForEmail(Duration.ofSeconds(30));
} catch (TimeoutException e) {
// Email not received within timeout
System.err.println("Timeout waiting for email");
}
IssuePossible CauseSolution
SSE not connectingFirewall blockingUse POLLING
High latencySlow poll intervalDecrease pollInterval
Missed emailsBackoff too aggressiveReduce maxBackoff
Too many requestsPoll interval too shortIncrease pollInterval
Frequent reconnectsUnstable networkIncrease sseReconnectInterval
Connection timeoutsNetwork issuesIncrease httpTimeout
  1. Use SSE for real-time needs - Default strategy with instant delivery
  2. Use POLLING in CI/CD - Most reliable in restricted environments
  3. Configure timeouts appropriately - Match your test requirements
  4. Test both strategies - Ensure code works with either strategy
  5. Set reasonable reconnect limits - Balance retry vs fail-fast
  6. Use jitter in polling - Prevents thundering herd on server