-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SYMPHONYP-940 refactor retry handler
- Loading branch information
1 parent
3f2fec6
commit a9f3699
Showing
9 changed files
with
133 additions
and
62 deletions.
There are no files selected for viewing
38 changes: 28 additions & 10 deletions
38
...low-spring-boot-starter/src/main/java/org/finos/springbot/teams/TeamsScheduledConfig.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 |
---|---|---|
@@ -1,23 +1,41 @@ | ||
package org.finos.springbot.teams; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.finos.springbot.teams.handlers.TeamsResponseHandler; | ||
import org.finos.springbot.teams.handlers.retry.NoOpRetryHandler; | ||
import org.finos.springbot.teams.handlers.retry.RetryHandler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
|
||
import org.springframework.scheduling.annotation.SchedulingConfigurer; | ||
import org.springframework.scheduling.config.ScheduledTaskRegistrar; | ||
|
||
@EnableScheduling | ||
public class TeamsScheduledConfig { | ||
public class TeamsScheduledConfig implements SchedulingConfigurer { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(TeamsScheduledConfig.class); | ||
|
||
@Autowired | ||
private TeamsResponseHandler handler; | ||
|
||
@Scheduled(fixedDelay = 30, timeUnit = TimeUnit.SECONDS) | ||
//Task to run after a fixed delay. | ||
//the duration between the end of last execution and the start of next execution is fixed | ||
public void scheduleRetryMessage() { | ||
|
||
@Autowired | ||
private RetryHandler retryHandler; | ||
|
||
@Value("${teams.retry.time:30000}") | ||
private long teamsRetrySchedulerCron; | ||
|
||
@Override | ||
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { | ||
if (retryHandler instanceof NoOpRetryHandler) { | ||
LOG.info("No operation retry handler is configure"); | ||
} else { | ||
Runnable runnable = () -> scheduleRetryMessage(); | ||
scheduledTaskRegistrar.addFixedDelayTask(runnable, teamsRetrySchedulerCron); | ||
} | ||
} | ||
|
||
private void scheduleRetryMessage() { | ||
handler.retryMessage(); | ||
} | ||
} |
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
11 changes: 0 additions & 11 deletions
11
...ng-boot-starter/src/main/java/org/finos/springbot/teams/handlers/MessageRetryHandler.java
This file was deleted.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
...pringbot/teams/handlers/MessageRetry.java → ...ot/teams/handlers/retry/MessageRetry.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
49 changes: 49 additions & 0 deletions
49
...t-starter/src/main/java/org/finos/springbot/teams/handlers/retry/MessageRetryHandler.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,49 @@ | ||
package org.finos.springbot.teams.handlers.retry; | ||
|
||
import java.util.concurrent.CompletionException; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.finos.springbot.workflow.response.Response; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import com.microsoft.bot.connector.rest.ErrorResponseException; | ||
|
||
import okhttp3.ResponseBody; | ||
|
||
public abstract class MessageRetryHandler implements RetryHandler { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(MessageRetryHandler.class); | ||
|
||
@Value("${teams.retry.count:3}") | ||
private long teamsRetryCount; | ||
|
||
public boolean handleException(Response t, int retryCount, Throwable e) { | ||
if (e instanceof CompletionException | ||
&& ((CompletionException) e).getCause() instanceof ErrorResponseException) { | ||
ErrorResponseException ere = (ErrorResponseException) ((CompletionException) e).getCause(); | ||
retrofit2.Response<ResponseBody> response = ere.response(); | ||
if (response.code() == HttpStatus.TOO_MANY_REQUESTS.value() && retryCount <= teamsRetryCount) { | ||
String retryAfter = response.headers().get("Retry-After"); | ||
LOG.info("MessageRetryHandler request retryAfter {}", retryAfter); | ||
|
||
int retryAfterInt = 1;//initiate to 1 sec | ||
if(StringUtils.isNumeric(retryAfter)) { | ||
retryAfterInt = Integer.parseInt(retryAfter); | ||
} | ||
|
||
add(new MessageRetry(t, retryCount, retryAfterInt)); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected abstract void add(MessageRetry messageRetry); | ||
|
||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...boot-starter/src/main/java/org/finos/springbot/teams/handlers/retry/NoOpRetryHandler.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,20 @@ | ||
package org.finos.springbot.teams.handlers.retry; | ||
|
||
import java.util.Optional; | ||
|
||
import org.finos.springbot.workflow.response.Response; | ||
|
||
public class NoOpRetryHandler implements RetryHandler { | ||
|
||
@Override | ||
public Optional<MessageRetry> get() { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public boolean handleException(Response t, int retryCount, Throwable e) { | ||
|
||
return false; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...ing-boot-starter/src/main/java/org/finos/springbot/teams/handlers/retry/RetryHandler.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,13 @@ | ||
package org.finos.springbot.teams.handlers.retry; | ||
|
||
import java.util.Optional; | ||
|
||
import org.finos.springbot.workflow.response.Response; | ||
|
||
public interface RetryHandler { | ||
|
||
public boolean handleException(Response t, int retryCount, Throwable e); | ||
|
||
public Optional<MessageRetry> get(); | ||
|
||
} |