Search in sources :

Example 1 with ContextManagerProvider

use of org.eclipse.microprofile.context.spi.ContextManagerProvider in project quarkus by quarkusio.

the class SmallRyeContextPropagationRecorder method configureRuntime.

public void configureRuntime(ExecutorService executorService, ShutdownContext shutdownContext) {
    // associate the static init manager to the runtime CL
    ContextManagerProvider contextManagerProvider = ContextManagerProvider.instance();
    // finish building our manager
    builder.withDefaultExecutorService(executorService);
    SmallRyeContextManager contextManager = builder.build();
    contextManagerProvider.registerContextManager(contextManager, Thread.currentThread().getContextClassLoader());
    // needs to be late, as running threads can re-create an implicit one
    shutdownContext.addLastShutdownTask(new Runnable() {

        @Override
        public void run() {
            contextManagerProvider.releaseContextManager(contextManager);
        }
    });
    // Avoid leaking the classloader:
    this.builder = null;
}
Also used : SmallRyeContextManager(io.smallrye.context.SmallRyeContextManager) SmallRyeContextManagerProvider(io.smallrye.context.SmallRyeContextManagerProvider) ContextManagerProvider(org.eclipse.microprofile.context.spi.ContextManagerProvider)

Example 2 with ContextManagerProvider

use of org.eclipse.microprofile.context.spi.ContextManagerProvider in project quarkus-quickstarts by quarkusio.

the class TransactionalResource method async2.

@POST
@Produces(MediaType.TEXT_PLAIN)
@Transactional
@Path("async-with-completion-stage")
public CompletionStage<Integer> async2() throws SystemException {
    System.out.printf("submitting async job ...%n");
    ContextManagerProvider cmp = ContextManagerProvider.INSTANCE.get();
    ManagedExecutor me = cmp.getContextManager().newManagedExecutorBuilder().propagated(ThreadContext.TRANSACTION).build();
    Transaction txnToPropagate = TransactionManager.transactionManager().getTransaction();
    return me.supplyAsync(() -> {
        try {
            Transaction txn = TransactionManager.transactionManager().getTransaction();
            if (!txn.equals(txnToPropagate)) {
                // the original transaction, txnToPropagate, should have been propagated to the new thread
                return -1;
            }
            // should return Status.STATUS_ACTIVE
            return getTransactionStatus();
        } catch (SystemException e) {
            return -1;
        }
    });
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) ManagedExecutor(org.eclipse.microprofile.context.ManagedExecutor) ContextManagerProvider(org.eclipse.microprofile.context.spi.ContextManagerProvider) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Transactional(javax.transaction.Transactional)

Example 3 with ContextManagerProvider

use of org.eclipse.microprofile.context.spi.ContextManagerProvider in project quarkus-quickstarts by quarkusio.

the class TransactionalResource method async1.

@POST
@Produces(MediaType.TEXT_PLAIN)
@Transactional
@Path("async-with-suspended")
public void async1(@Suspended AsyncResponse ar) throws SystemException {
    // the framework will have started a transaction because of the @Transactional annotation
    Transaction txnToPropagate = TransactionManager.transactionManager().getTransaction();
    ContextManagerProvider cmp = ContextManagerProvider.INSTANCE.get();
    ManagedExecutor me = cmp.getContextManager().newManagedExecutorBuilder().propagated(ThreadContext.TRANSACTION).build();
    // the transaction should be active (because of the @Transactional annotation)
    if (getTransactionStatus() != Status.STATUS_ACTIVE) {
        ar.resume(Response.status(Response.Status.PRECONDITION_FAILED).entity(getTransactionStatus()).build());
    }
    me.submit(() -> {
        try {
            Transaction txn = TransactionManager.transactionManager().getTransaction();
            if (!txn.equals(txnToPropagate)) {
                // the original transaction, txnToPropagate, should have been propagated to the new thread
                ar.resume(Response.status(Response.Status.PRECONDITION_FAILED).entity(getTransactionStatus()).build());
            }
            // should return Status.STATUS_ACTIVE
            ar.resume(Response.ok().entity(getTransactionStatus()).build());
        } catch (SystemException e) {
            ar.resume(e);
        }
    });
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) ManagedExecutor(org.eclipse.microprofile.context.ManagedExecutor) ContextManagerProvider(org.eclipse.microprofile.context.spi.ContextManagerProvider) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Transactional(javax.transaction.Transactional)

Example 4 with ContextManagerProvider

use of org.eclipse.microprofile.context.spi.ContextManagerProvider in project quarkus-quickstarts by quarkusio.

the class TransactionalResource method asyncIssue6471Reproducer.

@POST
@Produces(MediaType.TEXT_PLAIN)
@Transactional
@Path("async-6471-reproducer")
public void asyncIssue6471Reproducer(@Suspended AsyncResponse ar) throws SystemException {
    System.out.printf("submitting async job ...%n");
    Transaction txnToPropagate = TransactionManager.transactionManager().getTransaction();
    // the transaction should be active (because of the @Transactional annotation)
    if (getTransactionStatus() != Status.STATUS_ACTIVE) {
        ar.resume(Response.status(Response.Status.PRECONDITION_FAILED).entity(getTransactionStatus()).build());
    }
    ContextManagerProvider cmp = ContextManagerProvider.INSTANCE.get();
    ManagedExecutor me = cmp.getContextManager().newManagedExecutorBuilder().propagated(ThreadContext.TRANSACTION).build();
    me.submit(() -> {
        System.out.printf("running async job ...%n");
        try {
            Transaction txn = TransactionManager.transactionManager().getTransaction();
            if (!txn.equals(txnToPropagate)) {
                // the original transaction, txnToPropagate, should have been propagated to the new thread
                ar.resume(Response.status(Response.Status.PRECONDITION_FAILED).entity(getTransactionStatus()).build());
            }
            // execute a long running business activity and resume when done
            System.out.printf("resuming long running async job ...%n");
            // the transaction started via the @Transactional annotation should still be active
            // but due to Quarkus issue 6471 there is no interceptor for extending the transaction bondary
            // (see the issue for further details)
            // should return Status.STATUS_ACTIVE
            ar.resume(Response.ok().entity(getTransactionStatus()).build());
        } catch (SystemException e) {
            System.out.printf("resuming async job with exception: %s%n", e.getMessage());
            ar.resume(e);
        }
    });
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) ManagedExecutor(org.eclipse.microprofile.context.ManagedExecutor) ContextManagerProvider(org.eclipse.microprofile.context.spi.ContextManagerProvider) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Transactional(javax.transaction.Transactional)

Example 5 with ContextManagerProvider

use of org.eclipse.microprofile.context.spi.ContextManagerProvider in project quarkus by quarkusio.

the class SmallRyeContextPropagationRecorder method configureStaticInit.

public void configureStaticInit(List<ThreadContextProvider> discoveredProviders, List<ContextManagerExtension> discoveredExtensions) {
    // in the live-reload mode, the provider instance may be already set in the previous start
    if (ContextManagerProvider.INSTANCE.get() == null) {
        ContextManagerProvider contextManagerProvider = new SmallRyeContextManagerProvider();
        ContextManagerProvider.register(contextManagerProvider);
    }
    // do what config we can here, but we need the runtime executor service to finish
    builder = (SmallRyeContextManager.Builder) ContextManagerProvider.instance().getContextManagerBuilder();
    builder.withThreadContextProviders(discoveredProviders.toArray(new ThreadContextProvider[0]));
    builder.withContextManagerExtensions(discoveredExtensions.toArray(new ContextManagerExtension[0]));
}
Also used : ContextManagerExtension(org.eclipse.microprofile.context.spi.ContextManagerExtension) SmallRyeContextManager(io.smallrye.context.SmallRyeContextManager) ThreadContextProvider(org.eclipse.microprofile.context.spi.ThreadContextProvider) SmallRyeContextManagerProvider(io.smallrye.context.SmallRyeContextManagerProvider) SmallRyeContextManagerProvider(io.smallrye.context.SmallRyeContextManagerProvider) ContextManagerProvider(org.eclipse.microprofile.context.spi.ContextManagerProvider)

Aggregations

ContextManagerProvider (org.eclipse.microprofile.context.spi.ContextManagerProvider)5 SystemException (javax.transaction.SystemException)3 Transaction (javax.transaction.Transaction)3 Transactional (javax.transaction.Transactional)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 ManagedExecutor (org.eclipse.microprofile.context.ManagedExecutor)3 SmallRyeContextManager (io.smallrye.context.SmallRyeContextManager)2 SmallRyeContextManagerProvider (io.smallrye.context.SmallRyeContextManagerProvider)2 ContextManagerExtension (org.eclipse.microprofile.context.spi.ContextManagerExtension)1 ThreadContextProvider (org.eclipse.microprofile.context.spi.ThreadContextProvider)1