2

我正在 SAP SCP Neo 上开发一个 Java 应用程序,它使用 S/4 SDK 进行平台抽象。我正在努力创建一个租户感知的后台任务。这意味着,当使用 com.sap.cloud.sdk.cloudplatform.tenant.TenantAccessor 或 DestinationAccessor 等 S/4SDK 平台抽象方法来访问租户信息或检索目的地时,这些方法应返回租户特定信息,就像可以从典型的租户特定 Web 请求中调用它们。

在调用 S/4SDK 访问器方法时,我用一个可调用对象包装它们并使用 RequestContextExecutor 执行它。这很好用,但由于我看不到任何提供租户的方法,我不清楚如何解决我的问题。我看到在 S/4 SDK 中使用了默认侦听器,因此我假设它在提供程序帐户的上下文中运行。请在下面查找示例以检索目的地。

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

动机:

  • 我们喜欢编写一些逻辑,如果它是由针对 Java 应用程序的 Web 请求调用或由后台 Java 任务触发的,它应该独立工作。
4

1 回答 1

0

在 SAP CP Neo 上使用 RequestContextExecutor 时,这将返回您正确注意到的依赖于提供者租户。

目前,S/4 SDK 不提供代表另一个租户执行代码的通用方式。这主要是因为租户信息在 SAP CP 环境中的表示方式不同。例如,在 Cloud Foundry 上,租户被编码为 JSON Web 令牌中“zid”字段的一部分。因此,代表不同租户运行代码是很棘手的。因此,在 SAP CP Cloud Foundry 上,您实际上不会有这种对提供者租户的回退。

尽管如此,对于 SAP CP Neo,我希望您应该能够使用以下方法来运行Callable基于另一个租户的上下文。然后,这应该允许您在各自的上下文中按预期检索当前租户Callable

ScpNeoTenant currentTenant = (ScpNeoTenant)TenantAccessor.getCurrentTenant();
TenantContext currentTenantContext = currentTenant.getTenantContext();

currentTenantContext.execute("anotherTenantId", new Callable<MyResult>() {
    @Override
    public MyResult call() {
        return new RequestContextExecutor().execute(new Callable<MyResult>() {
            @Override
            public MyResult call() {
                Tenant tenant = TenantAccessor.getCurrentTenant();
                // ...
                return myResult;
            }
        });
    }
});

我还没有测试过这个,所以请让我知道这是否有效!

于 2018-12-28T15:24:58.507 回答