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:
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:
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!
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.