Can someone please give a sample Java code (Not XML config) for adding retry on Spring Integration SFTP Outbound Gateway for a file upload ? I know it should be RequestHandlerRetryAdvice, but how do I add it to annotation of Spring Integration SFTP Outbound Gateway ?
CodePudding user response:
See @ServiceActivator.adviceChain
:
/**
* Specify a "chain" of {@code Advice} beans that will "wrap" the message handler.
* Only the handler is advised, not the downstream flow.
* @return the advice chain.
*/
String[] adviceChain() default { };
So, you just declared a bean for the RequestHandlerRetryAdvice
and use its name in that attribute. For example with cache advice I have this test config:
@Bean
public CacheRequestHandlerAdvice cachePutAndEvictAdvice() {
CacheRequestHandlerAdvice cacheRequestHandlerAdvice = new CacheRequestHandlerAdvice();
cacheRequestHandlerAdvice.setKeyExpressionString("payload");
CachePutOperation.Builder cachePutBuilder = new CachePutOperation.Builder();
cachePutBuilder.setCacheName(TEST_PUT_CACHE);
CacheEvictOperation.Builder cacheEvictBuilder = new CacheEvictOperation.Builder();
cacheEvictBuilder.setCacheName(TEST_CACHE);
cacheRequestHandlerAdvice.setCacheOperations(cachePutBuilder.build(), cacheEvictBuilder.build());
return cacheRequestHandlerAdvice;
}
@ServiceActivator(inputChannel = "serviceChannel", outputChannel = "nullChannel",
adviceChain = "cachePutAndEvictAdvice")
public Message<?> service(Message<?> message) {
return message;
}
CodePudding user response:
Spring Boot has retry feature. Need to create a RetryTemplate
@Configuration
@EnableRetry
public class EncryptionLoadCacheRetryTemplate {
@Value("${<env variable>:3}")
String maxRetryAttempts;
@Value("${<env variable>:5000}")
String backoffPeriod;
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(Integer.parseInt(backoffPeriod));
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(Integer.parseInt(maxRetryAttempts));
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
}
Then use like below:
@Autowired
private RetryTemplate retryTemplate;
retryTemplate.execute(context -> <some method to execute>, context -> {
<some method to handle failure>(context);
return true;
});