# Getting error in Scheduling method in Cumulocity

**URL:** <https://community.cumulocity.com/t/getting-error-in-scheduling-method-in-cumulocity/4254>\
**Category:** Forum\
**Tags:** device-connectivity, cumulocity\
**Created:** [March 19, 2024, 7:54am UTC](https://community.cumulocity.com/t/getting-error-in-scheduling-method-in-cumulocity/4254 "2024-03-19T07:54:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rushikesh\_n](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@rushikesh\_n](https://community.cumulocity.com/u/rushikesh_n)\
**Post date:** [March 19, 2024, 7:54am UTC](https://community.cumulocity.com/t/getting-error-in-scheduling-method-in-cumulocity/4254/1 "2024-03-19T07:54:21Z")

</div>

I am developing a microservice, in which I want to schedule a method to call every 5 sec but I am getting the following error while doing it

language: Java  
c8y-backend version: 1020.73.0

_ **Service class:** _

```auto
public class FluentService {
    
    @Autowired
    private TenantOptionApi tenantOptionApi;
@Scheduled(fixedDelay = 5000)
    public void pollTenantOptions(){
        OptionPK optionPK = new OptionPK();
       optionPK.setCategory(AuthenticationConstants.TENANT_OPTION_CATEGORY);
        optionPK.setKey("data-mapping");
        OptionRepresentation optionRepresentation
tenantOptionApi.getOption(optionPK);
        String jsonString = optionRepresentation.getValue();
        System.out.println(jsonString);
        DataMapping dataMapping = JSONParser.defaultJSONParser().parse(DataMapping.class,jsonString);
        System.out.println(dataMapping.toString());
    }

```

Error message i am getting:

```auto
org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.tenantOptionApi': 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:** ![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:** [March 19, 2024, 8:23am UTC](https://community.cumulocity.com/t/getting-error-in-scheduling-method-in-cumulocity/4254/2 "2024-03-19T08:23:46Z")

</div>

Can you try:

```java
public class FluentService {

    @Autowired
    private TenantOptionApi tenantOptionApi;

    @Autowired
    private MicroserviceSubscriptionsService subscriptionsService;

    @Scheduled(fixedDelay = 5000)
    public void pollTenantOptions() {
        subscriptionService.runForTenant("your-tenant-id", () - > {
            OptionPK optionPK = new OptionPK();
            optionPK.setCategory(AuthenticationConstants.TENANT_OPTION_CATEGORY);
            optionPK.setKey("data-mapping");
            OptionRepresentation optionRepresentation
            tenantOptionApi.getOption(optionPK);
            String jsonString = optionRepresentation.getValue();
            System.out.println(jsonString);
            DataMapping dataMapping = JSONParser.defaultJSONParser().parse(DataMapping.class, jsonString);
            System.out.println(dataMapping.toString());
        });
    }
}

```

I’ve wrapped your code around the `MicroserviceSubscriptionsService` , this should give your code the tenant scope.

See also: [Microservice Client that connect to multiple devices - #2 by Kai\_Sieben](https://community.cumulocity.com/t/microservice-client-that-connect-to-multiple-devices/7683/2)

---

<div class="post-metadata">

**Author:** ![Christian\_Guether1](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/christian_guether1/32/14848_2.png) [@Christian\_Guether1](https://community.cumulocity.com/u/Christian_Guether1)\
**Post date:** [March 19, 2024, 9:50am UTC](https://community.cumulocity.com/t/getting-error-in-scheduling-method-in-cumulocity/4254/3 "2024-03-19T09:50:11Z")

</div>

As an additional remark to Korbinian’s answer. Instead of hardcoding your tenant id you can also read the tenant from the environment variables, if you only need the tenant id of the tenant on which the Microservice is deployed.

```auto
import org.springframework.beans.factory.annotation.Value;

@Value("${C8Y.bootstrap.tenant}")
String bootstrapTenant;

```

In case you want to run the scheduler in a `MULTI_TENANT` scenario, you can execute it per tenant using `subscriptionService.runForEachTenant()`

  

* * *

Christian Guether

[Software AG Professional Services](https://www.softwareag.com/en_corporate/resources/services/professional-services.html)

[![](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/c/3/c3a93a2618e4708f51be40fe2e142061f7c4edc7.jpeg)](https://www.softwareag.com/en_corporate/resources/services/fast-track-services.html)

---

<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:** [September 15, 2024, 9:50am UTC](https://community.cumulocity.com/t/getting-error-in-scheduling-method-in-cumulocity/4254/4 "2024-09-15T09:50:22Z")

</div>

Closed on September 15, 2024, 9:50am UTC

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