Error in Scheduling Function

o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task Error creating bean with name ‘scopedTarget.inventoryApi’: Scope ‘tenant’ is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: Not within any context!


the scope in a microservice defines the credentials that should be used to invoke C8Y APIs and which tenant to perform the actions against. When the microservice is invoked via REST, a user scope automatically exists for the user who performed the REST request and any call to Cumulocity IoT will be done on behalf of the user.

If you are running scheduled functionality, such a user scope does not exist and you need to explicitly establish a tenant scope using the MicroserviceSubscriptionService

microserviceSubscriptionsService.runForEachTenant( ()->{
  String tenant= subscriptionsService.getTenant();		 
  //do something in tenant context;

Here you can find a more comprehensive example that also takes into account multi-tenancy and potentially long-running scheduled job execution be separating scheduled execution from the actual job execution: