# Why are my tenant subscriptions working locally but not in cloud

**URL:** <https://community.cumulocity.com/t/why-are-my-tenant-subscriptions-working-locally-but-not-in-cloud/3606>\
**Category:** Forum\
**Tags:** java, cumulocity\
**Created:** [November 17, 2023, 8:49am UTC](https://community.cumulocity.com/t/why-are-my-tenant-subscriptions-working-locally-but-not-in-cloud/3606 "2023-11-17T08:49:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefan.reichmann](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@stefan.reichmann](https://community.cumulocity.com/u/stefan.reichmann)\
**Post date:** [November 17, 2023, 8:49am UTC](https://community.cumulocity.com/t/why-are-my-tenant-subscriptions-working-locally-but-not-in-cloud/3606/1 "2023-11-17T08:49:39Z")

</div>

#### _ **Product/components** used and **version/fix** level:_ Cumulocity IoT Hosted, BE v 1017.0.289, JavaSDK v 1017.0.375

#### _Detailed explanation of the problem:_ We have a Microservice that is on Multitenant context. When using the JavaSDK and run a HTTP GET command, there is a difference:

When run locally: MicroserviceSubscriptionsService.getAll() returns all subtenants

When run in the cloud: MicroserviceSubscriptionsService.getAll() returns an empty list

An example would be that in the following code, when i run locally (e.G. via Debugger) the part `subscriptionsService.getAll()` is returning the subtenants as needed. After deployment when I run the exact same command, this part returns an empty list.  
Also, the scheduled jobs, do not have any issue with using this command. Inside of a scheduled job this part returns the correct list with subtenants, locally and in the cloud.

#### _Error messages / **full error** message screenshot / **log** file:_

```java
/**
subscriptionsService is typeof MicroserviceSubscriptionsService
ctxService is typeof ContextService<MicroserviceCredentials>
**/
subscriptionsService.subscribe();
var tenants = subscriptionsService.getAll(); //This part is where the difference is
var result = new Result();
tenants.forEach(tenant -> {
    ctxService.callWithinContext(tenant, () -> {
        //Do smth and set result
    });
});

return ResponseEntity.ok(result);

```

Does anybody know why this issue is happening?  
Thanks in advance for the Support.

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [November 17, 2023, 9:18am UTC](https://community.cumulocity.com/t/why-are-my-tenant-subscriptions-working-locally-but-not-in-cloud/3606/2 "2023-11-17T09:18:28Z")

</div>

Could it be that you’re calling it too early in your code? There is a `onSubscriptionsInitialized` method you coud utilize here:

```java
@EventListener
public void onSubscriptionsInitialized(MicroserviceSubscriptionsInitializedEvent event) {
    log.info("All tenant subscriptions initialized.");
    subscriptionsService.subscribe();
    Collection<MicroserviceCredentials> allActiveSubscriptions = subscriptionsService.getAll();
    log.info(allActiveSubscriptions);
    subscriptionService.runForEachTenant(...);
}

```

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [November 17, 2023, 9:38am UTC](https://community.cumulocity.com/t/why-are-my-tenant-subscriptions-working-locally-but-not-in-cloud/3606/3 "2023-11-17T09:38:24Z")

</div>

Why not using the `subscriptionService.runForTenant` or `subscriptionService.runForEachTenant` or `subscriptionService.callForTenant`  
Within that methods you can also call `subscriptionService.getTenant()` to retrieve the current Tenant.

They work out of the box and will also waiting until all subscriptions are initialized basically meaning all service users are retrieved.

Btw. you can also use this to get the current tenant scope:

```auto
@EventListener
public void initialize(MicroserviceSubscriptionAddedEvent event) {
   String tenant = event.getCredentials().getTenant();
   MicroserviceCredentials credentials = event.getCredentials();
   ....
}

```

This is called on microservice startup for each subscribed tenant or when a new tenant is subscribed to the microservice

---

<div class="post-metadata">

**Author:** ![stefan.reichmann](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@stefan.reichmann](https://community.cumulocity.com/u/stefan.reichmann)\
**Post date:** [November 20, 2023, 11:26am UTC](https://community.cumulocity.com/t/why-are-my-tenant-subscriptions-working-locally-but-not-in-cloud/3606/4 "2023-11-20T11:26:44Z")

</div>

Thank you a lot!

The `subscriptionService.runForEachTenant ` was the solution. I don’t know why there is a difference when trying to fetch all tenants via `getAll()` but `runForEachTenant` actually solved this.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [May 18, 2024, 11:26am UTC](https://community.cumulocity.com/t/why-are-my-tenant-subscriptions-working-locally-but-not-in-cloud/3606/5 "2024-05-18T11:26:58Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
