Java Spring @Scheduled Tasks Executing Twice

Spring @Scheduled executing the job twice than the expected number of times

The problem is that you, probably, have the same component scan in both your JPAConfiguration and MvcConfig. The result is that you basically are loading your whole application twice (so unless you want to have memory-issues, weird transactional issues etc. that is the way to go).

What you want is that your ContextLoaderListener (the root context) loads everything but web related beans and the DispatcherServlet should load only web related beans (views, controllers, advices etc.).

The @ComponentScan has properties with which you can control this. In the JPAConfiguration add some excludeFilters on the MvcConfig disable the defaults and add some includeFilters.

Your @ComponentScan for the JPAConfiguration should contain the following:

@ComponentScan(
basePackages = {"com.prime.tutorials"},
excludeFilters = {
@ComponentScan.Filter( { Controller.class, ControllerAdvice.class })
})

Your MvcConfig you should use the same but as an includeFilters instead and disable the default filters.

@ComponentScan(
basePackages = {"com.prime.tutorials"},
useDefaultFilters = false,
includeFilters = {
@ComponentScan.Filter( { Controller.class, ControllerAdvice.class })
})

You don't want to do different base packages as that would be quite cumbersome. It is also IMHO that you shouldn't use technical separation as a way of creating packages (see also https://softwareengineering.stackexchange.com/questions/258196/java-application-structure-horizontal-vs-vertical-split/258197#258197) .

Spring @Scheduled task runs twice

I believe this is caused by same config file being loaded twice in your web.xml

<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_config.xml</param-value> <!-- FIRST -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_config.xml</param-value> <!-- SECOND -->
</context-param>

EDIT
To fix it:

Create another file servlet-servlet.xml (this will be picked up by the ServletDispatcher config by default since it matches the file by servlet name)
The file will contain this:

<beans>
<context:component-scan base-package="web.controllers"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
</beans>

Modify the original file (spring-config.xml):

<beans>
<task:annotation-driven />
<context:component-scan base-package="services"/>
<context:component-scan base-package="dao"/>
<context:component-scan base-package="scheduled"/>
<context:property-placeholder location="/WEB-INF/application.properties"/>
</beans>

Modify your web xml servlet config to following:

<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

Spring 4.3.1 scheduled task run twice

Solved creating a new class not implementing any interface (@Component annotated) containing the @Scheduled method.

Spring scheduler is running twice

I was able to fix the by removing the following line from tomcat config/server.xml. This line was deploying my app twice. I was able to see that from tocat manager. <Context docBase="myapp path="/myapp" reloadable="true" source="org.eclipse.jst.jee.server:myapp"/>

Thank you all for your inputs.

Spring - Bean executes method twice

When using the @EnableScheduling annotation, Spring creates a TaskExecutor in the background. This will schedule all the @Scheduled methods. In the case of methods with fixedDelay, they will be fired instantly (unless initialDelay is set).

You are also programatically executing the task, so you have two executions:

  • The one executed by Spring
  • The one executed in the main method.

You should remove the manual invocation, and everything should work as expected.

You can find more information in https://spring.io/guides/gs/scheduling-tasks/ and https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-task-scheduler



Related Topics



Leave a reply



Submit