TS-
TS-

Reputation: 4411

Double execution (2 threads) of Spring cron job in AWS Beanstalk

I have a Java + Spring (3.2.9.RELEASE) stack deployed in AWS Elastic Beanstalk configured with max of 1 instance. However, I am noticing my background job is being executed twice in a row in that instance in 2 different threads.

The background job is scheduled as cron jobs, Spring set up:

<task:annotation-driven scheduler="scheduler" />
<task:scheduler id="scheduler" pool-size="5" />
<task:scheduled-tasks scheduler="scheduler">

    <task:scheduled ref="className1" method="methodName1"
        cron="${scheduler.property1name.cronschedule}" />

    <task:scheduled ref="className2" method="methodName2"
        cron="${scheduler.property2name.cronschedule}" />

    <task:scheduled ref="className3" method="methodName3"
        cron="${scheduler.property3name.cronschedule}" />

</task:scheduled-tasks>

For application logic reasons, methodName1 is the only one that actually execute tasks (methodName2 and other tasks does not meet application logic condition). Since I am noticing methodName1 being executed twice, I added logging to see where it's coming from:

public void methodName1()
{
    // DEBUGGING to see which machine is running in AWS
    try
    {
        java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
        logger.debug("Hostname of local machine: " + localMachine.getHostName() + "/ " + localMachine.getHostAddress());
    }
    catch(Exception ex)
    {
        logger.debug("Failed to retrieve machine name", ex);
    }
    // DEBUGGING

    actuallyExecuteMethodName1();
}

Now, this very same code has been running in our own internal server (single node set up) for the past year with no issue. Recently, we moved this application into AWS and more specifically to Elastic Beanstalk. We know we have not configured the background job to handle multi-node environnment, and thus we set up Elastic Beanstalk to have min/max of 1 instance until we make the necessary changes.

Looking at the application log from AWS:

04:15:00,002 [scheduler-3] DEBUG ClassName1- methodName1- Hostname of local machine: ip-###.###.###.###/ ###.###.###.###
04:15:00,003 [scheduler-2] DEBUG ClassName1- methodName1- Hostname of local machine: ip-###.###.###.###/ ###.###.###.###

The task is executed twice, each on a different thread 1 milliseconds apart. We are only seeing this behavior after we moved to AWS Elastic Beanstalk.

The app is running on: 64bit Amazon Linux 2014.03 v1.0.2 running Tomcat 7 Java 7

Thoughts?

Upvotes: 0

Views: 1049

Answers (1)

Rohit Banga
Rohit Banga

Reputation: 18918

I would recommend using the latest solution stack "64bit Amazon Linux 2014.03 v1.0.4 running Tomcat 7 Java 7" for your environment.

Upvotes: 1

Related Questions