定時隨機訪問博客

目錄

 

需求描述:

邏輯:

實現:

效果展示:


需求描述:

有幾個網站url,定時、隨機訪問這些url。

邏輯:

定時任務--》組裝需要訪問的url集合--》實現隨機獲取其中的一個url--》http訪問此url--》隨機睡眠幾秒

實現:

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: chenping
 * @Date: 2020-4-20
 */
@Slf4j
@Component
public class GetFromUrilController {

    //任務執行完畢後8秒後再執行
    @Scheduled(fixedDelay = 8000L)
    public void auditingTask() throws InterruptedException {
        log.info("開始查閱博客----");
        List<String> urls = new ArrayList<>();
        urls.add("https://blog.csdn.net/chenping1993/article/details/104617424");
        urls.add("https://blog.csdn.net/chenping1993/article/details/104008477");
        urls.add("https://blog.csdn.net/chenping1993/article/details/103881649");
        urls.add("https://blog.csdn.net/chenping1993/article/details/103416941");
        urls.add("https://blog.csdn.net/chenping1993/article/details/100734652");
        int index = (int)(Math.random()*5);//隨機獲取0-4的隨機數
        log.info("隨機數:{}",index);
        String url = urls.get(index);
        log.info("訪問地址:{}",url);
        String result = HttpUtil.HttpGet(url);
        log.info("結束查閱博客");

        //隨機睡眠0-10秒
        Thread.sleep((long) (10000*Math.random()));
    }
}

啓動項目後

效果展示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章