周期性的任务:从共享单车官网获取站点状态数据

周期性的任务:从共享单车官网获取站点状态数据
(1) 该任务在项目启动的时候,就开始执行。
(2)所以该任务应该交由Spring容器管理。

package cn.tedu.nybike.conf;

import org.apache.hadoop.fs.FileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 周期性的任务:从共享单车官网获取站点状态数据
 *      该任务在项目启动的时候,就开始执行。
 *      所以该任务应该交由Spring容器管理。
 */
@Component
public class GetStationStatus {
    @Autowired
    private FileSystem fs;

    // 通过ScheduledExecutorService实现周期性的任务。
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    String url = "https://gbfs.citibikenyc.com/gbfs/en/station_status.json";

    public GetStationStatus(){
//        scheduleAtFixedRate() 该方法需要四个参数:
//                  1. 周期性任务(Runnable--线程) 2. 第一次启动任务时,延迟时间
//                  3. 周期性任务的间隔时间 4. 时间单位
        MyRunnable runnable = new MyRunnable();
        service.scheduleAtFixedRate(runnable,       // 周期性任务
                0,                                  // 第一次启动任务的延迟时间
                10,                                 // 任务的间隔时间
                TimeUnit.SECONDS);                  // 时间的单位
    }

    class MyRunnable implements Runnable{
        private RestTemplate template;

        public MyRunnable(){
            // 创建工厂对象
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            // 设置连接超时时间和数据读取时间
            factory.setConnectTimeout(5000);
            factory.setReadTimeout(5000);
            // 初始化 RestTemplate
            template = new RestTemplate(factory);
        }

        @Override
        public void run() {
            // 当前URL的请求,允许失败4次
            for(int i=0; i<5; i++) {
                String statusStr = template.getForObject(url, String.class);
                if(statusStr != null){
                    System.out.println(statusStr.length());
                    // 将查询得到数据保存在文件中:文件名称 当前的时间(07/09/11:50:xx)
                    // 使用 fs 对象将保存的文件上传HDFS   /stations/status/
                    break;
                }
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值