Class ThreadContextExecutor
java.lang.Object
com.sap.cloud.sdk.cloudplatform.thread.ThreadContextExecutor
Executes a
Callable
or Executable
within a ThreadContext
.
For example:
ThreadContextExecutor.fromNewContext().execute(() -> {
// code that is executed within the ThreadContext created by ThreadContextExecutor
new MyODataService().getAllEntities().top(10).execute();
});
Note: Please be aware that if you pass a ThreadContext
to the executor, you have to make
sure that this context is not destroyed during execution, for example, within asynchronous tasks. You should
therefore only pass a ThreadContext
for which you manage the lifecycle yourself.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
execute
(Executable executable) Executes the givenExecutable
as if it was called inside aThreadContext
.<T> T
Executes the givenCallable
as if it was called inside aThreadContext
.static ThreadContextExecutor
Create aThreadContextExecutor
using the currentThreadContext
, that is being resolved fromThreadContextAccessor
and duplicated with all existing properties.static ThreadContextExecutor
Create aThreadContextExecutor
using the currentThreadContext
, if one is available.static ThreadContextExecutor
Create aThreadContextExecutor
using a new instance ofThreadContext
with empty properties.<T extends ThreadContextListener>
io.vavr.control.Option<T>getListener
(int listenerPriority) Get aThreadContextListener
by its priority.ReturnsThreadContextListener
s by their priority.static ThreadContextExecutor
using
(ThreadContext context) Create aThreadContextExecutor
using a duplicate of the providedThreadContext
with preset properties.withListeners
(ThreadContextListener... listeners) Adds the specified listeners to the life-cycle of theThreadContext
.withListeners
(Iterable<ThreadContextListener> listeners) Adds the specified listeners to the life-cycle of theThreadContext
.Removes all SDK provided listeners on the classpath from the executor call.withoutListeners
(Integer... listenerPriorities) Removes the listeners with the given priorities from the life-cycle of theThreadContext
.withoutListeners
(Iterable<Integer> listenerPriorities) Removes the listeners with the given priorities from the life-cycle of theThreadContext
.
-
Method Details
-
fromCurrentContext
@Nonnull public static ThreadContextExecutor fromCurrentContext() throws ThreadContextAccessExceptionCreate aThreadContextExecutor
using the currentThreadContext
, that is being resolved fromThreadContextAccessor
and duplicated with all existing properties. If noThreadContext
can be determined, then an exception will be thrown.- Returns:
- A new instance of
ThreadContextExecutor
. - Throws:
ThreadContextAccessException
- if aThreadContext
cannot be resolved.
-
fromCurrentOrNewContext
Create aThreadContextExecutor
using the currentThreadContext
, if one is available. Otherwise, a newDefaultThreadContext
is created.- Returns:
- A new instance of
ThreadContextExecutor
. - Throws:
ThreadContextAccessException
- if aThreadContext
cannot be resolved.- See Also:
-
fromNewContext
Create aThreadContextExecutor
using a new instance ofThreadContext
with empty properties.- Returns:
- A new instance of
ThreadContextExecutor
.
-
using
Create aThreadContextExecutor
using a duplicate of the providedThreadContext
with preset properties.- Parameters:
context
- An existing instance ofThreadContext
.- Returns:
- A new instance of
ThreadContextExecutor
.
-
withoutDefaultListeners
Removes all SDK provided listeners on the classpath from the executor call.The various libraries of the SDK provide several implementations of the
ThreadContextListener
which are loaded automatically if their libraries are part of the classpath. To fine tune the used listeners this method allows to ignore those automatically loaded implementation.- Returns:
- This ThreadContextExecutor to provide a fluent interface.
-
withListeners
Adds the specified listeners to the life-cycle of theThreadContext
.- Parameters:
listeners
- The listeners to use during creation and destruction of theThreadContext
.- Returns:
- This ThreadContextExecutor to provide a fluent interface.
-
withListeners
@Nonnull public ThreadContextExecutor withListeners(@Nonnull Iterable<ThreadContextListener> listeners) Adds the specified listeners to the life-cycle of theThreadContext
.- Parameters:
listeners
- The listeners to use during creation and destruction of theThreadContext
.- Returns:
- This ThreadContextExecutor to provide a fluent interface.
-
withoutListeners
Removes the listeners with the given priorities from the life-cycle of theThreadContext
.- Parameters:
listenerPriorities
- The priorities of the listeners to remove.- Returns:
- This ThreadContextExecutor to provide a fluent interface.
-
withoutListeners
@Nonnull public ThreadContextExecutor withoutListeners(@Nonnull Iterable<Integer> listenerPriorities) Removes the listeners with the given priorities from the life-cycle of theThreadContext
.- Parameters:
listenerPriorities
- The priorities of the listeners to remove.- Returns:
- This ThreadContextExecutor to provide a fluent interface.
-
getListenersOrderedByPriority
ReturnsThreadContextListener
s by their priority.- Returns:
- The list of listeners.
-
getListener
@Nonnull public <T extends ThreadContextListener> io.vavr.control.Option<T> getListener(int listenerPriority) Get aThreadContextListener
by its priority.- Type Parameters:
T
- The type of listener- Parameters:
listenerPriority
- The priority of the listeners.- Returns:
- The expected listener, or
null
.
-
execute
@Nullable public <T> T execute(@Nonnull Callable<T> callable) throws ThreadContextExecutionException Executes the givenCallable
as if it was called inside aThreadContext
.- Type Parameters:
T
- The return type of the givenCallable
.- Parameters:
callable
- TheCallable
to execute inside aThreadContext
.- Returns:
- The value computed by the specified
Callable
. - Throws:
ThreadContextExecutionException
- If there is an issue while computing the value.
-
execute
Executes the givenExecutable
as if it was called inside aThreadContext
.- Parameters:
executable
- TheExecutable
to execute inside aThreadContext
.- Throws:
ThreadContextExecutionException
- If there is an issue while running theExecutable
.
-