All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wildfly.clustering.session.cache.ManagedSession Maven / Gradle / Ivy

There is a newer version: 4.0.0.Final
Show newest version
/*
 * 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();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy