定时推送任务 Apache HttpClient/okhttp3

定时推送任务

需求

需要定时推送我方的数据到对方那边

方法1 Apache HttpClient

此方法指定推送过去的信息转为utf-8格式的json字符串

java 复制代码
@Scheduled(initialDelay = 1000 * 120, fixedDelay = 1000 * 60 * 5)
public void diseaseInterface() {
    String lockKey = "lock:diseaseInterface";
    String requestId = UUID.randomUUID().toString();
    boolean acquired = false;
    try {
        // 加锁,锁定时间为15分钟
        acquired = redisTemplate.opsForValue().setIfAbsent(lockKey, requestId, Duration.ofMinutes(15L));
        if (acquired) {
            MainBody mainBody = interfaceService.diseaseInterface(new WebServiceBO());
            if (mainBody != null) {
                String sendUrl = "对方的接口";
                
                // 如果在数据库中配置了接口信息,就从数据库中查询并赋值
                Code code = codeService.selectByPrimaryKey(DATABASE_CODE_PK_JXZ);
                if (code != null && StringUtils.isNotEmpty(code.getCode())) {
                    sendUrl = code.getCode();
                }
                
                
                CloseableHttpClient httpClient = HttpClientBuilder.create().build();
                HttpPost httpPost = new HttpPost(sendUrl);
                StringEntity entity = new StringEntity(JSON.toJSONString(mainBody), "utf-8");
                entity.setContentEncoding("utf-8");
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
                HttpResponse response;
                try {
                    
                    // 信息推送过去之后,对方会给返回值,结构就是常见的
                    //     {
                    //  code: 1,//接口返回码,成功0,失败1
                    //  msg: '',//提示详细信息,可以为空,可以为中文提示信息
                    //  data: {
                          //返回结果对象
                    //	}
                    //}
                    // data存放了已经同步过去的id,拿到id,我方这边更新字段,表示该信息已经同步,后续不用再同步
                    response = httpClient.execute(httpPost);
                    JSONObject resultJson = JSON.parseObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8).trim());
                    if (resultJson != null) {
                        Utils.debug(resultJson.toJSONString());
                        if (200 == resultJson.getInteger("code")) {
                           // 更新字段
                            recSubmitService.updateReportedStateByRecSubId(resultJson.getInteger("data"), STATUS_YES);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    } finally {
        // 释放锁
        Utils.debug("[释放锁]:" + lockKey);
        if (acquired) {
            redisTemplate.delete(lockKey);
        }
    }
}

方法2 okhttp3

第二个业主也我们需要推送信息过去,按照上面的写法遇到一个报错,

报错信息为

html 复制代码
<!DOCTYPE html>
<html>
  <head>
    <title>Error - 415</title>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">

    <style>
      body {
        padding: 50px 80px;
        font: 14px "Helvetica Neue", Helvetica, sans-serif;
      }

      h1 {
        font-size: 2em;
        margin-bottom: 5px;
      }

      pre {
        font-size: .8em;
      }
    </style>
  </head>
  <body>
    <div id="error">
      <h1>Error</h1>
      <p>Looks like something broke!</p>
      <pre>
        <code>
Error: Unsupported Content-Encoding: utf-8
    at inflate (D:\项目\道路巡检-2\ai-node-1\node_modules\inflation\index.js:35:15)
    at module.exports [as json] (D:\项目\道路巡检-2\ai-node-1\node_modules\co-body\lib\json.js:39:25)
    at parseBody (D:\项目\道路巡检-2\ai-node-1\node_modules\koa-bodyparser\index.js:87:26)
    at bodyParser (D:\项目\道路巡检-2\ai-node-1\node_modules\koa-bodyparser\index.js:67:25)
    at dispatch (D:\项目\道路巡检-2\ai-node-1\node_modules\koa-compose\index.js:42:32)
    at D:\项目\道路巡检-2\ai-node-1\node_modules\koa-compose\index.js:34:12
    at Application.handleRequest (D:\项目\道路巡检-2\ai-node-1\node_modules\koa\lib\application.js:186:12)
    at Server.handleRequest (D:\项目\道路巡检-2\ai-node-1\node_modules\koa\lib\application.js:157:21)
    at Server.emit (node:events:518:28)
    at parserOnIncoming (node:_http_server:1143:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17)
        </code>
      </pre>
    </div>
  </body>
</html>

直接返回了一个网页,看报错信息好像是node报错,不支持utf-8格式,具体怎么解决要看甲方那边调试。

有个现象就是将需要的信息用postman发送过去是正常了,但是写成方法一的代码就出错了,postman支持代码生成,看它提供了 okhttp3 的方法,直接拿来用了

java 复制代码
@Scheduled(initialDelay = 1000 * 120, fixedDelay = 1000 * 60 * 5)
public void diseaseInterfaceXC() {
    String lockKey = "lock:diseaseInterfaceOfXC";
    String requestId = UUID.randomUUID().toString();
    boolean acquired = false;
    try {
        // 加锁,锁定时间为15分钟
        acquired = redisTemplate.opsForValue().setIfAbsent(lockKey, requestId, Duration.ofMinutes(15L));
        if (acquired) {
            MainBody mainBody = interfaceService.diseaseInterfaceXC(new WebServiceBO());
            if (mainBody != null) {
                String sendUrl = "对方的接口";
                Code code = codeService.selectByPrimaryKey(DATABASE_CODE_PK_XC);
                if (code != null && StringUtils.isNotEmpty(code.getCode())) {
                    sendUrl = code.getCode();
                }

                // 换一种写法,不指定编码格式为utf-8
                OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType,JSON.toJSONString(WebResponse.ok(mainBody)));
                Request request = new Request.Builder()
                    .url(sendUrl)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
                Response response = client.newCall(request).execute();
                String responseBody = response.body().string();
                ObjectMapper objectMapper = new ObjectMapper();
                // 用实体类接受返回值
                ResultVO resultVO = objectMapper.readValue(responseBody, ResultVO.class);
                if (resultVO.getCode() == 200) {
                    recSubmitService.updateReportedStateByRecSubId(resultVO.getTaskId(), STATUS_YES);
                }
            }
        }

    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        // 释放锁
        Utils.debug("[释放锁]:" + lockKey);
        if (acquired) {
            redisTemplate.delete(lockKey);
        }
    }
}

ResultVO

JAVA 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultVO<T> {

    private Integer code;

    private Integer taskId;

    private T msg;
}
相关推荐
远歌已逝2 小时前
维护在线重做日志(二)
数据库·oracle
qq_433099403 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz3 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
工业甲酰苯胺5 小时前
Redis性能优化的18招
数据库·redis·性能优化
没书读了6 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
i道i6 小时前
MySQL win安装 和 pymysql使用示例
数据库·mysql
小怪兽ysl6 小时前
【PostgreSQL使用pg_filedump工具解析数据文件以恢复数据】
数据库·postgresql
wqq_9922502777 小时前
springboot基于微信小程序的食堂预约点餐系统
数据库·微信小程序·小程序
爱上口袋的天空7 小时前
09 - Clickhouse的SQL操作
数据库·sql·clickhouse
聂 可 以8 小时前
Windows环境安装MongoDB
数据库·mongodb