springMVC xml配置定时器 SpringBoot定时器

36 篇文章 0 订阅
12 篇文章 0 订阅

定时器方法不能带参数

 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");
        }
    }

}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值