工作中的spring定时任务

2018-07-31  本文已影响16人  先生_吕

直接案例代码

【定时案例1】

/**
 * Created by lvfang on 2018/3/2.
 * 业务方回调地址定时刷新
 */
@Service
public  class FriendlySaltService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private RcJunktextBusinessInfoDao rcJunktextBusinessInfoDao;

    public Map<String, RcJunktextBusinessInfo> infoMap = new ConcurrentHashMap<>();

    /**
     * 初始延迟 1秒钟 ; 固定延迟 1分钟
     * 及时刷新表rc_junktext_business_info业务方数据
     */
    @Scheduled(initialDelay = 1000, fixedRate = 60000)
    public void refresh() {
        infoMap = rcJunktextBusinessInfoDao.findAllList().stream().collect(Collectors.toMap(RcJunktextBusinessInfo::getBusinessUid, Function.identity()));
        logger.info("come into refresh , infoMap size : {}",infoMap.size());
    }

    /**
     * @param infoUid
     * @return
     * 问题描述:获取业务方信息(新代码。数据库获取)
     *
     * 原因说明:业务方信息从配置文件中获取造成每次有新的业务方接入时都要修改配置文件,不利于系统维护和业务方接入,增加工作量
     *
     * 解决方案:新建接入业务放表rc_junktext_business_info、将数据持久化到数据库中,每次获取从数据库中查询
     *
     * 修改日期:2018-07-20  吕方
     */
    public String getInfoMD5(String infoUid) {
        if (infoMap.isEmpty()) refresh();

        if(infoMap.get(infoUid)!=null){
            return infoMap.get(infoUid).getMd5Salt();
        }else{
            return null;
        }
    }
}

【定时案例2】

/**
 * @Author: LvFang
 * @Date: Created in 2018/5/29.
 * @Description:将mysql数据定时刷新至ES中
 */
@Component
public class ScheduleESJobs {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private NlpInfoService nlpInfoService;

    @Autowired
    private JestService jestService;

    public final static long SECOND = 1 * 1000;

    @Value("${job.execute.ip}")
    private String jobExecuteIp;

    FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    //SELECT * FROM rc_admin_nlp_info WHERE ctime BETWEEN '2018-05-29 11:00:15' AND '2018-05-29 14:00:15';

    @Scheduled(fixedDelay = SECOND * 60 * 5)
    public void fixedDelayNlpInfoToES() throws Exception {
        InetAddress addr = InetAddress.getLocalHost();
        String localIp=addr.getHostAddress().toString();
        //多节点部署保证只有一台机器定时刷新即可
        if(!jobExecuteIp.equals(localIp)){
            //System.out.println("IP不对");
        }else{
            String nowTime = fdf.format(new Date());//2018-04-11 18:42:00
            String oldTime = fdf.format(new Date().getTime()-(SECOND*60*10));
            RcASMsgVo rcASMsgVo = nlpInfoService.findNlpInfoListLast(oldTime,nowTime);//"2018-04-11 18:40:00","2018-04-11 18:42:30"

            List<RcAdminNlpInfo> infoList = (List<RcAdminNlpInfo>)rcASMsgVo.getData();
            int count = infoList.size();
            for(RcAdminNlpInfo info:infoList){

                ESNlpInfoDoc infoDoc = new ESNlpInfoDoc(info.getId()+"",
                        info.getUserName(),info.getTitle(),
                        info.getContent(),info.getCtime().getTime());

                JestResult jestResult = jestService.insertDocument(
                        ESConstants.INDEX_NAME_NLP,
                        ESConstants.INDEX_TYPE_NLPINFO,
                        infoDoc);
                int code = jestResult.getResponseCode();
                if(code!=200){
                    count--;
                    logger.error("ID为:【" + info.getId() + "】的info对象同步ES库失败");
                }
            }
            logger.info("此次应入口 "+infoList.size()+" 条,实际入库数据有: " + count +"  条");
        }
    }
}

over!!!!!!!!!!!

上一篇下一篇

猜你喜欢

热点阅读