org.wildfly.clustering.session.cache.ManagedSession Maven / Gradle / Ivy
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.clustering.session.cache;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import org.wildfly.clustering.session.ImmutableSession;
import org.wildfly.clustering.session.Session;
import org.wildfly.clustering.session.SessionManager;
/**
* {@link Session} decorator that auto-detaches on {@link #close()}.
* @param the session context type
* @author Paul Ferraro
*/
public class ManagedSession extends DecoratedSession {
private final AtomicReference> session;
private final Session detachedSession;
public ManagedSession(SessionManager manager, Session session, Consumer closeTask) {
this(new AttachedSession<>(session, closeTask), new DetachedSession<>(manager, session.getId(), session.getContext()));
}
ManagedSession(Session attachedSession, Session detachedSession) {
this(new AtomicReference<>(attachedSession), detachedSession);
}
private ManagedSession(AtomicReference> session, Session detachedSession) {
super(session::get);
this.session = session;
this.detachedSession = detachedSession;
}
@Override
public void close() {
this.session.getAndSet(this.detachedSession).close();
}
}