fixed ping batch strategy

This commit is contained in:
anhefti 2023-11-16 14:16:47 +01:00
parent 01f3517989
commit fda6c80eb4
2 changed files with 4 additions and 59 deletions

View file

@ -1,28 +0,0 @@
/*
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package ch.ethz.seb.sebserver.webservice.servicelayer.session;
/** Defines a SEB Client connection ping handling strategy.
* The strategy may be different in case of stand-alone or distributed SEB Server setup. */
public interface PingHandlingStrategy {
/** Initializes the ping handling for a new SEB Connection.
*
* @param connectionId the SEB Connection identifier
* @param connectionToken the SEB Connection token */
void initForConnection(Long connectionId, String connectionToken);
/** Used to notify a ping from a SEB Client connection.
*
* @param connectionToken the SEB Client connection token
* @param timestamp the ping time-stamp
* @param pingNumber the ping number */
void notifyPing(final String connectionToken, final long timestamp, final int pingNumber);
}

View file

@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
@ -40,41 +41,21 @@ public class SEBClientPingBatchService implements SEBClientPingService {
private final ExamSessionCacheService examSessionCacheService;
private final SEBClientInstructionService sebClientInstructionService;
private final ThreadPoolTaskScheduler threadPoolTaskScheduler;
private final long schendulerInterval;
private final Set<String> pingKeys = new HashSet<>();
private final Map<String, String> pings = new ConcurrentHashMap<>();
private final Map<String, String> instructions = new ConcurrentHashMap<>();
private ScheduledFuture<?> scheduleAtFixedRate = null;
public SEBClientPingBatchService(
final ExamSessionCacheService examSessionCacheService,
final SEBClientInstructionService sebClientInstructionService,
final ThreadPoolTaskScheduler threadPoolTaskScheduler,
@Value("${sebserver.webservice.api.exam.session.ping.batch.interval:500}") final long schendulerInterval) {
final SEBClientInstructionService sebClientInstructionService) {
this.examSessionCacheService = examSessionCacheService;
this.sebClientInstructionService = sebClientInstructionService;
this.threadPoolTaskScheduler = threadPoolTaskScheduler;
this.schendulerInterval = schendulerInterval;
}
void init() {
if (this.scheduleAtFixedRate == null) {
log.info(
"Initialize SEBClientPingBatchService for schedule batch update at a rate of {} milliseconds",
this.schendulerInterval);
this.scheduleAtFixedRate = this.threadPoolTaskScheduler.scheduleAtFixedRate(
() -> processPings(),
this.schendulerInterval);
}
}
//@Scheduled(fixedDelayString = "${sebserver.webservice.api.exam.session.ping.batch.interval:500}")
@Scheduled(fixedDelayString = "${sebserver.webservice.api.exam.session.ping.batch.interval:500}")
public void processPings() {
if (this.pings.isEmpty()) {
return;
@ -160,12 +141,4 @@ public class SEBClientPingBatchService implements SEBClientPingService {
this.instructions.put(connectionToken, instructionJSON);
}
}
@PreDestroy
protected void shutdown() {
if (this.scheduleAtFixedRate != null) {
this.scheduleAtFixedRate.cancel(true);
}
}
}