Java Code Examples for org.eclipse.microprofile.context.ThreadContext#currentContextExecutor()

The following examples show how to use org.eclipse.microprofile.context.ThreadContext#currentContextExecutor() . 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: CdiBeanProducer.java    From microprofile-context-propagation with Apache License 2.0 6 votes vote down vote up
@Produces
@ApplicationScoped
@CDIBean.Priority3Executor
public Executor createPriority3Executor(@CDIBean.PriorityContext ThreadContext ctx) {
    int originalPriority = Thread.currentThread().getPriority();
    try {
        Thread.currentThread().setPriority(3);
        Label.set("do-not-propagate-this-label");
        Buffer.set(new StringBuffer("do-not-propagate-this-buffer"));

        return ctx.currentContextExecutor();
    } finally {
        // restore previous values
        Buffer.set(null);
        Label.set(null);
        Thread.currentThread().setPriority(originalPriority);
    }
}
 
Example 2
Source File: MPConfigBean.java    From microprofile-context-propagation with Apache License 2.0 6 votes vote down vote up
@Inject
protected void setContextSnapshot(@Named("producedThreadContext") ThreadContext contextPropagator) {
    int originalPriority = Thread.currentThread().getPriority();
    int newPriority = originalPriority == 4 ? 3 : 4;
    Thread.currentThread().setPriority(newPriority);
    Label.set("setContextSnapshot-test-label");
    Buffer.set(new StringBuffer("setContextSnapshot-test-buffer"));
    try {
        contextSnapshot = contextPropagator.currentContextExecutor();
    }
    finally {
        Buffer.set(null);
        Label.set(null);
        Thread.currentThread().setPriority(originalPriority);
    }
}
 
Example 3
Source File: ThreadContextTest.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that the ThreadContext's currentContextExecutor
 * method can be used to create an Executor instance with the context that is captured from the
 * current thread per the configuration of the ThreadContext builder, and that the context is
 * applied to the thread where the Executor's execute method runs. This test case aligns with use 
 * case of supplying a contextual Executor to a thread that is otherwise not context-aware.
 *
 * @throws ExecutionException indicates test failure
 * @throws InterruptedException indicates test failure
 * @throws TimeoutException indicates test failure
 */
@Test
public void currentContextExecutorRunsWithContext() throws InterruptedException, ExecutionException, TimeoutException {
    ThreadContext bufferContext = ThreadContext.builder()
            .propagated(Buffer.CONTEXT_NAME)
            .unchanged()
            .cleared(ThreadContext.ALL_REMAINING)
            .build();

    try {
        // Set non-default values
        Buffer.get().append("currentContextExecutor-test-buffer-A");
        Label.set("currentContextExecutor-test-label-A");

        // Reusable contextual Executor
        Executor contextSnapshot = bufferContext.currentContextExecutor();

        Buffer.get().append("-B");
        Label.set("currentContextExecutor-test-label-B");

        // Run contextSnapshot.execute from another thread.
        Future<Void> future = unmanagedThreads.submit(() -> {
            try {
                Buffer.get().append("currentContextExecutor-test-buffer-C");
                Label.set("currentContextExecutor-test-label-C");
                contextSnapshot.execute(() -> {
                    Assert.assertEquals(Buffer.get().toString(), "currentContextExecutor-test-buffer-A-B",
                            "Context type was not propagated to contextual action.");
                    Buffer.get().append("-D");

                    Assert.assertEquals(Label.get(), "",
                            "Context type that is configured to be cleared was not cleared.");
                    Label.set("currentContextExecutor-test-label-D");
                });

                // Execute should not have changed the current thread's context
                Assert.assertEquals(Buffer.get().toString(), "currentContextExecutor-test-buffer-C",
                        "Existing context was altered by a contextual Executor.execute().");

                Assert.assertEquals(Label.get(), "currentContextExecutor-test-label-C",
                        "Existing context was altered by a contextual Executor.execute().");
                return null;
            }
            finally {
                // Restore original values
                Buffer.set(null);
                Label.set(null);
            }
        });

        future.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS);

        // Execute should not have changed the current thread's context
        Assert.assertEquals(Buffer.get().toString(), "currentContextExecutor-test-buffer-A-B-D",
                "Existing context was altered by a contextual Executor.execute().");

        Assert.assertEquals(Label.get(), "currentContextExecutor-test-label-B",
                "Existing context was altered by a contextual Executor.execute().");

        // Run contextSnapshot.execute after the context has changed.
        contextSnapshot.execute(() -> {
            Assert.assertEquals(Buffer.get().toString(), "currentContextExecutor-test-buffer-A-B-D",
                    "Context type was not propagated to contextual action.");
            Buffer.get().append("-E");

            Assert.assertEquals(Label.get(), "",
                    "Context type that is configured to be cleared was not cleared.");
            Label.set("currentContextExecutor-test-label-E");
        });

        // Execute should not have changed the current thread's context
        Assert.assertEquals(Buffer.get().toString(), "currentContextExecutor-test-buffer-A-B-D-E",
                "Existing context was altered by a contextual Executor.execute().");

        Assert.assertEquals(Label.get(), "currentContextExecutor-test-label-B",
                "Existing context was altered by a contextual Executor.execute().");
    }
    finally {
        // Restore original values
        Buffer.set(null);
        Label.set(null);
    }
}