关于webmagic定时爬取,修改任意时间,依次

/**
     * 启动时间的改变
     * @param id
     */
    @Override
    public void changeConfig(Serializable id) {
        SpiderList spiderList = spiderListMapper.get(id);
        String method = spiderList.getMethod();
        String startTime = spiderList.getStartTimeSet();
        String[] split = startTime.split(",", 4);
        String string3 = null;
        for (int i = 0; i < split.length; i++) {
            char[] charArray = split[i].toCharArray();
            String cron = ConfigUtil.charToString(charArray);
//            char temp = charArray[0];
//            charArray[0] = charArray[3];
//            char temp2 = charArray[1];
//            charArray[1] = charArray[4];
//            charArray[3] = temp;
//            charArray[4] = temp2;
//            string3 = new String(charArray);
//            String replace = charToString.replace(":", " ");
//            String cron = "00 " + replace + " * * ?";
            // 读取XML文件,获得document对象
            SAXReader reader = new SAXReader();
            URI uri = null;
            Document document = null;
            try {
                uri = Thread.currentThread().getContextClassLoader().getResource("applicationContext-quartz.xml")
                        .toURI();
                // File file =new );
                document = reader.read(new File(uri));
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 获取文档的根节点
            Element root = document.getRootElement();

            // 取得某个节点的子节点.
            List<Element> childElements = root.elements("scheduled-tasks");
            for (Element child : childElements) {
                List<Element> elements = child.elements();
                for (Element element : elements) {
                    Attribute cronAtt = element.attribute("cron");
                    Attribute methAtt = element.attribute("method");
                    String method1 = methAtt.getValue();
                    if (method.equals(method1)) {
                        element.setAttributeValue("cron", cron);
                    }
                }
            }
            
            // 重新写入xml文件
            FileWriter newFile;
            try {
                newFile = new FileWriter(new File(uri));
                XMLWriter newWriter = new XMLWriter(newFile);
                newWriter.write(document);
                newWriter.close();
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String currentTimeStr = sdf.format(date);
                Date currentTimeDate = sdf.parse(currentTimeStr);
                Date coreTime = sdf.parse(split[i]);
                long time = coreTime.getTime();
                long time2 = currentTimeDate.getTime();
                if (currentTimeDate.getTime() < coreTime.getTime()) {
                    //循环  如果当前的时间 比定时时间小 就是 在定时时间之前 就把 数据库定时时间 设置到定时的xml文件中  
                    AbstractRefreshableApplicationContext abstractRefreshableApplicationContext = new ClassPathXmlApplicationContext(
                            "classpath:applicationContext.xml");
                    abstractRefreshableApplicationContext.refresh();
                    return;
                }else if(currentTimeDate.getTime() > coreTime.getTime()) {
                    System.out.println("时间初始化设置!!!");
                    //如果当前的时间都比 定时时间大  那么就把  定时的时间 设置为第一个 最早的时间 
                    char[]  splitInit = split[0].toCharArray();
                    String coreInit = ConfigUtil.charToString(splitInit);
                    System.out.println("coreInit======="+coreInit);
                    for (Element child : childElements) {
                        List<Element> elements = child.elements();
                        for (Element element : elements) {
                            Attribute cronAtt = element.attribute("cron");
                            Attribute methAtt = element.attribute("method");
                            String method1 = methAtt.getValue();
                            if (method.equals(method1)) {
                                element.setAttributeValue("cron", coreInit);
                                AbstractRefreshableApplicationContext abstractRefreshableApplicationContext = new ClassPathXmlApplicationContext(
                                        "classpath:applicationContext.xml");
                                abstractRefreshableApplicationContext.refresh();
                                return;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

转载于:https://my.oschina.net/moonroot/blog/805758

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值