通过Http调度服务器task执行时间区间

  1. 通过URL发送Json格式的Http请求
/**
 * @Auther: LiuY
 * @Date: 2018/5/21 08 48
 * @Desc:
 * @Version 1.0
 */
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class HttpSender {
    public static String JsonPost(String urlStr,JSONObject obj){
        String result = "";
        try {
            // 创建url资源
            URL url = new URL(urlStr);
            // 建立http连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出,post请求必须要求
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置文件类型:
            conn.setRequestProperty("contentType", "application/json");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            //转换为字节数组
            byte[] data = (obj.toString()).getBytes();
            // 设置文件长度
            conn.setRequestProperty("Content-Length", String.valueOf(data.length));

            // 开始连接请求
            conn.connect();
            OutputStream  out = conn.getOutputStream();
            // 写入请求的字符串
            out.write((obj.toString()).getBytes());
            out.flush();
            out.close();

            // 请求返回的状态
            if (conn.getResponseCode() == 200) {
                // 请求返回的数据
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                try {

                    String line;
                    while ((line = in.readLine()) != null) {
                        result += line;
                    }
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } else {
                System.err.println("request: "+urlStr+" param: "+obj.toJSONString()+" failed: "+conn.getResponseCode());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
}
  1. 遍历时间段调度请求(这里以天为单位,是可以调的)
import com.alibaba.fastjson.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Auther: LiuY
 * @Date: 2018/5/21 08 51
 * @Desc:
 * @Version 1.0
 */
public class TestHttpSender {


    public static String URL = "http://localhost:8080//schedule/scheduleService/syncLotteryStaticss";

    public static void main(String[] args) {


        Calendar start = Calendar.getInstance();
        start.set(2018, 0, 1);
        Long startTIme = start.getTimeInMillis();

        Calendar end = Calendar.getInstance();
        end.set(2018, 4, 22);
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24l;
        Long time = startTIme;


        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String date = df.format(d);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("date",date);
            //这里是返回结果,酌情自行处理
            String result = HttpSender.JsonPost(URL,jsonObject);
            String text = "failed";
            if("null".equals(result)){
                text="success";
            }
            System.out.println(date+": "+text);
            time += oneDay;
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值