Java Spring @Scheduled tasks executing twice

前端 未结 23 1687
清歌不尽
清歌不尽 2020-11-29 09:07

I have a simple test method here that is set to run every 5 seconds and it does, but looking at the System.out you can see it appears to be doing something odd.



        
23条回答
  •  时光说笑
    2020-11-29 09:38

    I was declaring my class as a "Service" and also using the @Scheduled annotation to declare it as a Scheduler. The scheduler invokes it normally, but as we declared the class as a service. It was initiated twice.

    I removed the Service annotation. And declared that class as a bean in the context XML I have maintained. (custom context XML which is declared in web.xml). This fixed the issue for me. Hope it helps someone.

提交回复
热议问题