Improve lock code.

This commit is contained in:
Paulo Gustavo Veiga 2022-03-16 23:16:34 -03:00
parent a9d091a187
commit 92dd4a7014
2 changed files with 12 additions and 18 deletions

View File

@ -7,6 +7,7 @@ import com.wisemapping.model.User;
import com.wisemapping.security.Utils;
import com.wisemapping.service.LockManager;
import com.wisemapping.service.MindmapService;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
@ -16,6 +17,7 @@ import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class UnlockOnExpireListener implements HttpSessionListener {
private static final Logger logger = Logger.getLogger(UnlockOnExpireListener.class);
@Override
public void sessionCreated(@NotNull HttpSessionEvent event) {
@ -34,10 +36,8 @@ public class UnlockOnExpireListener implements HttpSessionListener {
if (user != null) {
try {
lockManager.unlockAll(user);
} catch (LockException e) {
e.printStackTrace();
} catch (AccessDeniedSecurityException e) {
e.printStackTrace();
} catch (LockException | AccessDeniedSecurityException e) {
logger.error(e);
}
}
}

View File

@ -41,8 +41,7 @@ import java.util.concurrent.ConcurrentHashMap;
* - Usuario pierde el lock:
* - Y grabo con la misma sessions y el timestap ok.
* - Y grabo con la misma session y el timestap esta mal
* - Y grabo con distinta sessions
* -
* - Y grabo con distinta sessions
* - Usuario pierde el lock, pero intenta grabar camio
*/
@ -150,25 +149,20 @@ class LockManagerImpl implements LockManager {
}
public LockManagerImpl() {
lockInfoByMapId = new ConcurrentHashMap<Integer, LockInfo>();
lockInfoByMapId = new ConcurrentHashMap<>();
expirationTimer.schedule(new TimerTask() {
@Override
public void run() {
logger.debug("Lock expiration scheduler started. Current locks:" + lockInfoByMapId.keySet());
final List<Integer> toRemove = new ArrayList<Integer>();
final Set<Integer> mapIds = lockInfoByMapId.keySet();
for (Integer mapId : mapIds) {
final LockInfo lockInfo = lockInfoByMapId.get(mapId);
if (lockInfo.isExpired()) {
toRemove.add(mapId);
}
}
// Search for expired sessions and remove them ....
lockInfoByMapId.
keySet().
stream().
filter(mapId -> lockInfoByMapId.get(mapId).isExpired()).
forEach(mapId -> unlock(mapId));
for (Integer mapId : toRemove) {
unlock(mapId);
}
}
}, ONE_MINUTE_MILLISECONDS, ONE_MINUTE_MILLISECONDS);
}