Skip to content

Instantly share code, notes, and snippets.

@RaviH
Created November 25, 2014 02:09
Show Gist options
  • Save RaviH/6222db0f50ab0a7b12ae to your computer and use it in GitHub Desktop.
Save RaviH/6222db0f50ab0a7b12ae to your computer and use it in GitHub Desktop.
Spring Batch Retry with Timeout
import com.rackspace.monitoring.BaseLoggingThing
import org.springframework.retry.RetryCallback
import org.springframework.retry.RetryContext
import org.springframework.retry.policy.TimeoutRetryPolicy
import org.springframework.retry.support.RetryTemplate
class TimeoutRetryExample extends BaseLoggingThing {
RetryTemplate retryTemplate = new RetryTemplate()
CarRepository carRepository
TimeoutRetryExample(com.rackspace.monitoring.atomhopper.CarRepository carRepository, int timeoutInSecs) {
this.carRepository = carRepository
TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy()
timeoutRetryPolicy.timeout = timeoutInSecs * 1000L
retryTemplate.retryPolicy = timeoutRetryPolicy
}
String getCar(final String make) {
return retryTemplate.execute(new RetryCallback() {
@Override
String doWithRetry(RetryContext retryContext) throws Exception {
// A long operation is simulated here.
sleep(1000)
return carRepository.findCarNameByMake(make)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment