Product/components used and version/fix level:
Cumulocity Production
Detailed explanation of the problem:
Hi, I am using java SDK to fetch data from my service. But I am not able to get any data. What I am doing is I have 1 method that I am calling from my main method since scheduler will run at a certain time, I am directly calling it from my main class.
@Autowired
CronJob cronJob;
public static void main(String[] args) {
SpringApplication.run(CronJobApplication.class, args);
}
@PostConstruct
public void init() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
log.info("Current Time: " + CalibrationUtils.getCurrentTime());
cronJob.sayHello();
}
@Autowired
InventoryApi inventoryApi;
@Autowired
MeasurementApi measurementApi;
@Autowired
MicroserviceSubscriptionsService subscriptionsService;
public void sayHello(){
List<ManagedObjectRepresentation> morList = new ArrayList<>();
subscriptionsService.runForEachTenant(() -> {
inventoryApi.getManagedObjects().get().allPages().forEach(morList::add);
});
List<MeasurementCollection> meaList = new ArrayList<>();
subscriptionsService.runForEachTenant(() -> {
meaList.add(measurementApi.getMeasurements());
});
logger.info("Getting Managed Objects: "+morList);
logger.info("Getting Measurements: "+meaList);
logger.info("Managed Objects: "+inventoryApi.get(new GId("32409")));
}
In console I am getting like:
Getting Managed Objects: []
Getting Measurements: []
and when I am running it without the subscription service I am getting error that tenant is not in scope.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cronJobApplication': Invocation of init method failed; nested exception is 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!
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:440) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.20.jar:5.3.20]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.20.jar:5.3.20]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.0.jar:2.7.0]
at com.CronJobApplication.main(CronJobApplication.java:24) ~[classes/:na]
I tried to follow this as well: https://github.com/SoftwareAG/cumulocity-microservice-templates/tree/main/multischeduler
But no luck, please help me with this. How can I get data in my scheduled service?
Thanks & Regards,
Samanyu