Search in sources :

Example 1 with SessionExpiredEvent

use of io.micronaut.session.event.SessionExpiredEvent in project redisson by redisson.

the class RedissonSessionStore method onMessage.

@Override
public void onMessage(CharSequence pattern, CharSequence channel, String body) {
    if (deletedTopic.getPatternNames().contains(pattern.toString())) {
        if (!body.contains(SESSION_PREFIX + "notification:")) {
            return;
        }
        String id = body.split(SESSION_PREFIX + "notification:")[1];
        loadSession(id, true).whenComplete((r, e) -> {
            r.ifPresent(v -> {
                eventPublisher.publishEvent(new SessionDeletedEvent(v));
            });
        });
    } else if (expiredTopic.getPatternNames().contains(pattern.toString())) {
        if (!body.contains(SESSION_PREFIX + "notification:")) {
            return;
        }
        String id = body.split(SESSION_PREFIX + "notification:")[1];
        loadSession(id, true).whenComplete((r, e) -> {
            r.ifPresent(v -> {
                eventPublisher.publishEvent(new SessionExpiredEvent(v));
            });
        });
    }
}
Also used : Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) SessionCreatedEvent(io.micronaut.session.event.SessionCreatedEvent) SessionDeletedEvent(io.micronaut.session.event.SessionDeletedEvent) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) CompositeCodec(org.redisson.codec.CompositeCodec) Primary(io.micronaut.context.annotation.Primary) SessionStore(io.micronaut.session.SessionStore) Map(java.util.Map) SessionSettings(io.micronaut.session.SessionSettings) org.redisson.api(org.redisson.api) Requires(io.micronaut.context.annotation.Requires) SessionExpiredEvent(io.micronaut.session.event.SessionExpiredEvent) MessageListener(org.redisson.api.listener.MessageListener) IntegerCodec(org.redisson.client.codec.IntegerCodec) InMemorySessionStore(io.micronaut.session.InMemorySessionStore) Logger(org.slf4j.Logger) ApplicationEventPublisher(io.micronaut.context.event.ApplicationEventPublisher) Singleton(jakarta.inject.Singleton) UUID(java.util.UUID) StringUtils(io.micronaut.core.util.StringUtils) PatternMessageListener(org.redisson.api.listener.PatternMessageListener) SessionIdGenerator(io.micronaut.session.SessionIdGenerator) Optional(java.util.Optional) Replaces(io.micronaut.context.annotation.Replaces) SessionDeletedEvent(io.micronaut.session.event.SessionDeletedEvent) SessionExpiredEvent(io.micronaut.session.event.SessionExpiredEvent)

Example 2 with SessionExpiredEvent

use of io.micronaut.session.event.SessionExpiredEvent in project micronaut-redis by micronaut-projects.

the class RedisSessionStore method message.

@Override
public void message(String pattern, String channel, String message) {
    if (message.startsWith(expiryPrefix)) {
        boolean expired = pattern.endsWith(":expired");
        if (pattern.endsWith(":del") || expired) {
            String id = message.substring(expiryPrefix.length());
            redisSortedSetAsyncCommands.zrem(activeSessionsSet, id.getBytes(charset)).whenComplete((aVoid, throwable) -> {
                if (throwable != null) {
                    if (LOG.isErrorEnabled()) {
                        LOG.error("Error removing session [" + id + "] from active sessions: " + throwable.getMessage(), throwable);
                    }
                }
            });
            findSessionInternal(id, true).whenComplete((optional, throwable) -> {
                if (throwable == null && optional.isPresent()) {
                    RedisSession session = optional.get();
                    eventPublisher.publishEvent(expired ? new SessionExpiredEvent(session) : new SessionDeletedEvent(session));
                }
            });
        }
    }
}
Also used : SessionDeletedEvent(io.micronaut.session.event.SessionDeletedEvent) RedisSession(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession) SessionExpiredEvent(io.micronaut.session.event.SessionExpiredEvent)

Aggregations

SessionDeletedEvent (io.micronaut.session.event.SessionDeletedEvent)2 SessionExpiredEvent (io.micronaut.session.event.SessionExpiredEvent)2 RedisSession (io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession)1 Primary (io.micronaut.context.annotation.Primary)1 Replaces (io.micronaut.context.annotation.Replaces)1 Requires (io.micronaut.context.annotation.Requires)1 ApplicationEventPublisher (io.micronaut.context.event.ApplicationEventPublisher)1 StringUtils (io.micronaut.core.util.StringUtils)1 InMemorySessionStore (io.micronaut.session.InMemorySessionStore)1 SessionIdGenerator (io.micronaut.session.SessionIdGenerator)1 SessionSettings (io.micronaut.session.SessionSettings)1 SessionStore (io.micronaut.session.SessionStore)1 SessionCreatedEvent (io.micronaut.session.event.SessionCreatedEvent)1 Singleton (jakarta.inject.Singleton)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 org.redisson.api (org.redisson.api)1 MessageListener (org.redisson.api.listener.MessageListener)1