后台--模拟推送数据

用定时器和生成随机数
在这里插入图片描述

import com.fang.entity.IotSlWarnDataEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.Random;

@Component
@RestController
@RequestMapping("/random")
public class SimulationController {
    private static Logger log = LoggerFactory.getLogger(SimulationController.class);

    RestTemplate restTemplate = new RestTemplate();

    // 这个cron表达式代表从0秒开始,每过1秒执行一次。
    @Scheduled(cron = "0/5 * * * * ?")
    @RequestMapping("/value")
    public void getValue() {
        IotSlWarnDataEntity entity = new IotSlWarnDataEntity();
        Random random=new Random();
           //生成[-5,40)区间的整数
       // String x1= String.valueOf(Math.abs(random.nextInt()%45)-5);
        //log.info("发送x1:"+x1);
        //生成[-10,30)区间的整数
       // String x2= Math.abs(random.nextInt()%40)-10+"";
       // log.info("发送x2:"+x2);
        //生成[-15,20)区间的整数
        //String x3= Math.abs(random.nextInt()%35)-15+"";
        //        log.info("发送x3:"+x3);
        
        String x1 = (random.nextInt(100)-10)+"";
//        log.info("发送x1:"+x1);
        String x2 = (random.nextInt(100)-10)+"";
//        log.info("发送x2:"+x2);
        String x3 = (random.nextInt(100)-10)+"";
//        log.info("发送x3:"+x3);
        entity.setDeviceId(1);
        entity.setX1(x1);
        entity.setX2(x2);
        entity.setX3(x3);
        entity.setCreateTime(new Date());
        System.out.println("发送的数据:" + entity);
	    String url = "http://localhost:8080/pullData";
        //HttpHeaders 客户端向服务区发送请求时带的是请求头,而服务器响应客户端数据时带的是响应头。
        HttpHeaders responseHeader = new HttpHeaders();
        //响应实体
        HttpEntity<String> requestEntity = new HttpEntity(entity, responseHeader);
        //ResponseEntity 状态码
        ResponseEntity<String> responseEntity = this.restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, new Object[0]);
        System.out.println(responseEntity);
    }
}

如果要开启定时器 必加上该注解@EnableScheduling //开启定时器
在这里插入图片描述

@Component@Scheduled(cron = "0/5 * * * * ?")

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AZ8FmT5d-1662716041544)(C:\Users\JACK\AppData\Roaming\Typora\typora-user-images\image-20220909102637745.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值