Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring3通过在服务器上同时运行多个实例的注释进行任务调度_Java_Multithreading_Spring_Spring Mvc_Spring Scheduled - Fatal编程技术网

Java Spring3通过在服务器上同时运行多个实例的注释进行任务调度

Java Spring3通过在服务器上同时运行多个实例的注释进行任务调度,java,multithreading,spring,spring-mvc,spring-scheduled,Java,Multithreading,Spring,Spring Mvc,Spring Scheduled,我正在使用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 - *

我正在使用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 ***
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 ***");
    }
}
}
@服务
公共类GetDeviceStatusJob{
专用静态最终记录器=记录器
.getLogger(GetDeviceStatusJob.class);
@自动连线
专用设备服务设备服务;
公共设备服务getDeviceService(){
返回设备服务;
}
公共无效设置设备服务(设备服务设备服务){
this.deviceService=设备服务;
}
@计划(cron=“0/10***?”)
public void getLostDeviceInfo(){
List deviceList=deviceService.getAllLostDevices();
if(deviceList!=null&&!deviceList.isEmpty()){
用于(设备:deviceList){
字符串gcmRegistrationId=device.getGcmRegistrationId();
如果(gcmRegistrationId!=null){
字符串状态=空;
if(device.isStatus()){
状态=“丢失”;
}否则{
status=“Active”;
}
String message=device.getMessage();
字符串jsonString=“{\”状态\“:\”+状态
+“\”,“\”消息\“:\”+消息
+“\”,“\”注册ID\”:[\“”
+gcmRegistrationId+“\”]}”;
System.out.println(jsonString);
NetClientUtil.httpGcmPostClient(jsonString,
“getLostDeviceInfo”);
}否则{
logger.info(“**gcmrregistrationid不存在于设备”+设备.getId());
}
}
}否则{
logger.info(“***无丢失设备***”);
}
}
}

正如@MaciejWalkowiak所说,正如

确保您没有初始化同一数据库的多个实例 @在运行时调度注释类,除非您确实要调度 回调每个这样的实例。与此相关,请确保 不要在带有注释的bean类上使用@Configurable @计划并注册为容器中的常规SpringBean: 否则,一旦通过 容器和一次通过@Configurable方面 每个@Scheduled方法被调用两次的结果

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


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

请查看日志。您可以看到同一个作业GetDeviceStatusJob同时运行三次。您可以用XML定义bean,并使用@Service进行注释。你确定你不会在不同的上下文中创建这个类的3个实例吗?我确定我不会再创建任何实例。但是,是的,我可以从xml中删除bean实例。我觉得这是一个错误。我会删除它,但不认为会有任何区别。我认为服务和bean一起造成了问题。现在我正在观察日志非常稳定。是的,如果您看到代码,您将看到我在类上和应用程序上下文中使用了@service。我觉得这造成了问题。现在,由于我已经从appcontext中删除了bean def,它工作得很好