我正在构建与Java Spark中的Spotify API交互的API。我使用了用于令牌管理的授权代码流-这意味着令牌将在一个小时内(对于给定的用户)有效,然后需要刷新。
对于每个连接其Spotify帐户的用户,我都会创建一个计时器,该计时器将检查该用户在50分钟后是否处于活动状态:
如果是->我刷新用户的令牌。如果不是->我将删除用户以及用户的令牌,这意味着如果他们要使用我的服务(出于存储目的),他们将不得不再次登录。
[我还保留了一个带有用户对象的HashMap,其中包含来自每个用户的各种信息,例如其个人资料名称,图像,播放列表等。如果计时器的检查证明该用户处于非活动状态,也会从HashMap中删除该信息。] >
问题:
每个Timer对象都会创建一个新线程。从理论上讲,如果有成千上万的用户在使用我的服务,那么就会有成千上万的线程……我的直觉告诉我这是不可接受的。我似乎无法为此缠住头。我应该如何跟踪每位用户50分钟的时间,同时保持尽可能少的线程并且不“过度”使用API?任何提示将不胜感激!代码:
package Authentication; import Spotify.Users.UserSessions; import java.util.Date; import java.util.Set; import java.util.Timer; import java.util.TimerTask; public class RefreshTokens extends TimerTask { private UserSessions userSessions; private Authentication authentication; private String currentUserSession; private Timer timer = new Timer(true); public RefreshTokens(UserSessions userSessions, Authentication authentication, String currentUserSession) { this.userSessions = userSessions; this.authentication = authentication; this.currentUserSession = currentUserSession; } public void startAutomaticProcess() { timer.schedule(this, 20000, 20000); //runs every 20 seconds for testing purposes } @Override public void run() { System.out.println("Automatic process started: " + new Date()); refresh(); } private void refresh() { if (userSessions.contains(currentUserSession)) { if (userSessions.get(currentUserSession).isActive()) { authentication.refreshToken(userSessions.get(currentUserSession)); } else { System.out.println("User was not active enough and has been removed from the server."); System.out.println("----------"); System.out.println("Size of HashMap before: " + userSessions.getHashMap().size()); userSessions.getHashMap().remove(currentUserSession); System.out.println("Size of HashMap after: " + userSessions.getHashMap().size()); timer.cancel(); timer.purge(); } } } }
我为每个新用户创建一个此类的新实例,并调用startAutomaticProcess()方法。
我正在构建与Java Spark中的Spotify API交互的API。我使用了用于令牌管理的授权代码流-这意味着令牌将在一个小时内有效(对于...
将为我的API的每个用户很好地创建线程吗?