spring定时查询

package org.hopeframework.biz.service.impl;

import Util.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.hopeframework.client.biz.service.MaintainService;
import org.hopeframework.db.mapper.DataMapper;
import org.hopeframework.db.mapper.MaintainMapper;
import org.hopeframework.db.model.Maintain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 功能描述:
* 版本信息: Copyright (c)2017
* 公司信息: 杭州佐通科技有限公司
* 开发人员: xiaohuang
* 版本日志: 1.0
* 创建日期: 2018/3/20 0020
* 创建时间: 10:29
* 修改历史:
* 时间 开发者 版本号 修改内容
* ——————————————————————
* 2018/3/20 0020 xiaohuang 1.0 1.0 Version
*/
@Configuration
@Service
@EnableScheduling
public class MaintainQueryToUpdate {
//ApplicationContext springContext = SpringContextUtil.getApplicationContext();
//protected MaintainService maintainService =(MaintainService)springContext.getBean(MaintainService.class);
//MaintainServiceImpl maintainService=new MaintainServiceImpl();
//@Reference(version = MaintainService.VERSION)
//@Resource
@Autowired
private MaintainMapper maintainMapper;
@Autowired
private DataMapper dataMapper;
private static final Logger logger= LoggerFactory.getLogger(MaintainQueryToUpdate.class);
@Scheduled(cron = “0 0 0/1 * * ?”)//每过一小时执行一次
public void queryMaintain(){
logger.info(“每小时执行一次查询==========================================”);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
//List maintainList=maintainMapper.getAllSetMaintainData();
List maintainList=maintainMapper.getAllSetMaintainData();
System.out.println(maintainList.size());
List overdueMaintainList=new ArrayList<>();
for (Maintain maintain : maintainList) {
String tainplan=maintain.getMaintainplan();
Integer time=Integer.parseInt(tainplan);
String maintainsettime=maintain.getMaintainsettime();
//SimpleDateFormat simpleDateFormat=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date parse;
int hours=0;
try {
parse = simpleDateFormat.parse(maintainsettime);
hours= DateUtil.diffDateToHour( new Date(),parse);
} catch (ParseException e) {
e.printStackTrace();
}
if(maintain.getTimesource()==”系统时间”){
if(hours>time){
overdueMaintainList.add(maintain);
}

        }else if(maintain.getTimesource()=="风机运行时间"){
            Integer id=maintain.getMonitorplaceid();
            int sumHour=dataMapper.selectByMonitorplaceid(id);
            if(sumHour>time){
                overdueMaintainList.add(maintain);
            }
        }else if(maintain.getTimesource()=="变频器运行时间"){
            Integer id=maintain.getMonitorplaceid();
            int sumHour=dataMapper.selectByMonitorplaceid1(id);
            if(sumHour>time){
                overdueMaintainList.add(maintain);
            }
        }
    }
    for(Maintain maintain : overdueMaintainList){
        maintain.setIsalert(1);
        maintain.setAlerttime(simpleDateFormat.format(new Date()));
        maintain.setAlerttype("保养");
        //maintain.setIsdeleted(0);
        maintainMapper.updateByPrimaryKeySelective(maintain);
    }
}

}
注意:本来想是放在controller里面的 结果该项目是dubbo分层管理的 dubbo创建的bean和spring创建的bean版本不一样 造成无法获取bean 所以写在了这里面
报错示例
这里写图片描述
这里写图片描述
网上说是由于service有事务 被事务代理的spring service 使用注解方式发布Dubbo服务 使用 @com.alibaba.dubbo.config.annotation.Service 发布dubbo服务的时候,当服务类没有加入@Transactional的时候没有问题.
但是当加入事务后【@Transactional】, dubbo的 AnnotationBean 扫描 类执行下面的代码的时候就获取不到对应的注解,也就发布不了服务。

究其原因,是因为Dubbo提供的注解没有@Inherited元注解。
详细说明见
http://blog.csdn.net/yushitao110223/article/details/51050662

猜你喜欢

转载自blog.csdn.net/hhc0917/article/details/79626246