-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request #30: SYMPHONYP-940 refactor retry handler
Merge in SYMPHONYP/symphony-java-toolkit from feature/SYMPHONYP-939-ms-teams-spring-bot-retry-messages to spring-bot-master-db * commit '3e4b60cbfa4318e45122d113b408aec181c8bf98': review comments changes done. added testcases for Message Retry. SYMPHONYP-939 implements review comments SYMPHONYP-940 refactor retry handler fixing formatting Adding roadmap Update LICENSE setting copyright name
- Loading branch information
Showing
13 changed files
with
261 additions
and
77 deletions.
There are no files selected for viewing
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
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.MessageRetryHandler; | ||
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 MessageRetryHandler 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 configured."); | ||
} 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
48 changes: 48 additions & 0 deletions
48
...rter/src/main/java/org/finos/springbot/teams/handlers/retry/BasicMessageRetryHandler.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,48 @@ | ||
package org.finos.springbot.teams.handlers.retry; | ||
|
||
import java.time.LocalDateTime; | ||
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 BasicMessageRetryHandler implements MessageRetryHandler { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(BasicMessageRetryHandler.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); | ||
} | ||
LocalDateTime time = LocalDateTime.now().plusSeconds(retryAfterInt); | ||
add(new MessageRetry(t, retryCount, retryAfterInt, time)); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
} |
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
Oops, something went wrong.