我正在使用spring scheduler来安排工作。 它在本地工作正常,但在服务器上它运行多次同一个实例。
从服务器登录
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 ***");
}
}
}
答案 0 :(得分:2)
正如@MaciejWalkowiak所说,正如manual所说,
确保您没有初始化相同的多个实例 @Scheduled注释类在运行时,除非您确实要安排 回调每个这样的实例。与此相关,请确保你 不要在带有注释的bean类上使用@Configurable @Scheduled并注册为带有容器的常规Spring bean: 你会得到双初始化,一旦通过 容器和一次通过@Configurable方面,用 每个@Scheduled方法被调用两次的结果。
在您的工作中记录this
。如果它是同一个实例,则会以某种方式错误地触发调度。如果没有,您将三次实例化预定班级。
更新:我已经考虑过这个问题了,因为你说它是特定于环境的,并注意到线程池名称不同,但它们都是线程1在他们各自的池中。这意味着你有多个ThreadPoolTaskExecutors
。你是否以某种方式创建了多个容器?</ p>