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

View File

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