BiswajitP
BiswajitP

Reputation: 233

Spring 3 Task Scheduling via Annotations running multiple instances at same time on server

I am using spring scheduler for scheduling job. It is working fine in local but on server it is running multiple times for same instance.

Log from server

20 Mar 2014 09:00:00 [pool-3-thread-1] INFO  com.yourkey.jobs.GetDeviceStatusJob  - *** No Lost Devices ***
20 Mar 2014 09:00:00 [pool-5-thread-1] INFO  com.yourkey.jobs.GetDeviceStatusJob  - *** No Lost Devices ***
20 Mar 2014 09:00:00 [pool-4-thread-1] INFO  com.yourkey.jobs.GetDeviceStatusJob  - *** No Lost Devices ***

applicationcontext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    -------------------------------------
    -------------------------------------

    <task:annotation-driven />
    <bean id="syncBrandOffersJob" class="com.yourkey.jobs.SyncBrandOffersJob"></bean>
    <bean id="getDeviceStatusJob" class="com.yourkey.jobs.GetDeviceStatusJob"></bean>


</beans>

GetDeviceStatusJob.java

@Service
public class GetDeviceStatusJob {

private static final Logger logger = Logger
        .getLogger(GetDeviceStatusJob.class);

@Autowired
private DeviceService deviceService;

public DeviceService getDeviceService() {
    return deviceService;
}

public void setDeviceService(DeviceService deviceService) {
    this.deviceService = deviceService;
}

@Scheduled(cron = "0 0/10 * * * ?")
public void getLostDeviceInfo() {
    List<Device> deviceList = deviceService.getAllLostDevices();
    if (deviceList != null && !deviceList.isEmpty()) {
        for (Device device : deviceList) {
            String gcmRegistrationId = device.getGcmRegistrationId();
            if (gcmRegistrationId != null) {
                String status = null;
                if (device.isStatus()) {
                    status = "LOST";
                } else {
                    status = "Active";
                }
                String message = device.getMessage();

                String jsonString = "{\"status\":\"" + status
                        + "\",\"message\":\"" + message
                        + "\",\"registration_ids\" : [\""
                        + gcmRegistrationId + "\"]}";
                System.out.println(jsonString);
                NetClientUtil.httpGcmPostClient(jsonString,
                        "getLostDeviceInfo");
            }else{
                logger.info("**** gcmRegistrationId not present for device" + device.getId());
            }
        }
    }else{
        logger.info("*** No Lost Devices ***");
    }
}
}

Upvotes: 0

Views: 2998

Answers (1)

Emerson Farrugia
Emerson Farrugia

Reputation: 11353

As @MaciejWalkowiak said, and as the manual says,

Make sure that you are not initializing multiple instances of the same @Scheduled annotation class at runtime, unless you do want to schedule callbacks to each such instance. Related to this, make sure that you do not use @Configurable on bean classes which are annotated with @Scheduled and registered as regular Spring beans with the container: You would get double initialization otherwise, once through the container and once through the @Configurable aspect, with the consequence of each @Scheduled method being invoked twice.

Log this in your job. If it's the same instance, the scheduling is getting triggered incorrectly somehow. If not, you're instantiating the scheduled class three times.

Update: I thought about this a bit more since you said it's environment-specific, and noticed that the thread pool names are different, but they're all thread 1 in their respective pools. That implies you have multiple ThreadPoolTaskExecutors. Are you creating multiple containers somehow?

Upvotes: 2

Related Questions