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

org.seedstack.jms.internal.JmsSessionLink Maven / Gradle / Ivy

There is a newer version: 3.1.1
Show newest version
/**
 * Copyright (c) 2013-2016, The SeedStack authors 
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.seedstack.jms.internal;


import org.seedstack.seed.transaction.spi.TransactionalLink;

import javax.jms.Session;
import java.util.ArrayDeque;
import java.util.Deque;

class JmsSessionLink implements TransactionalLink {
    private final ThreadLocal> sessionThreadLocal;

    JmsSessionLink() {
        sessionThreadLocal = new ThreadLocal>() {
            @Override
            protected Deque initialValue() {
                return new ArrayDeque<>();
            }
        };
    }

    @Override
    public Session get() {
        Session session = sessionThreadLocal.get().peek();
        if (session == null) {
            throw new IllegalStateException("Attempt to use a JMS session without a transaction");
        }

        return session;
    }

    Session getCurrentTransaction() {
        return sessionThreadLocal.get().peek();
    }

    void push(Session session) {
        sessionThreadLocal.get().push(session);
    }

    Session pop() {
        Deque sessions = sessionThreadLocal.get();
        Session session = sessions.pop();
        if (sessions.isEmpty()) {
            sessionThreadLocal.remove();
        }
        return session;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy