测试使用Timer定时调用http接口

1.编写Timer工具类

package com.hontye.parameter.util;

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

public class TimerUtil {

    private ScheduledExecutorService scheduledExecutorService;
    /**
     * @param runnable 方法
     * @param time 延迟执行时间
     * @param    period 执行周期
     */
    public void scheduleAtFixedRate(Runnable runnable,Long time,int period ){
        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleAtFixedRate(runnable, time, period,TimeUnit.SECONDS);
    }

    public void shutdown(){
        if(!scheduledExecutorService.isShutdown()){
            scheduledExecutorService.shutdown();
        }

    }
}
2.编写调用http接口的工具类

package com.hontye.parameter.util;

import java.io.*;
import java.net.*;
import java.util.Random;

public class HttpUtil {
    public String httpUrl(){
        //模拟调用http://192.168.1.103:8081/setParameter?key="+key+"&value="+value接口
        Random random=new Random();
        String value = String.valueOf(random.nextFloat());
        String key = "";
        try {
            String keys[] = {"FL001", "FL002", "DZL001", "DZL002", "DZL003"};
            key = keys[random.nextInt(5)];
        }catch(Throwable e){
            System.out.println("发现异常:"+e);
        }
        String urlString1 = "http://192.168.1.103:8081/setParameter?key="+key+"&value="+value;
        String response = "";

        try {
            //请求的webservice的url
            URL url = new URL(urlString1);

            //创建http链接
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            //设置请求的方法类型
            httpURLConnection.setRequestMethod("POST");

            //设置请求的内容类型
            httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

            //设置发送数据
            httpURLConnection.setDoOutput(true);
            //设置接受数据
            httpURLConnection.setDoInput(true);

            //发送数据,使用输出流
            OutputStream outputStream = httpURLConnection.getOutputStream();
            //发送的soap协议的数据
            //String requestXmlString = requestXml("北京");

            String content = "user_id="+ URLEncoder.encode("13846", "gbk");

            //发送数据
            outputStream.write(content.getBytes());

            //接收数据
            InputStream inputStream = httpURLConnection.getInputStream();

            //定义字节数组
            byte[] b = new byte[1024];

            //定义一个输出流存储接收到的数据
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            //开始接收数据
            int len = 0;
            while (true) {
                len = inputStream.read(b);
                if (len == -1) {
                    //数据读完
                    break;
                }
                byteArrayOutputStream.write(b, 0, len);
            }
            //从输出流中获取读取到数据(服务端返回的)
            response = byteArrayOutputStream.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("模拟调用数据监控接口:发生变化的指标:"+key+",指标值:"+value+"。受影响的数据-----:"+response);
        return response;
    }
}

3.编写Controller类()---(开关控制)

package com.hontye.parameter.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hontye.parameter.util.HttpUtil;
import com.hontye.parameter.util.TimerUtil;

@Controller
@RequestMapping("/HttpTest")
public class HttpTestController {
    TimerUtil timerUtil;
    @RequestMapping(value = "/openTimer")
    @ResponseBody
    public String openTimer(int period){
        //匿名方法
        Runnable runnable = () -> {
            HttpUtil httpUtil = new HttpUtil();
            httpUtil.httpUrl();
        };
        final long time = 5;//延迟执行实际:秒
    	timerUtil = new TimerUtil();
        timerUtil.scheduleAtFixedRate(runnable,time,period);
        System.out.println("模拟调用数据监控接口已开启!模拟数据变化频率:"+period+"秒");
        return "模拟调用http://192.168.1.103:8081/setParameter?key=\"+key+\"&value=\"+value接口已开启!模拟数据变化频率:"+period+"秒";
    }

    @RequestMapping(value = "/shutdownTimer")
    @ResponseBody
    public String shutdownTimer(){
        timerUtil.shutdown();
        System.out.println("模拟调用数据监控接口已关闭");
        return "模拟调用http://192.168.1.103:8081/setParameter?key=\"+key+\"&value=\"+value接口已关闭";
    }
}

4.

调用HttpTest接口开启测试状态

调用shutdownTimer接口开启测试状态


你可能感兴趣的:(功能实现)