子線程不能調用父線程的FeignClient服務解決方案

  • 因爲要導入數據到Hive中,故開啓了一個新的線程,導入完數據後,要調用另一個服務的接口version+1然後保存到數據庫中。
  • 開啓新線程:
@Override
    public Map<String, Object> loadExceToHive(MultipartFile file, Long nodeId, String importType, CurrUserInfo currUserInfo,String token) throws Exception {
       //....
        //開啓線程
        Thread thread = new Thread(new TagUdfDataImportThread(realPath, importDataVo, txtPath2, importType, importLogDO.getId(), tagUdfDO, currUserInfo, mainKeyFields, modelDO, webDate, p_exchange_date,token));
        thread.start();
        Map<String, Object> map = new HashMap<>();
        map.put("id", importLogDO.getId());
        return map;
    }
  • 線程中調用FeignClient服務
 @Override
    public void run() {
        try {
           //        加載完成後修改自定義標籤的版本號+1
       MetaServiceClient metaServiceClient = SpringApplicationContextHolder.getBean(MetaServiceClient.class);
        this.tagUdfDO.setVersion(this.tagUdfDO.getVersion()+1);
        metaServiceClient.tagUdfUpdate(this.tagUdfDO);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • FeignClient內代碼
@Component
@FeignClient(value = "meta-service", configuration = FeignConfiguration.class)
public interface MetaServiceClient {

    //自定義標籤修改
    @PostMapping("/tagUdf/update")
    ApiResult tagUdfUpdate(@RequestBody TagUdfDO tagUdfDO);

}
  • 這樣調用會出現空指針異常,主線程可以調用成功,子線程調用失敗。

解決方案:子線程中,新開啓一個請求,代碼如下:

@Override
    public void run() {
        try {
           //        加載完成後修改自定義標籤的版本號+1
		       TagUdfService tagUdfService = SpringApplicationContextHolder.getBean(TagUdfService.class);
		       this.tagUdfDO.setVersion(this.tagUdfDO.getVersion()+1);
               tagUdfService.tagUdfUpdate(tagUdfDO,token);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
  • tagUdfService中的代碼
/**
     * 自定義標籤的修改
     * @param tagUdfDO
     */
    @Override
    public void tagUdfUpdate(TagUdfDO tagUdfDO,String token) {
        //headers
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("token", token);
        requestHeaders.add("content-type", MediaType.APPLICATION_JSON_UTF8.toString());
        //body
        MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        String json = JSON.toJSONString(tagUdfDO);
        log.info(json);
        JSONObject jsonObject =JSONObject.parseObject(json);
        for(String key:jsonObject.keySet()){
            requestBody.add(key, jsonObject.getString(key));
        }
        //HttpEntity
//        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(requestBody, requestHeaders);
        HttpEntity<String> requestEntity = new HttpEntity<String>(json, requestHeaders);
        //post
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://meta-service/tagUdf/update", requestEntity, String.class);
        log.info(" responseEntity :"+responseEntity.getBody());
    }
  • 子線程中調用成功
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章