1.
우선 라이브러리 부터 추가.
maven > pom.xml
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.0.3.RELEASE</version>
</dependency>
2.
실제 적용 코드
> blabla.java
public class blabla implements InitializingBean {
static final String KEY = "testKey";
private RetryTemplate retryTemplate;
private RetryCallback<Long> retryCallback;
private Client client;
@Autowired
public blabla(Client client) {
this.client= client;
}
public void setRetryTemplate(RetryTemplate retryTemplate) {
this.retryTemplate = retryTemplate;
}
@Override
public void afterPropertiesSet() throws Exception {
retryTemplate = new RetryTemplate();
// 전체 시도 시간 기준 ex) 3초 동안 횟수 제한 없이 시도한다.
// TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy();
// timeoutRetryPolicy.setTimeout(3000L);
// 전체 시도 횟수 기준 ex) 3회 시도 한다.
int maxAttempts = 3;
// Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<Class<? extends Throwable>, Boolean>();
// retryableExceptions.put(Exception.class, true);
// SimpleRetryPolicy aa = new SimpleRetryPolicy(maxAttempts, retryableExceptions);
SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(maxAttempts);
retryTemplate.setRetryPolicy(simpleRetryPolicy);
retryCallback = new RetryCallback<Long>() {
public Long doWithRetry(RetryContext context) throws RuntimeException {
return getNewLogNo();
}
};
}
/**
* @return
*/
Long getNewNo() {
final Long newNo = client.increment(KEY);
if (newNo == null) {
throw new RuntimeException("getNewNo : new no is null.");
}
return newNo;
}
/**
* @return
*/
public Long getNewNoWithRetry() throws RuntimeException {
return retryTemplate.execute(retryCallback);
}
'emotional developer > detect-Java' 카테고리의 다른 글
ibatis iterate , mybatis foreach 동적쿼리 비교. (0) | 2014.04.30 |
---|---|
java 타입형 interface. (0) | 2014.02.24 |
eclipse reverse engineering plugin (0) | 2014.01.06 |