# runForTenant() & MicroserviceSubscription AddedEvent do not work together

**URL:** <https://community.cumulocity.com/t/runfortenant-microservicesubscription-addedevent-do-not-work-together/7802>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [November 11, 2024, 4:41am UTC](https://community.cumulocity.com/t/runfortenant-microservicesubscription-addedevent-do-not-work-together/7802 "2024-11-11T04:41:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Voigt2](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/michael_voigt2/32/18431_2.png) [@Michael\_Voigt2](https://community.cumulocity.com/u/Michael_Voigt2)\
**Post date:** [November 11, 2024, 4:41am UTC](https://community.cumulocity.com/t/runfortenant-microservicesubscription-addedevent-do-not-work-together/7802/1 "2024-11-11T04:41:00Z")

</div>

#### _ **Product/components** used and **version/fix** level:_

Microservice SDK 1018.510.0

#### _Detailed explanation of the problem:_

We’re using the SDK and observed a strange behavior. We use `EventListeners` to react on `MicroserviceSubscriptionAddedEvents`:

```auto
    @EventListener
    public void onMicroserviceSubscriptionAddedEvent(final MicroserviceSubscriptionAddedEvent event) {
        logger.info("MicroserviceSubscriptionAddedEvent: {}", event);
    }

```

This worked very good until we added a @PostConstruct block which runs some logic on the bootstrap tenant:

```auto
    @PostConstruct
    void init() {
        String bootstrapTenant = platformProperties.getMicroserviceBoostrapUser().getTenant();
        logger.info("bootstrapTenant: {}", bootstrapTenant);
        subscription.runForTenant(bootstrapTenant, () -> {
            logger.info("running in bootstrapTenant: {}", bootstrapTenant);
        });
    }

```

Independently of what is executed within the `runForTenant` block (even if it is empty): as a result, the `MicroserviceSubscriptionAddedEvent` is **not** received anymore and therefore the `onMicroserviceSubscriptionAddedEvent` method is **not** invoked!  
As a side note: the `MicroserviceSubscriptionsInitializedEvent` is still received.

Now we wonder why that `runForTenant` block with the `bootstrapTenant` is a problem? Can someone explain?

Thanks a lot,  
Michael

---

<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 11, 2024, 2:23pm UTC](https://community.cumulocity.com/t/runfortenant-microservicesubscription-addedevent-do-not-work-together/7802/2 "2024-11-11T14:23:49Z")

</div>

I cannot explain it but I recognized the same behavior sometime ago. I think I raised a support incident but couldn’t find it anymore. The solution was that you can only use one of them at a time. As the `onMicroserviceSubscriptionAddedEvent` is called on every startup and when a new tenant is added it should be where you should place your logic und you don’t need the `init()` anymore.

---

<div class="post-metadata">

**Author:** ![Michael\_Voigt2](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/michael_voigt2/32/18431_2.png) [@Michael\_Voigt2](https://community.cumulocity.com/u/Michael_Voigt2)\
**Post date:** [November 13, 2024, 5:21am UTC](https://community.cumulocity.com/t/runfortenant-microservicesubscription-addedevent-do-not-work-together/7802/3 "2024-11-13T05:21:58Z")

</div>

Thanks @Stefan_Witschel for the feedback. While I still dont understand why we cannot use both approaches, we now use a different workaround: we listen to the `MicroserviceSubscriptionsInitializedEvent` and do the initialization in there by invoking the `subscription.runForTenant()`.

---

<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 12, 2025, 5:22am UTC](https://community.cumulocity.com/t/runfortenant-microservicesubscription-addedevent-do-not-work-together/7802/4 "2025-05-12T05:22:48Z")

</div>

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