org.eclipse.microprofile.context.spi.ThreadContextController Java Examples

The following examples show how to use org.eclipse.microprofile.context.spi.ThreadContextController. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LabelContextProvider.java    From microprofile-context-propagation with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a snapshot of context for the specified 'label'
 */
private ThreadContextSnapshot snapshot(String label) {
    return () -> {
        String labelToRestore = Label.get();
        AtomicBoolean restored = new AtomicBoolean();

        // Construct an instance that restores the previous context that was on the thread
        // prior to applying the specified 'label' as the new context.
        ThreadContextController contextRestorer = () -> {
            if (restored.compareAndSet(false, true)) {
                Label.set(labelToRestore);
            }
            else {
                throw new IllegalStateException();
            }
        };

        Label.set(label);
        return contextRestorer;
    };
}
 
Example #2
Source File: ThreadPrioritySnapshot.java    From microprofile-context-propagation with Apache License 2.0 6 votes vote down vote up
/**
 * Apply the saved thread priority to the current thread, but
 * first storing a copying of the thread priority that was previously on the thread
 * so that the previous thread priority can later be restored via the returned
 * ThreadContextController.
 */
@Override
public ThreadContextController begin() {
    Thread thread = Thread.currentThread();
    int priorityToRestore = thread.getPriority();
    AtomicBoolean restored = new AtomicBoolean();

    ThreadContextController contextRestorer = () -> {
        if (restored.compareAndSet(false, true)) {
            thread.setPriority(priorityToRestore);
        }
        else {
            throw new IllegalStateException();
        }
    };

    thread.setPriority(priority);

    return contextRestorer;
}
 
Example #3
Source File: CustomContextProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private ThreadContextSnapshot snapshot(String label) {
    return () -> {
        String labelToRestore = CustomContext.get();
        AtomicBoolean restored = new AtomicBoolean();

        // Construct an instance that restores the previous context that was on the thread
        // prior to applying the specified 'label' as the new context.
        ThreadContextController contextRestorer = () -> {
            if (restored.compareAndSet(false, true)) {
                CustomContext.set(labelToRestore);
            } else {
                throw new IllegalStateException();
            }
        };

        CustomContext.set(label);
        return contextRestorer;
    };
}
 
Example #4
Source File: ArcContextProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public ThreadContextController begin() {
    // can be called later on, we should retrieve the container again
    ArcContainer arcContainer = Arc.container();
    if (arcContainer == null || !arcContainer.isRunning()) {
        return NOOP_CONTROLLER;
    }
    ManagedContext requestContext = arcContainer.requestContext();
    // this is executed on another thread, context can but doesn't need to be active here
    if (requestContext.isActive()) {
        // context active, store current state, start blank context anew and restore state afterwards
        InjectableContext.ContextState stateToRestore = requestContext.getState();
        requestContext.deactivate();
        requestContext.activate();
        return new RestoreContextController(requestContext, stateToRestore);
    } else {
        // context not active, activate blank one, deactivate afterwards
        requestContext.activate();
        return ArcContextProvider::deactivateRequestContext;
    }
}
 
Example #5
Source File: ArcContextProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public ThreadContextController begin() {
    // can be called later on, we should retrieve the container again
    ArcContainer arcContainer = Arc.container();
    if (arcContainer == null || !arcContainer.isRunning()) {
        //this happens on shutdown, if we blow up here it can break shutdown, and stop
        //resources from being cleaned up, causing tests to fail
        return NOOP_CONTROLLER;
    }
    ManagedContext requestContext = arcContainer.requestContext();
    // this is executed on another thread, context can but doesn't need to be active here
    if (requestContext.isActive()) {
        // context active, store current state, feed it new one and restore state afterwards
        InjectableContext.ContextState stateToRestore = requestContext.getState();
        requestContext.deactivate();
        return new ThreadContextController() {
            @Override
            public void endContext() throws IllegalStateException {
                requestContext.activate(stateToRestore);
            }
        };
    } else {
        // context not active
        return NOOP_CONTROLLER;
    }
}
 
Example #6
Source File: ArcContextProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public ThreadContextController begin() {
    // can be called later on, we should retrieve the container again
    ArcContainer arcContainer = Arc.container();
    if (arcContainer == null || !arcContainer.isRunning()) {
        //this happens on shutdown, if we blow up here it can break shutdown, and stop
        //resources from being cleaned up, causing tests to fail
        return NOOP_CONTROLLER;
    }
    ManagedContext requestContext = arcContainer.requestContext();
    // this is executed on another thread, context can but doesn't need to be active here
    if (requestContext.isActive()) {
        // context active, store current state, feed it new one and restore state afterwards
        InjectableContext.ContextState stateToRestore = requestContext.getState();
        requestContext.deactivate();
        requestContext.activate(state);
        return new RestoreContextController(requestContext, stateToRestore);
    } else {
        // context not active, activate and pass it new instance, deactivate afterwards
        requestContext.activate(state);
        return ArcContextProvider::deactivateRequestContext;
    }
}
 
Example #7
Source File: BufferContextSnapshot.java    From microprofile-context-propagation with Apache License 2.0 5 votes vote down vote up
/**
 * Apply the requested buffer context to the current thread,
 * first storing a copying of the buffer that was previously associated with the thread,
 * to later be restored via the returned ThreadContextController.
 */
@Override
public ThreadContextController begin() {
    ThreadContextController contextRestorer = new BufferContextRestorer();
    Buffer.set(buffer == null ? new StringBuffer() : buffer);
    return contextRestorer;
}