TokenTask.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package info.aspirecn.iov.sjjh.supplier10000053.task;
  2. import info.aspirecn.iov.sjjh.supplier10000053.constant.CommonConstant;
  3. import info.aspirecn.iov.sjjh.supplier10000053.service.ChannelService;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.data.redis.core.StringRedisTemplate;
  8. import org.springframework.scheduling.annotation.Scheduled;
  9. import org.springframework.stereotype.Component;
  10. /**
  11. * @author xusonglin
  12. * @version V1.0
  13. **/
  14. @Component
  15. @Slf4j
  16. public class TokenTask {
  17. @Autowired
  18. StringRedisTemplate stringRedisTemplate;
  19. @Autowired
  20. ChannelService channelService;
  21. @Scheduled(cron = "0 0/10 * * * ?")
  22. public void tokenTask() {
  23. String redisToken = stringRedisTemplate.opsForValue().get(CommonConstant.REDIS_TOKEN_KEY);
  24. if (StringUtils.isNotBlank(redisToken)) {
  25. String[] redisTokenArray = redisToken.split("&&");
  26. String accessTokenTimestamp = redisTokenArray[1];
  27. if (System.currentTimeMillis() - Long.parseLong(accessTokenTimestamp) >= 1800000) {
  28. String token = channelService.queryAccessToken(1500);
  29. log.info("supplier10000053.tokenTask-token即将过期,间隔{},重新获取token:{}", System.currentTimeMillis() - Long.parseLong(accessTokenTimestamp), token);
  30. } else {
  31. log.info("supplier10000053.tokenTask-还在有效期内,token间隔毫秒:{}", System.currentTimeMillis() - Long.parseLong(accessTokenTimestamp));
  32. }
  33. } else {
  34. String token = channelService.queryAccessToken(1500);
  35. log.info("supplier10000053.tokenTask-token不存在,重新获取token:{}", token);
  36. }
  37. }
  38. }