I'm developing a Java App on SAP SCP Neo which is using the S/4 SDK for platform abstraction. I'm struggling to create a background task that is tenant aware. That means, when using the S/4SDK platform abstraction methods like com.sap.cloud.sdk.cloudplatform.tenant.TenantAccessor or DestinationAccessor to access the tenant information or to retrieve a destination, these methods shall return the tenant specific information, as if one would call them from a typical tenant specific web request.
When calling the S/4SDK accessor methods I wrapped them with a callable and execute it with the RequestContextExecutor. This works fine but as I do not see any way to provide a tenant, it is unclear to me how to solve my problem. I saw that a default listener is used inside the S/4 SDK, so I assume its running in the context of the provider account. Please find below a sample to retrieve a destination.
Destination getDestination(String destinationName) {
// Request Context is present when action is triggered by a web request
if (RequestContextAccessor.getCurrentRequest().isPresent()){
return DestinationAccessor.getDestination(destinatioName);
}
// Use RequestContextExecutor if we are called from a background task
Callable<Destination> callable = new Callable<Destination>() {
@Override
public Destination call() {
return DestinationAccessor.getDestination(destinatioName);
}
};
// TODO this defaults the contexts to the provider account.
return new RequestContextExecutor().execute(callable);
}
Motivation:
- We like to write some logic once and it shall work independent if it is invoked by a Web Request against the java app or triggered by a background java task.