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;
}
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;
}
});
}
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);
}
});
}
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);
}
});
}
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]));
}
Aggregations