-3

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.userPlatform': Scope 'user' 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.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:355) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.getTarget(CglibAopProxy.java:705) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) at com.cumulocity.sdk.client.PlatformImpl$$EnhancerBySpringCGLIB$$998b9bde.close() at com.test.CleanAssignedSoftwareService.CreateSoftwareAssignmentOperations(CleanAssignedSoftwareService.java:235) at com.test.ScheduledTask.reportCurrentTime(ScheduledTask.java:33) at sun.reflect.GeneratedMethodAccessor69.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.IllegalStateException: Not within any context! at com.cumulocity.microservice.context.ContextServiceImpl.getContext(ContextServiceImpl.java:41) at com.cumulocity.microservice.context.annotation.EnableContextSupportConfiguration$2.getContextId(EnableContextSupportConfiguration.java:45) at com.cumulocity.microservice.context.scope.BaseScope.doGetSynchronized(BaseScope.java:47) at com.cumulocity.microservice.context.scope.BaseScope.get(BaseScope.java:39) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340) ... 19 common frames omitted

  • Can you provide a little bit more context about what you are doing when this exception is thrown? Otherwise I can really only guess what is going on. Usually Not in any context is reported when no Cumulocity credentials are present, possibly because a service is not subscribed for your tenant – l2p Oct 26 '18 at 17:21
  • I have created a micro service with an end point and it's working fine and there I am using the cumulocity annotation below to connect to the cumulocity platform. Now I want to schedule this service using @Schecule annotation of spring boot and getting the above error message. @Autowired(required = true) @Qualifier("userPlatform") private Platform platform; – Vachaspati Diwevedi Oct 26 '18 at 21:33

1 Answers1

1

When running some code with the @Scheduled Annotation you need to define the context. Otherwise you get this exception, as the thread using a cumulocity API is not within any context.

E.g. for all subscribed tenants:

private final MicroserviceSubscriptionsService subscriptions;

@Scheduled(fixedDelay = 5 * 60 * 1000)
private void checkForAgentRepresentation() {
    for (final MicroserviceCredentials mc : subscriptions.getAll()) {
        final String tenant = mc.getTenant();
        subscriptions.runForTenant(tenant, () -> {
            //doSomething like
            inventoryApi.get(...);
        });
    }
}
socona
  • 432
  • 3
  • 13
  • 1
    There is an alternative way not using a for-loop, instead using `subscriptions.runForAllTenants`. – socona Apr 05 '19 at 09:59