1

I made a schedule that execute itself every 5 minutes here is it

package com.start.services;
import ...    
@Component
public class ScheduledTasks { 
    @Autowired
    AlertServiceImpl alertServ ;
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 1000*60*5) // every 5 minutes
    public void reportCurrentTime() {

        log.info("Refreshing started at {}", dateFormat.format(new Date()));
        alertServ.refreshAlerts();

    }
}

But I get this strange exception when it executes a twitter service :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.twitter': Scope 'request' 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: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:355) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at com.sun.proxy.$Proxy105.searchOperations(Unknown Source) ~[na:na]
BadrEddineBen
  • 235
  • 1
  • 4
  • 24
  • Possible duplicate of [Scope 'session' is not active for the current thread; IllegalStateException: No thread-bound request found](https://stackoverflow.com/questions/21286675/scope-session-is-not-active-for-the-current-thread-illegalstateexception-no) – xyz Jul 24 '17 at 18:51

1 Answers1

0

This happens because external services, like your twitter integration, run in scope 'request', but scheduled tasks are meant to run in other scopes, like application scope; spring, by design's choice, can't know when the request starts and when it ends. But there are a couple of solutions/workarounds, like implementing a custom ResquestAttributes and a custom job - or even a custom request scope.

Please refer to this article: https://medium.com/@pranav_maniar/spring-accessing-request-scope-beans-outside-of-web-request-faad27b5ed57

For more information on spring scopes: https://www.baeldung.com/spring-bean-scopes

Ismael Sarmento
  • 844
  • 1
  • 11
  • 22