我正在使用 spring 调度程序来调度作业。它在本地运行良好,但在服务器上它为同一个实例运行多次。
从服务器登录
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 ***
应用程序上下文.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 ***");
}
}
}