Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduled tasks seem to run twice (Not in a Web environment) [SPR-10830] #15456

Closed
spring-projects-issues opened this issue Aug 13, 2013 · 2 comments
Labels

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 13, 2013

Macias De La Rosa Jesus Gerardo opened SPR-10830 and commented

The problem is, using @Scheduled(cron = "${cron.expression}") run twice. I am using setters variables ( or variables with @Autowired) that are Datasources. One time my setters variables are null, the other time these variables ar not null!

In the Main Program I only use:


public class MainImport {

private static ClassPathXmlApplicationContext classPathXmlApplicationContext;

public static void main(String[] args) throws InterruptedException {
	classPathXmlApplicationContext = new ClassPathXmlApplicationContext("/config/springJob.xml");
}

}

In SpringJob.xml:


<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>
<task:scheduler id="taskScheduler" pool-size="20"/>
<task:executor id="taskExecutor" pool-size="20"/>

<context:property-placeholder location="classpath:/config/*.properties" />
<context:component-scan base-package="dbimport" />

<!-- *** DATASOURCES *** -->
<!-- ******************* -->
   
 
<bean id="myOracle" class="dbimport.dao.DataSourceOracle"
    p:oracleDriver="${oracleW4BIS.driver}"
    p:oracleUrl="${oracleW4BIS.url}"
    p:oracleUsername="${oracleW4BIS.username}"
    p:oraclePassword="${oracleW4BIS.password}"
/>
                   
<!--  Which JOB should be executed -->
<bean id="JobToExecuteID" class="dbimport.job.JobMapping"
   p:oracleDatasource-ref="myOracle"
/>

Now in In JobMapping:


public void setOracleDatasource( DataSourceOracle dsOracle) {
this.dsOracle = dsOracle;
}

@Scheduled(cron = "${cron.expression}")
public void executeJob() {
List<Partner> lPartners = null;

	System.out.println("execute() --> Executing Job ");
	if ( dsOracle == null ) {
		System.out.println("FIRST TIME --> " + new Date() + " ... Oracle Connection is = null ");
	} else {
        System.out.println("SECOND TIME --> " + new Date() + " ... DATOS = " + dsOracle.getTestString());

 callMathod(dsOracle);
	}
}

Notice that cron.expression is configured to run each 30 seconds.
and my output shows as:

SECOND TIME Tue Aug 13 10:51:30 CEST 2013 ... DATOS = oracle.jdbc.OracleDriver, jdbc:oracle:thin:@139.X.Y.Z:1001:MYDB, MyUser, MyPassword
FIRST TIME --> Tue Aug 13 10:51:30 CEST 2013 ... Oracle Connection is = null

This is not a Web Application, I don't have Tomcat or something similar.
I am using Windows´7, 64 Bits, Java 7.
Is there a similar problem with this environment?


Affects: 3.2.3

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Sep 29, 2015

Anton Reshetnikov commented

Check you config again, may be do you use twice class-scanning or double imports of you spring beans?

Loading

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Sep 20, 2016

Tejas commented

Looks like your bean is getting created twice. First time with annotations, where you are getting data source as null, and another is from the xml configuration, where you have explicitly given the reference to myOracle bean, and hence not null.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant