Microservice Subscription event using Java microservice

I am using MicroServiceSubscription Event in Java microservice,
I am getting the tenant using env.getCredentials.getTenant() but I want to check if the tenant has subscribe to the microservice and add it to a list.if yes then if statement should be executed.

Java version: 17
c8Y_backend version: 1020.73.0

 @EventListener
    public void onMicroserviceSubscribed(MicroserviceSubscriptionAddedEvent event) {
        try {
            String tenant = event.getCredentials().getTenant();
            String currentTenant = env.getProperty(C8Y_BOOTSTRAP_TENANT);
            if(tenant.equals(currentTenant)){
                getAllDeviceList();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

In the code snippet mentioned below I am checking whether the tenant is equal to the bootstrap tenant.

So you’re having a MULIT_TENANT Microservice here?

If you want to detect the moment at when the Microservice has started and initialized all its subscriptions, you can use

@EventListener
public void onSubscriptionsInitialized(MicroserviceSubscriptionsInitializedEvent event) {
	log.info("Subscriptions have been initialized on application startup");
	...
}

i am using a MULT_TENANT microservice here.
I want to check if the current tenant has subscribed to the microservice or not

Each time the microservice is subscribed to a tenant the EventListener is fired with the context of that tenant it has been subscribed on. With this:

String tenant = event.getCredentials().getTenant();

you get this information already and you are already in the context of that tenant to retrieve devices etc. You can also add it to a list if desired. No additional checks needed.

Your current code is wrong because the boostrap tenant is not the currentTenant!

String currentTenant = env.getProperty(C8Y_BOOTSTRAP_TENANT);

This is the Boostrap Tenant and has nothing to do with the tenants the microservice is subscribed to. The bootstrap credentials should also be not used to do service call but are always existing to retrieve the service user for each tenant the microservice is subscribed to.

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