Created
December 5, 2017 14:59
-
-
Save odedia/1451f7ddee545383be7b7c91f3c77f6c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class RedisOperationsSessionRepository implements | |
| FindByIndexNameSessionRepository<RedisOperationsSessionRepository.RedisSession>, | |
| MessageListener { | |
| ... | |
| @SuppressWarnings("unchecked") | |
| public void onMessage(Message message, byte[] pattern) { | |
| byte[] messageChannel = message.getChannel(); | |
| byte[] messageBody = message.getBody(); | |
| if (messageChannel == null || messageBody == null) { | |
| return; | |
| } | |
| String channel = new String(messageChannel); | |
| if (channel.startsWith(getSessionCreatedChannelPrefix())) { | |
| // TODO: is this thread safe? | |
| Map<Object, Object> loaded = (Map<Object, Object>) this.defaultSerializer | |
| .deserialize(message.getBody()); | |
| handleCreated(loaded, channel); | |
| return; | |
| } | |
| String body = new String(messageBody); | |
| if (!body.startsWith(getExpiredKeyPrefix())) { | |
| return; | |
| } | |
| boolean isDeleted = channel.endsWith(":del"); | |
| if (isDeleted || channel.endsWith(":expired")) { | |
| int beginIndex = body.lastIndexOf(":") + 1; | |
| int endIndex = body.length(); | |
| String sessionId = body.substring(beginIndex, endIndex); | |
| RedisSession session = getSession(sessionId, true); | |
| if (logger.isDebugEnabled()) { | |
| logger.debug("Publishing SessionDestroyedEvent for session " + sessionId); | |
| } | |
| cleanupPrincipalIndex(session); | |
| if (isDeleted) { | |
| handleDeleted(sessionId, session); | |
| } | |
| else { | |
| handleExpired(sessionId, session); | |
| } | |
| return; | |
| } | |
| } | |
| private void handleDeleted(String sessionId, RedisSession session) { | |
| if (session == null) { | |
| publishEvent(new SessionDeletedEvent(this, sessionId)); | |
| } | |
| else { | |
| publishEvent(new SessionDeletedEvent(this, session)); | |
| } | |
| } | |
| private void handleExpired(String sessionId, RedisSession session) { | |
| if (session == null) { | |
| publishEvent(new SessionExpiredEvent(this, sessionId)); | |
| } | |
| else { | |
| publishEvent(new SessionExpiredEvent(this, session)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment