gpt4 book ai didi

java - Spring 3 通过注释进行任务调度在服务器上同时运行多个实例

转载 作者:行者123 更新时间:2023-12-02 06:02:55 27 4
gpt4 key购买 nike

我正在使用 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>

获取设备状态作业.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 ***");
}
}
}

最佳答案

正如@MaciejWalkowiak所说,以及manual说,

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.

记录到您的作业中。如果是同一个实例,则调度会以某种方式错误地触发。如果没有,您将实例化预定的类三次。

更新:由于您说它是特定于环境的,所以我对此进行了更多思考,并注意到线程池名称不同,但它们都是各自池中的线程 1。这意味着您有多个 ThreadPoolTask​​Executors。您是否以某种方式创建多个容器?<​​/p>

关于java - Spring 3 通过注释进行任务调度在服务器上同时运行多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22528587/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com