Skip to content

Commit

Permalink
Add ability to disable strategies via message property
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Plotnikov committed Dec 12, 2023
1 parent efaaed7 commit 58c24f3
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main/java/com/exactpro/th2/FixHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public class FixHandler implements AutoCloseable, IHandler {
private static final String STRING_MSG_TYPE = "MsgType";
private static final String REJECT_REASON = "Reject reason";
private static final String UNGRACEFUL_DISCONNECT_PROPERTY = "ungracefulDisconnect";
private static final String ENABLE_STRATEGIES_PROPERTY = "enableStrategy";
private static final String DISABLE_STRATEGIES_PROPERTY = "disableStrategy";
private static final String STUBBING_VALUE = "XXX";
private static final String SPLIT_SEND_TIMESTAMPS_PROPERTY = "BufferSlicesSendingTimes";
private static final String STRATEGY_EVENT_TYPE = "StrategyState";
Expand All @@ -209,6 +211,7 @@ public class FixHandler implements AutoCloseable, IHandler {
private final AtomicBoolean sessionActive = new AtomicBoolean(true);
private final AtomicBoolean enabled = new AtomicBoolean(false);
private final AtomicBoolean connStarted = new AtomicBoolean(false);
private final AtomicBoolean strategiesEnabled = new AtomicBoolean(true);
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
private final IHandlerContext context;
private final InetSocketAddress address;
Expand Down Expand Up @@ -348,6 +351,18 @@ public CompletableFuture<MessageID> send(@NotNull ByteBuf body, @NotNull Map<Str
return CompletableFuture.completedFuture(null);
}

if(properties.containsKey(ENABLE_STRATEGIES_PROPERTY)) {
strategiesEnabled.set(true);
context.send(CommonUtil.toEvent("Enabled strategies using message property."));
return CompletableFuture.completedFuture(null);
}

if(properties.containsKey(DISABLE_STRATEGIES_PROPERTY)) {
strategiesEnabled.set(false);
context.send(CommonUtil.toEvent("Disabled strategies using message property."));
return CompletableFuture.completedFuture(null);
}

boolean isUngracefulDisconnect = Boolean.getBoolean(properties.get(UNGRACEFUL_DISCONNECT_PROPERTY));
if(isLogout) {
context.send(CommonUtil.toEvent(String.format("Closing session %s. Is graceful disconnect: %b", channel.getSessionAlias(), !isUngracefulDisconnect)));
Expand Down Expand Up @@ -1241,7 +1256,7 @@ private String encrypt(String password, String encryptKey, String encryptAlgo, S
public void onClose(@NotNull IChannel channel) {
enabled.set(false);
activeLogonExchange.set(false);
if(passwordManager != null) passwordManager.poll();
if(passwordManager != null && !strategiesEnabled.get()) passwordManager.poll();
strategy.getOnCloseHandler().close();
cancelFuture(heartbeatTimer);
cancelFuture(testRequestTimer);
Expand Down Expand Up @@ -2038,6 +2053,12 @@ private void defaultCleanupHandler() {}

// <editor-fold desc="strategies scheduling and cleanup">
private void applyNextStrategy() {
if(!strategiesEnabled.get()) {
LOGGER.info("Strategies disabled. New strategy will not be applied. Trying again in 30 seconds.");
strategy.resetStrategyAndState(RuleConfiguration.Companion.defaultConfiguration());
executorService.schedule(this::applyNextStrategy, Duration.of(30, ChronoUnit.SECONDS).toSeconds(), TimeUnit.SECONDS);
return;
}
LOGGER.info("Cleaning up current strategy {}", strategy.getState().getType());
LOGGER.info("Started waiting for recovery finish.");
while (activeRecovery.get()) {
Expand Down

0 comments on commit 58c24f3

Please sign in to comment.