1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| 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("抛出自定义异常信息"); }}
|