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);
}
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;
}
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;
}
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);
}
}
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);
}
Aggregations