-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
lottery-consumer-service/src/main/java/com/example/lottery/config/RetryTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.lottery.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.retry.backoff.ExponentialBackOffPolicy; | ||
import org.springframework.retry.policy.SimpleRetryPolicy; | ||
import org.springframework.retry.support.RetryTemplate; | ||
|
||
/** | ||
* | ||
* @author Binnur Kurt <binnur.kurt@gmail.com> | ||
* | ||
*/ | ||
@Configuration | ||
public class RetryTemplateConfig { | ||
@Bean | ||
public RetryTemplate retryTemplate() { | ||
RetryTemplate retryTemplate = new RetryTemplate(); | ||
|
||
ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy(); | ||
exponentialBackOffPolicy.setInitialInterval(1000L); | ||
retryTemplate.setBackOffPolicy(exponentialBackOffPolicy); | ||
|
||
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); | ||
retryPolicy.setMaxAttempts(3); | ||
retryTemplate.setRetryPolicy(retryPolicy); | ||
return retryTemplate; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ce/src/main/java/com/example/lottery/service/LotteryConsumerServiceWithRetryTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.example.lottery.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.retry.RecoveryCallback; | ||
import org.springframework.retry.RetryCallback; | ||
import org.springframework.retry.support.RetryTemplate; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.example.lottery.dto.LotteryResponse; | ||
|
||
@Service | ||
public class LotteryConsumerServiceWithRetryTemplate { | ||
@Autowired | ||
private RetryTemplate retryTemplate; | ||
|
||
@Scheduled(fixedRate = 12_000) | ||
public void doSomething() { | ||
RetryCallback<LotteryResponse, Exception> retryCallback = context -> { | ||
System.err.println("Calling lottery service from LotteryConsumerServiceWithRetryTemplate..."); | ||
var rt = new RestTemplate(); | ||
var response = rt.getForObject("http://localhost:8001/lottery/api/v1/numbers?column=10", | ||
LotteryResponse.class); | ||
System.err.println(response); | ||
return response; | ||
}; | ||
RecoveryCallback<LotteryResponse> recoveryCallback = context -> { | ||
System.err.println("Calling recoveryCallback from LotteryConsumerServiceWithRetryTemplate..."); | ||
return new LotteryResponse(List.of(List.of(5, 10, 15, 20, 25, 30))); | ||
}; | ||
try { | ||
var response = retryTemplate.execute(retryCallback, recoveryCallback); | ||
System.out.println(response); | ||
} catch (Exception e) { | ||
System.err.println(e.getMessage()); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters