目录
需求描述:
有几个网站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()));
}
}
启动项目后