@Component
public class ApplicationConfig implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
WebApplicationContext context;
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
this.context.getServletContext().setAttribute("logout", NanWeiConfiguration.LOGOUTBACK);
new Thread(new AccessTokenThread()).start();
}
}
public class AccessTokenThread extends Thread
{
private static Logger log = LoggerFactory.getLogger(AccessTokenThread.class);
private static String access_token_url=QztConfiguration.GETAPPTOCKEN;
public static String access_token_val;
@Override
public void run() {
while(true){
access_token_val= getAccessToken();
try {
Thread.sleep(6000*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static String getAccessToken() {
HashMap hashMap = null;
try {
hashMap = HttpUtils.sendRequestCommon(QztConfiguration.GETAPPTOCKEN,
new HashedMap(),
"GET", false,false);
Integer resultCode = (Integer) hashMap.get("code");
String data = (String) hashMap.get("data");
hashMap = JSON.parseObject(data, HashMap.class);
Integer errCode = (Integer) hashMap.get("errCode");
if (errCode.equals(0)) {
JSONObject tokenInfo = (JSONObject) hashMap.get("data");
String token = (String) tokenInfo.get("appToken");
QztConfiguration.appToken = token;
return token;
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}