# Error in Scheduling Function

**URL:** <https://community.cumulocity.com/t/error-in-scheduling-function/3574>\
**Category:** Forum\
**Tags:** cumulocity, microservice\
**Created:** [October 25, 2023, 8:01am UTC](https://community.cumulocity.com/t/error-in-scheduling-function/3574 "2023-10-25T08:01:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaibhavi\_ghumare](https://avatars.discourse-cdn.com/v4/letter/v/a587f6/32.png) [@vaibhavi\_ghumare](https://community.cumulocity.com/u/vaibhavi_ghumare)\
**Post date:** [October 25, 2023, 8:01am UTC](https://community.cumulocity.com/t/error-in-scheduling-function/3574/1 "2023-10-25T08:01:31Z")

</div>

o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task

org.springframework.beans.factory.support.ScopeNotActiveException: 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!

---

<div class="post-metadata">

**Author:** ![Harald\_Meyer](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/harald_meyer/32/1959_2.png) [@Harald\_Meyer](https://community.cumulocity.com/u/Harald_Meyer)\
**Post date:** [October 25, 2023, 1:07pm UTC](https://community.cumulocity.com/t/error-in-scheduling-function/3574/2 "2023-10-25T13:07:08Z")

</div>

Hi,

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

```auto
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:

> **[cumulocity-microservice-templates/multischeduler at main ·...](https://github.com/Cumulocity-IoT/cumulocity-microservice-templates/tree/main/multischeduler)**
>
> Collection of templates / examples to give the developer a starting point how common features can be solved by using Cumulocity Microservice SDK for Java. Developed by Global Competency Center IoT...

---

<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:** [April 22, 2024, 1:07pm UTC](https://community.cumulocity.com/t/error-in-scheduling-function/3574/3 "2024-04-22T13:07:59Z")

</div>

Closed on April 22, 2024, 1:07pm UTC

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