Search in sources :

Example 1 with ImmutableSessionMetaData

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

the class RecordableInactiveSessionStatisticsTestCase method test.

@Test
public void test() {
    ImmutableSession session = mock(ImmutableSession.class);
    ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
    Instant now = Instant.now();
    Instant created = now.minus(Duration.ofMinutes(20L));
    when(session.getMetaData()).thenReturn(metaData);
    when(metaData.isExpired()).thenReturn(false);
    when(metaData.getCreationTime()).thenReturn(created);
    this.statistics.record(session);
    assertEquals(0L, this.statistics.getExpiredSessionCount());
    assertEquals(20L, this.statistics.getMaxSessionLifetime().toMinutes());
    assertEquals(20L, this.statistics.getMeanSessionLifetime().toMinutes());
    now = Instant.now();
    created = now.minus(Duration.ofMinutes(10L));
    when(metaData.isExpired()).thenReturn(true);
    when(metaData.getCreationTime()).thenReturn(created);
    this.statistics.record(session);
    assertEquals(1L, this.statistics.getExpiredSessionCount());
    assertEquals(20L, this.statistics.getMaxSessionLifetime().toMinutes());
    assertEquals(15L, this.statistics.getMeanSessionLifetime().toMinutes());
    this.statistics.reset();
    assertEquals(0L, this.statistics.getExpiredSessionCount());
    assertEquals(0L, this.statistics.getMaxSessionLifetime().toMinutes());
    assertEquals(0L, this.statistics.getMeanSessionLifetime().toMinutes());
}
Also used : ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Instant(java.time.Instant) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Test(org.junit.Test)

Example 2 with ImmutableSessionMetaData

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

the class DistributableSessionManagerTestCase method getSessionByIdentifier.

@Test
public void getSessionByIdentifier() {
    Batcher<Batch> batcher = mock(Batcher.class);
    Batch batch = mock(Batch.class);
    ImmutableSession session = mock(ImmutableSession.class);
    ImmutableSessionAttributes attributes = mock(ImmutableSessionAttributes.class);
    ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
    String id = "session";
    String name = "name";
    Object value = new Object();
    Set<String> names = Collections.singleton(name);
    Instant creationTime = Instant.now();
    Instant lastAccessedTime = Instant.now();
    Duration maxInactiveInterval = Duration.ofMinutes(30L);
    when(this.manager.getBatcher()).thenReturn(batcher);
    when(this.manager.viewSession(id)).thenReturn(session);
    when(session.getId()).thenReturn(id);
    when(session.getAttributes()).thenReturn(attributes);
    when(attributes.getAttributeNames()).thenReturn(names);
    when(attributes.getAttribute(name)).thenReturn(value);
    when(session.getMetaData()).thenReturn(metaData);
    when(metaData.getCreationTime()).thenReturn(creationTime);
    when(metaData.getLastAccessedTime()).thenReturn(lastAccessedTime);
    when(metaData.getMaxInactiveInterval()).thenReturn(maxInactiveInterval);
    when(batcher.createBatch()).thenReturn(batch);
    io.undertow.server.session.Session result = this.adapter.getSession(id);
    assertSame(this.adapter, result.getSessionManager());
    assertSame(id, result.getId());
    assertEquals(creationTime.toEpochMilli(), result.getCreationTime());
    assertEquals(lastAccessedTime.toEpochMilli(), result.getLastAccessedTime());
    assertEquals(maxInactiveInterval.getSeconds(), result.getMaxInactiveInterval());
    assertEquals(names, result.getAttributeNames());
    assertSame(value, result.getAttribute(name));
    verify(batch).close();
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Instant(java.time.Instant) Duration(java.time.Duration) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) Batch(org.wildfly.clustering.ee.Batch) Test(org.junit.Test)

Example 3 with ImmutableSessionMetaData

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

the class InfinispanSessionManager method start.

@Override
public void start() {
    this.executor = Executors.newSingleThreadExecutor(createThreadFactory());
    if (this.recorder != null) {
        this.recorder.reset();
    }
    this.identifierFactory.start();
    final List<Scheduler> schedulers = new ArrayList<>(2);
    schedulers.add(new SessionExpirationScheduler(this.batcher, new ExpiredSessionRemover<>(this.factory, this.expirationListener)));
    if (this.maxActiveSessions >= 0) {
        schedulers.add(new SessionEvictionScheduler(this.cache.getName() + ".eviction", this.factory, this.batcher, this.dispatcherFactory, this.maxActiveSessions));
    }
    this.scheduler = new Scheduler() {

        @Override
        public void schedule(String sessionId, ImmutableSessionMetaData metaData) {
            schedulers.forEach(scheduler -> scheduler.schedule(sessionId, metaData));
        }

        @Override
        public void cancel(String sessionId) {
            schedulers.forEach(scheduler -> scheduler.cancel(sessionId));
        }

        @Override
        public void cancel(Locality locality) {
            schedulers.forEach(scheduler -> scheduler.cancel(locality));
        }

        @Override
        public void close() {
            schedulers.forEach(scheduler -> scheduler.close());
        }
    };
    this.dispatcher = this.dispatcherFactory.createCommandDispatcher(this.cache.getName() + ".schedulers", this.scheduler);
    this.cache.addListener(this, this.filter);
    this.schedule(new SimpleLocality(false), this.locality);
}
Also used : Session(org.wildfly.clustering.web.session.Session) Cache(org.infinispan.Cache) CommandDispatcher(org.wildfly.clustering.dispatcher.CommandDispatcher) SessionManager(org.wildfly.clustering.web.session.SessionManager) Future(java.util.concurrent.Future) Duration(java.time.Duration) Map(java.util.Map) CacheEntryRemovedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent) SessionMetaData(org.wildfly.clustering.web.session.SessionMetaData) ThreadFactory(java.util.concurrent.ThreadFactory) HttpSession(javax.servlet.http.HttpSession) ImmutableHttpSessionAdapter(org.wildfly.clustering.web.session.ImmutableHttpSessionAdapter) InfinispanWebLogger(org.wildfly.clustering.web.infinispan.logging.InfinispanWebLogger) Listener(org.infinispan.notifications.Listener) IdentifierFactory(org.wildfly.clustering.web.IdentifierFactory) CacheEntryPassivatedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryPassivatedEvent) Key(org.wildfly.clustering.infinispan.spi.distribution.Key) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) CommandDispatcherFactory(org.wildfly.clustering.dispatcher.CommandDispatcherFactory) Set(java.util.Set) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) WildFlySecurityManager(org.wildfly.security.manager.WildFlySecurityManager) Stream(java.util.stream.Stream) Invoker(org.wildfly.clustering.ee.Invoker) Flag(org.infinispan.context.Flag) CacheEntryRemoved(org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved) DataRehashed(org.infinispan.notifications.cachelistener.annotation.DataRehashed) Locality(org.wildfly.clustering.infinispan.spi.distribution.Locality) CacheEntryPassivated(org.infinispan.notifications.cachelistener.annotation.CacheEntryPassivated) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) SimpleLocality(org.wildfly.clustering.infinispan.spi.distribution.SimpleLocality) HttpSessionActivationListener(javax.servlet.http.HttpSessionActivationListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Command(org.wildfly.clustering.dispatcher.Command) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) ConsistentHashLocality(org.wildfly.clustering.infinispan.spi.distribution.ConsistentHashLocality) NodeFactory(org.wildfly.clustering.group.NodeFactory) ExecutorService(java.util.concurrent.ExecutorService) Address(org.infinispan.remoting.transport.Address) JBossThreadFactory(org.jboss.threads.JBossThreadFactory) CacheException(org.infinispan.commons.CacheException) Iterator(java.util.Iterator) Batcher(org.wildfly.clustering.ee.Batcher) CacheEntryActivated(org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated) TransactionBatch(org.wildfly.clustering.ee.infinispan.TransactionBatch) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) CacheEntryActivatedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryActivatedEvent) TimeUnit(java.util.concurrent.TimeUnit) Recordable(org.wildfly.clustering.ee.Recordable) Batch(org.wildfly.clustering.ee.Batch) CacheProperties(org.wildfly.clustering.ee.infinispan.CacheProperties) RetryingInvoker(org.wildfly.clustering.ee.infinispan.RetryingInvoker) Node(org.wildfly.clustering.group.Node) ServletContext(javax.servlet.ServletContext) DataRehashedEvent(org.infinispan.notifications.cachelistener.event.DataRehashedEvent) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener) DistributionManager(org.infinispan.distribution.DistributionManager) ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) Locality(org.wildfly.clustering.infinispan.spi.distribution.Locality) SimpleLocality(org.wildfly.clustering.infinispan.spi.distribution.SimpleLocality) ConsistentHashLocality(org.wildfly.clustering.infinispan.spi.distribution.ConsistentHashLocality) ArrayList(java.util.ArrayList) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) SimpleLocality(org.wildfly.clustering.infinispan.spi.distribution.SimpleLocality)

Example 4 with ImmutableSessionMetaData

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

the class UndertowSessionExpirationListenerTestCase method sessionExpired.

@Test
public void sessionExpired() {
    Deployment deployment = mock(Deployment.class);
    UndertowSessionManager manager = mock(UndertowSessionManager.class);
    SessionManager<LocalSessionContext, Batch> delegateManager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    Batch batch = mock(Batch.class);
    SessionListener listener = mock(SessionListener.class);
    ImmutableSession session = mock(ImmutableSession.class);
    ImmutableSessionAttributes attributes = mock(ImmutableSessionAttributes.class);
    ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
    ArgumentCaptor<Session> capturedSession = ArgumentCaptor.forClass(Session.class);
    String expectedSessionId = "session";
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    SessionExpirationListener expirationListener = new UndertowSessionExpirationListener(deployment, listeners);
    when(deployment.getSessionManager()).thenReturn(manager);
    when(manager.getSessionManager()).thenReturn(delegateManager);
    when(delegateManager.getBatcher()).thenReturn(batcher);
    when(batcher.suspendBatch()).thenReturn(batch);
    when(session.getId()).thenReturn(expectedSessionId);
    when(session.getAttributes()).thenReturn(attributes);
    when(attributes.getAttributeNames()).thenReturn(Collections.emptySet());
    when(session.getMetaData()).thenReturn(metaData);
    when(metaData.getCreationTime()).thenReturn(Instant.now());
    when(metaData.getLastAccessedTime()).thenReturn(Instant.now());
    when(metaData.getMaxInactiveInterval()).thenReturn(Duration.ZERO);
    expirationListener.sessionExpired(session);
    verify(batcher).suspendBatch();
    verify(listener).sessionDestroyed(capturedSession.capture(), isNull(HttpServerExchange.class), same(SessionListener.SessionDestroyedReason.TIMEOUT));
    verify(batcher).resumeBatch(batch);
    assertSame(expectedSessionId, capturedSession.getValue().getId());
    assertSame(manager, capturedSession.getValue().getSessionManager());
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Deployment(io.undertow.servlet.api.Deployment) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionListener(io.undertow.server.session.SessionListener) Session(io.undertow.server.session.Session) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Test(org.junit.Test)

Example 5 with ImmutableSessionMetaData

use of org.wildfly.clustering.web.session.ImmutableSessionMetaData 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);
                this.listener.sessionExpired(session);
            }
            return this.factory.remove(id);
        }
    }
    return false;
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData)

Aggregations

ImmutableSessionMetaData (org.wildfly.clustering.web.session.ImmutableSessionMetaData)9 Test (org.junit.Test)7 ImmutableSession (org.wildfly.clustering.web.session.ImmutableSession)7 ImmutableSessionAttributes (org.wildfly.clustering.web.session.ImmutableSessionAttributes)6 Instant (java.time.Instant)3 Batch (org.wildfly.clustering.ee.Batch)3 Duration (java.time.Duration)2 Map (java.util.Map)2 TransactionBatch (org.wildfly.clustering.ee.infinispan.TransactionBatch)2 SessionExpirationListener (org.wildfly.clustering.web.session.SessionExpirationListener)2 HttpServerExchange (io.undertow.server.HttpServerExchange)1 Session (io.undertow.server.session.Session)1 SessionListener (io.undertow.server.session.SessionListener)1 SessionListeners (io.undertow.server.session.SessionListeners)1 Deployment (io.undertow.servlet.api.Deployment)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Set (java.util.Set)1