ApexAdvancedcalloutsintegrationbatch
Design Apex integration with OAuth token refresh in long-running batch
Real World Scenario
Batch job processing 8 hours hits expired OAuth token mid-execute causing thousands of failed callouts until manual restart.
Expected Answer
• Token refresh utility checks expiry before each callout chunk
• Store token and expiry in Named Credential or custom secure store
• Catch 401 response trigger refresh and retry same request once
• Stateful batch stores refreshed token across execute invocations
• Never hardcode tokens in Apex—Named Credential auth provider flow
• Test token expiry simulation with mock 401 then 200 sequence
• Finish alerts if token refresh fails stopping batch gracefully
Follow-Up Questions & Answers
Click to expand — each follow-up includes a direct, interview-ready answer
Direct answer: Token refresh utility checks expiry before each callout chunk Also consider: Store token and expiry in Named Credential or custom secure store In practice: Catch 401 response trigger refresh and retry same request once Optimize for scale and operational observability.
Architect Perspective
Long batch jobs must handle token lifecycle—not assume token valid for entire job duration.