package com.baiyz.test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.sensorsdata.utils.HttpDriver; import com.sensorsdata.utils.MyETLRuntimeException; import lombok.extern.slf4j.Slf4j; import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.core5.http.ParseException; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.http.Consts; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicBoolean;
@Slf4j public class TestTokenFetcher {
private static final AtomicBoolean shuShuoTokenStatus = new AtomicBoolean(); private static String token = "";
public static String refreshToken() throws IOException, ParseException { if (shuShuoTokenStatus.compareAndSet(false, true)) { log.info("开始刷新token" + Thread.currentThread().getName()); JSONObject reqBody = new JSONObject(); String url = ""; HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(reqBody.toJSONString(), Consts.UTF_8); httpPost.setHeader("Content-Type","application/json;charset=UTF-8"); httpPost.setEntity(stringEntity); try (CloseableHttpResponse response = HttpDriver.client.execute(httpPost)) { final String content = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); JSONObject jsonObject = JSON.parseObject(content); token = (String) jsonObject.getJSONObject("result").get("token"); return token; } finally { shuShuoTokenStatus.set(false); } } throw new RuntimeException("抛出自定义异常信息"); }}
|