springMVC xml配置定时器 SpringBoot定时器

定时器方法不能带参数

 SpringMVC:



    
    
    
    
    
    
    
    
    

    
    
    
        
        
        
    

Springboot:


import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.seimicrawler.xpath.JXDocument;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@EnableScheduling
public class ProjectScheduler{
    //每隔2秒执行一次
    @PostConstruct
    @Scheduled(fixedRate = 10000)
    public void projectTask() throws IOException, HttpStatusException {
        Document document=null;
        String url="https://www.jianshu.com/p/029c6108067a";
        Connection con = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")
                .timeout(30000); // 设置连接超时时间
        Connection.Response response = con.execute();

        if (response.statusCode() == 200) {
            document = con.get();
        } else {
        }
        if(document!=null){
            JXDocument jxd = new JXDocument(document.getElementsByTag("html"));
            Element article = (Element) jxd.selOne("//*[@id=\"__next\"]/div[1]/div/div/section[1]/article");
            List ps=article.getElementsByTag("p");
            Map map=new HashMap();
            for (int i = 0; i < ps.size(); i++) {
                JSONObject jsonObject=JSONObject.parseObject(ps.get(i).text());
                map.put(jsonObject.get("id").toString(),jsonObject.get("value").toString());
            }
            if("0".equals(map.get("project0001"))){
                YamlUtil.updateTaml(this,"project0001","0");
            }
            else {
                YamlUtil.updateTaml(this,"project0001","1");
            }
        }else {
            YamlUtil.updateTaml(this,"project0001","1");
        }
    }

}

 

你可能感兴趣的:(自用,后端,java)