当前位置:   article > 正文

测试使用Timer定时调用http接口_timer()里面调用接口

timer()里面调用接口

1.编写Timer工具类

  1. package com.hontye.parameter.util;
  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.ScheduledExecutorService;
  4. import java.util.concurrent.TimeUnit;
  5. public class TimerUtil {
  6. private ScheduledExecutorService scheduledExecutorService;
  7. /**
  8. * @param runnable 方法
  9. * @param time 延迟执行时间
  10. * @param period 执行周期
  11. */
  12. public void scheduleAtFixedRate(Runnable runnable,Long time,int period ){
  13. scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
  14. scheduledExecutorService.scheduleAtFixedRate(runnable, time, period,TimeUnit.SECONDS);
  15. }
  16. public void shutdown(){
  17. if(!scheduledExecutorService.isShutdown()){
  18. scheduledExecutorService.shutdown();
  19. }
  20. }
  21. }
2.编写调用http接口的工具类

  1. package com.hontye.parameter.util;
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.Random;
  5. public class HttpUtil {
  6. public String httpUrl(){
  7. //模拟调用http://192.168.1.103:8081/setParameter?key="+key+"&value="+value接口
  8. Random random=new Random();
  9. String value = String.valueOf(random.nextFloat());
  10. String key = "";
  11. try {
  12. String keys[] = {"FL001", "FL002", "DZL001", "DZL002", "DZL003"};
  13. key = keys[random.nextInt(5)];
  14. }catch(Throwable e){
  15. System.out.println("发现异常:"+e);
  16. }
  17. String urlString1 = "http://192.168.1.103:8081/setParameter?key="+key+"&value="+value;
  18. String response = "";
  19. try {
  20. //请求的webservice的url
  21. URL url = new URL(urlString1);
  22. //创建http链接
  23. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  24. //设置请求的方法类型
  25. httpURLConnection.setRequestMethod("POST");
  26. //设置请求的内容类型
  27. httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
  28. //设置发送数据
  29. httpURLConnection.setDoOutput(true);
  30. //设置接受数据
  31. httpURLConnection.setDoInput(true);
  32. //发送数据,使用输出流
  33. OutputStream outputStream = httpURLConnection.getOutputStream();
  34. //发送的soap协议的数据
  35. //String requestXmlString = requestXml("北京");
  36. String content = "user_id="+ URLEncoder.encode("13846", "gbk");
  37. //发送数据
  38. outputStream.write(content.getBytes());
  39. //接收数据
  40. InputStream inputStream = httpURLConnection.getInputStream();
  41. //定义字节数组
  42. byte[] b = new byte[1024];
  43. //定义一个输出流存储接收到的数据
  44. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  45. //开始接收数据
  46. int len = 0;
  47. while (true) {
  48. len = inputStream.read(b);
  49. if (len == -1) {
  50. //数据读完
  51. break;
  52. }
  53. byteArrayOutputStream.write(b, 0, len);
  54. }
  55. //从输出流中获取读取到数据(服务端返回的)
  56. response = byteArrayOutputStream.toString();
  57. } catch (MalformedURLException e) {
  58. e.printStackTrace();
  59. } catch (UnsupportedEncodingException e) {
  60. e.printStackTrace();
  61. } catch (ProtocolException e) {
  62. e.printStackTrace();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. System.out.println("模拟调用数据监控接口:发生变化的指标:"+key+",指标值:"+value+"。受影响的数据-----:"+response);
  67. return response;
  68. }
  69. }

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

  1. package com.hontye.parameter.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. import com.hontye.parameter.util.HttpUtil;
  6. import com.hontye.parameter.util.TimerUtil;
  7. @Controller
  8. @RequestMapping("/HttpTest")
  9. public class HttpTestController {
  10. TimerUtil timerUtil;
  11. @RequestMapping(value = "/openTimer")
  12. @ResponseBody
  13. public String openTimer(int period){
  14. //匿名方法
  15. Runnable runnable = () -> {
  16. HttpUtil httpUtil = new HttpUtil();
  17. httpUtil.httpUrl();
  18. };
  19. final long time = 5;//延迟执行实际:秒
  20. timerUtil = new TimerUtil();
  21. timerUtil.scheduleAtFixedRate(runnable,time,period);
  22. System.out.println("模拟调用数据监控接口已开启!模拟数据变化频率:"+period+"秒");
  23. return "模拟调用http://192.168.1.103:8081/setParameter?key=\"+key+\"&value=\"+value接口已开启!模拟数据变化频率:"+period+"秒";
  24. }
  25. @RequestMapping(value = "/shutdownTimer")
  26. @ResponseBody
  27. public String shutdownTimer(){
  28. timerUtil.shutdown();
  29. System.out.println("模拟调用数据监控接口已关闭");
  30. return "模拟调用http://192.168.1.103:8081/setParameter?key=\"+key+\"&value=\"+value接口已关闭";
  31. }
  32. }

4.

调用HttpTest接口开启测试状态

调用shutdownTimer接口开启测试状态


声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号