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

org.tkit.quarkus.context.ApplicationContextProvider Maven / Gradle / Ivy

There is a newer version: 2.32.0
Show newest version
package org.tkit.quarkus.context;

import java.util.Map;

import org.eclipse.microprofile.context.spi.ThreadContextProvider;
import org.eclipse.microprofile.context.spi.ThreadContextSnapshot;

/**
 * Microprofile context provider for propagation of correlation scope between threads.
 */
public class ApplicationContextProvider implements ThreadContextProvider {

    private static final String TYPE = "tkit-application-context";

    @Override
    public ThreadContextSnapshot currentContext(Map props) {
        Context captured = ApplicationContext.get();
        return () -> {
            Context current = restore(captured);
            return () -> restore(current);
        };
    }

    private Context restore(Context context) {
        Context currentContext = ApplicationContext.get();
        ApplicationContext.set(context);
        return currentContext;
    }

    @Override
    public ThreadContextSnapshot clearedContext(Map props) {
        return () -> {
            Context current = restore(null);
            return () -> restore(current);
        };
    }

    @Override
    public String getThreadContextType() {
        return TYPE;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy