定時器方法不能帶參數
SpringMVC:
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:component-scan base-package="com.task" />
<!--這是使用xml方式的開關-->
<task:annotation-driven/>
<!-- spring初始化完成後 調用某個類的方法 -->
<bean id="cacheInitializingBean" class="com.task.CacheCron" init-method="cron"/>
<!-- 定時器配置 -->
<bean id="hotListCron" class="com.task.HotListCron" />
<bean id="carouselCron" class="com.task.CarouselCron" />
<bean id="cacheCron" class="com.task.CacheCron" />
<task:scheduler id="scheduler" pool-size="10" />
<!-- 0/3 * * * * ? --><!-- 0 0 0 ? * SUN -->
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="hotListCron" method="cron" cron="0 0 0 ? * SUN" />
<task:scheduled ref="carouselCron" method="cron" cron="0 0 0 ? * SUN"/>
<task:scheduled ref="cacheCron" method="cron" cron="0 0 */3 * * ?"/>
</task:scheduled-tasks>
</beans>
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<Element> 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");
}
}
}