Search in sources :

Example 1 with SessionExpirationListener

use of org.wildfly.clustering.web.session.SessionExpirationListener in project wildfly by wildfly.

the class HotRodSessionFactory method expired.

@ClientCacheEntryExpired
public void expired(ClientCacheEntryCustomEvent<byte[]> event) {
    RemoteCache<SessionCreationMetaDataKey, SessionCreationMetaDataEntry<L>> creationMetaDataCache = this.creationMetaDataCache;
    RemoteCache<SessionAccessMetaDataKey, SessionAccessMetaData> accessMetaDataCache = this.accessMetaDataCache;
    ImmutableSessionMetaDataFactory<CompositeSessionMetaDataEntry<L>> metaDataFactory = this.metaDataFactory;
    ImmutableSessionAttributesFactory<V> attributesFactory = this.attributesFactory;
    Remover<String> attributesRemover = this.attributesRemover;
    Collection<SessionExpirationListener> listeners = this.listeners;
    boolean nearCacheEnabled = this.nearCacheEnabled;
    Runnable task = new Runnable() {

        @Override
        public void run() {
            ByteBuffer buffer = ByteBuffer.wrap(event.getEventData());
            byte[] key = new byte[UnsignedNumeric.readUnsignedInt(buffer)];
            buffer.get(key);
            byte[] value = buffer.remaining() > 0 ? new byte[UnsignedNumeric.readUnsignedInt(buffer)] : null;
            if (value != null) {
                buffer.get(value);
            }
            Marshaller marshaller = creationMetaDataCache.getRemoteCacheManager().getConfiguration().marshaller();
            String id = null;
            try {
                SessionCreationMetaDataKey creationKey = (SessionCreationMetaDataKey) marshaller.objectFromByteBuffer(key);
                id = creationKey.getId();
                @SuppressWarnings("unchecked") SessionCreationMetaDataEntry<L> creationEntry = (value != null) ? (SessionCreationMetaDataEntry<L>) marshaller.objectFromByteBuffer(value) : new SessionCreationMetaDataEntry<>(new SimpleSessionCreationMetaData(Instant.EPOCH));
                // Ensure entry is removed from near cache
                if (nearCacheEnabled) {
                    creationMetaDataCache.withFlags(Flag.SKIP_LISTENER_NOTIFICATION).remove(creationKey);
                }
                SessionAccessMetaData accessMetaData = accessMetaDataCache.withFlags(Flag.FORCE_RETURN_VALUE).remove(new SessionAccessMetaDataKey(id));
                if (accessMetaData != null) {
                    V attributesValue = attributesFactory.findValue(id);
                    if (attributesValue != null) {
                        ImmutableSessionMetaData metaData = metaDataFactory.createImmutableSessionMetaData(id, new CompositeSessionMetaDataEntry<>(creationEntry, accessMetaData));
                        ImmutableSessionAttributes attributes = attributesFactory.createImmutableSessionAttributes(id, attributesValue);
                        ImmutableSession session = HotRodSessionFactory.this.createImmutableSession(id, metaData, attributes);
                        Logger.ROOT_LOGGER.tracef("Session %s has expired.", id);
                        for (SessionExpirationListener listener : listeners) {
                            listener.sessionExpired(session);
                        }
                        attributesRemover.remove(id);
                    }
                }
            } catch (IOException | ClassNotFoundException e) {
                Logger.ROOT_LOGGER.failedToExpireSession(e, id);
            }
        }
    };
    this.executor.submit(task);
}
Also used : ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) CompositeSessionMetaDataEntry(org.wildfly.clustering.web.cache.session.CompositeSessionMetaDataEntry) Marshaller(org.infinispan.commons.marshall.Marshaller) ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) IOException(java.io.IOException) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) ByteBuffer(java.nio.ByteBuffer) SimpleSessionCreationMetaData(org.wildfly.clustering.web.cache.session.SimpleSessionCreationMetaData) SessionAccessMetaData(org.wildfly.clustering.web.cache.session.SessionAccessMetaData) SessionCreationMetaDataEntry(org.wildfly.clustering.web.cache.session.SessionCreationMetaDataEntry) ClientCacheEntryExpired(org.infinispan.client.hotrod.annotation.ClientCacheEntryExpired)

Example 2 with SessionExpirationListener

use of org.wildfly.clustering.web.session.SessionExpirationListener in project wildfly by wildfly.

the class DistributableSessionManagerFactory method createSessionManager.

@Override
public io.undertow.server.session.SessionManager createSessionManager(final Deployment deployment) {
    DeploymentInfo info = deployment.getDeploymentInfo();
    boolean statisticsEnabled = info.getMetricsCollector() != null;
    RecordableInactiveSessionStatistics inactiveSessionStatistics = statisticsEnabled ? new RecordableInactiveSessionStatistics() : null;
    Supplier<String> factory = new IdentifierFactoryAdapter(info.getSessionIdGenerator());
    SessionExpirationListener expirationListener = new UndertowSessionExpirationListener(deployment, this.listeners);
    SessionManagerConfiguration<ServletContext> configuration = new SessionManagerConfiguration<ServletContext>() {

        @Override
        public ServletContext getServletContext() {
            return deployment.getServletContext();
        }

        @Override
        public Supplier<String> getIdentifierFactory() {
            return factory;
        }

        @Override
        public SessionExpirationListener getExpirationListener() {
            return expirationListener;
        }

        @Override
        public Recordable<ImmutableSessionMetaData> getInactiveSessionRecorder() {
            return inactiveSessionStatistics;
        }
    };
    SessionManager<Map<String, Object>, Batch> manager = this.factory.createSessionManager(configuration);
    Batcher<Batch> batcher = manager.getBatcher();
    info.addThreadSetupAction(new ThreadSetupHandler() {

        @Override
        public <T, C> Action<T, C> create(Action<T, C> action) {
            return new Action<T, C>() {

                @Override
                public T call(HttpServerExchange exchange, C context) throws Exception {
                    Batch batch = batcher.suspendBatch();
                    try (BatchContext ctx = batcher.resumeBatch(batch)) {
                        return action.call(exchange, context);
                    }
                }
            };
        }
    });
    SessionListeners listeners = this.listeners;
    RecordableSessionManagerStatistics statistics = (inactiveSessionStatistics != null) ? new DistributableSessionManagerStatistics(manager, inactiveSessionStatistics, this.config.getMaxActiveSessions()) : null;
    io.undertow.server.session.SessionManager result = new DistributableSessionManager(new DistributableSessionManagerConfiguration() {

        @Override
        public String getDeploymentName() {
            return info.getDeploymentName();
        }

        @Override
        public SessionManager<Map<String, Object>, Batch> getSessionManager() {
            return manager;
        }

        @Override
        public SessionListeners getSessionListeners() {
            return listeners;
        }

        @Override
        public RecordableSessionManagerStatistics getStatistics() {
            return statistics;
        }

        @Override
        public boolean isOrphanSessionAllowed() {
            // TODO Configure via DeploymentInfo
            return WildFlySecurityManager.doUnchecked(new PrivilegedAction<Boolean>() {

                @Override
                public Boolean run() {
                    return Boolean.getBoolean(ALLOW_ORPHAN_SESSION_PROPERTY);
                }
            });
        }
    });
    result.setDefaultSessionTimeout((int) this.config.getDefaultSessionTimeout().getSeconds());
    return result;
}
Also used : PrivilegedAction(java.security.PrivilegedAction) SessionManagerConfiguration(org.wildfly.clustering.web.session.SessionManagerConfiguration) HttpServerExchange(io.undertow.server.HttpServerExchange) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) PrivilegedAction(java.security.PrivilegedAction) IdentifierFactoryAdapter(org.wildfly.clustering.web.undertow.IdentifierFactoryAdapter) ServletContext(javax.servlet.ServletContext) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) SessionManager(org.wildfly.clustering.web.session.SessionManager) BatchContext(org.wildfly.clustering.ee.BatchContext) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Map(java.util.Map)

Example 3 with SessionExpirationListener

use of org.wildfly.clustering.web.session.SessionExpirationListener in project wildfly by wildfly.

the class ExpiredSessionRemover method remove.

@Override
public boolean remove(String id) {
    MV metaDataValue = this.factory.getMetaDataFactory().tryValue(id);
    if (metaDataValue != null) {
        ImmutableSessionMetaData metaData = this.factory.getMetaDataFactory().createImmutableSessionMetaData(id, metaDataValue);
        if (metaData.isExpired()) {
            AV attributesValue = this.factory.getAttributesFactory().findValue(id);
            if (attributesValue != null) {
                ImmutableSessionAttributes attributes = this.factory.getAttributesFactory().createImmutableSessionAttributes(id, attributesValue);
                ImmutableSession session = this.factory.createImmutableSession(id, metaData, attributes);
                InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s has expired.", id);
                for (SessionExpirationListener listener : this.listeners) {
                    listener.sessionExpired(session);
                }
            }
            return this.factory.remove(id);
        }
        InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s is not yet expired.", id);
    } else {
        InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s was not found or is currently in use.", id);
    }
    return false;
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData)

Example 4 with SessionExpirationListener

use of org.wildfly.clustering.web.session.SessionExpirationListener in project wildfly by wildfly.

the class ExpiredSessionRemoverTestCase method test.

@Test
public void test() {
    SessionFactory<Object, UUID, UUID, Object> factory = mock(SessionFactory.class);
    SessionMetaDataFactory<UUID> metaDataFactory = mock(SessionMetaDataFactory.class);
    SessionAttributesFactory<Object, UUID> attributesFactory = mock(SessionAttributesFactory.class);
    SessionExpirationListener listener = mock(SessionExpirationListener.class);
    ImmutableSessionAttributes expiredAttributes = mock(ImmutableSessionAttributes.class);
    ImmutableSessionMetaData validMetaData = mock(ImmutableSessionMetaData.class);
    ImmutableSessionMetaData expiredMetaData = mock(ImmutableSessionMetaData.class);
    ImmutableSession expiredSession = mock(ImmutableSession.class);
    String missingSessionId = "missing";
    String expiredSessionId = "expired";
    String validSessionId = "valid";
    UUID expiredMetaDataValue = UUID.randomUUID();
    UUID expiredAttributesValue = UUID.randomUUID();
    UUID validMetaDataValue = UUID.randomUUID();
    ExpiredSessionRemover<Object, UUID, UUID, Object> subject = new ExpiredSessionRemover<>(factory);
    try (Registration regisration = subject.register(listener)) {
        when(factory.getMetaDataFactory()).thenReturn(metaDataFactory);
        when(factory.getAttributesFactory()).thenReturn(attributesFactory);
        when(metaDataFactory.tryValue(missingSessionId)).thenReturn(null);
        when(metaDataFactory.tryValue(expiredSessionId)).thenReturn(expiredMetaDataValue);
        when(metaDataFactory.tryValue(validSessionId)).thenReturn(validMetaDataValue);
        when(metaDataFactory.createImmutableSessionMetaData(expiredSessionId, expiredMetaDataValue)).thenReturn(expiredMetaData);
        when(metaDataFactory.createImmutableSessionMetaData(validSessionId, validMetaDataValue)).thenReturn(validMetaData);
        when(expiredMetaData.isExpired()).thenReturn(true);
        when(validMetaData.isExpired()).thenReturn(false);
        when(attributesFactory.findValue(expiredSessionId)).thenReturn(expiredAttributesValue);
        when(attributesFactory.createImmutableSessionAttributes(expiredSessionId, expiredAttributesValue)).thenReturn(expiredAttributes);
        when(factory.createImmutableSession(same(expiredSessionId), same(expiredMetaData), same(expiredAttributes))).thenReturn(expiredSession);
        subject.remove(missingSessionId);
        subject.remove(expiredSessionId);
        subject.remove(validSessionId);
        verify(factory).remove(expiredSessionId);
        verify(factory, never()).remove(missingSessionId);
        verify(factory, never()).remove(validSessionId);
        verify(listener).sessionExpired(expiredSession);
    }
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) Registration(org.wildfly.clustering.Registration) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with SessionExpirationListener

use of org.wildfly.clustering.web.session.SessionExpirationListener in project wildfly by wildfly.

the class HotRodSessionManagerFactory method createSessionManager.

@Override
public SessionManager<LC, TransactionBatch> createSessionManager(SessionManagerConfiguration<SC> configuration) {
    Registrar<SessionExpirationListener> expirationRegistrar = this.expirationRegistrar;
    Batcher<TransactionBatch> batcher = this.batcher;
    Duration transactionTimeout = this.transactionTimeout;
    HotRodSessionManagerConfiguration<SC> config = new HotRodSessionManagerConfiguration<SC>() {

        @Override
        public SessionExpirationListener getExpirationListener() {
            return configuration.getExpirationListener();
        }

        @Override
        public Registrar<SessionExpirationListener> getExpirationRegistrar() {
            return expirationRegistrar;
        }

        @Override
        public Supplier<String> getIdentifierFactory() {
            return configuration.getIdentifierFactory();
        }

        @Override
        public SC getServletContext() {
            return configuration.getServletContext();
        }

        @Override
        public Batcher<TransactionBatch> getBatcher() {
            return batcher;
        }

        @Override
        public Duration getStopTimeout() {
            return transactionTimeout;
        }
    };
    return new ConcurrentSessionManager<>(new HotRodSessionManager<>(this.factory, config), ConcurrentManager::new);
}
Also used : SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) ConcurrentManager(org.wildfly.clustering.ee.cache.ConcurrentManager) Duration(java.time.Duration) ConcurrentSessionManager(org.wildfly.clustering.web.cache.session.ConcurrentSessionManager)

Aggregations

SessionExpirationListener (org.wildfly.clustering.web.session.SessionExpirationListener)6 ImmutableSessionMetaData (org.wildfly.clustering.web.session.ImmutableSessionMetaData)5 ImmutableSession (org.wildfly.clustering.web.session.ImmutableSession)4 ImmutableSessionAttributes (org.wildfly.clustering.web.session.ImmutableSessionAttributes)4 SessionListeners (io.undertow.server.session.SessionListeners)2 Map (java.util.Map)2 Test (org.junit.Test)2 Batch (org.wildfly.clustering.ee.Batch)2 HttpServerExchange (io.undertow.server.HttpServerExchange)1 Session (io.undertow.server.session.Session)1 SessionListener (io.undertow.server.session.SessionListener)1 Deployment (io.undertow.servlet.api.Deployment)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 ThreadSetupHandler (io.undertow.servlet.api.ThreadSetupHandler)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 PrivilegedAction (java.security.PrivilegedAction)1 Duration (java.time.Duration)1 UUID (java.util.UUID)1 ServletContext (javax.servlet.ServletContext)1