1515import org .slf4j .LoggerFactory ;
1616import org .springframework .beans .factory .DisposableBean ;
1717import org .springframework .beans .factory .annotation .Value ;
18+ import org .springframework .scheduling .annotation .Scheduled ;
1819import org .springframework .stereotype .Component ;
1920import reactor .core .Exceptions ;
2021import reactor .core .publisher .Mono ;
3233import java .time .Duration ;
3334import java .util .List ;
3435import java .util .UUID ;
35- import java .util .concurrent .*;
36+ import java .util .concurrent .ConcurrentLinkedDeque ;
37+ import java .util .concurrent .TimeoutException ;
3638import java .util .function .BiFunction ;
3739import java .util .function .Function ;
3840
@@ -42,19 +44,13 @@ public class WatchManager implements DisposableBean {
4244 private final WatchConfigManager watchConfigManager ;
4345 private final RedisClient redisClient ;
4446 private final GatewayDiscordClient discordClient ;
45- private final ScheduledExecutorService executorService ;
4647 private final ObjectMapper objectMapper ;
4748 private final boolean watchesEnabled ;
4849 private final ConcurrentLinkedDeque <ConnectionsRecord > joinsQueue = new ConcurrentLinkedDeque <>();
4950 private final ConcurrentLinkedDeque <ConnectionsRecord > leavesQueue = new ConcurrentLinkedDeque <>();
5051 private final ConcurrentLinkedDeque <ChatsRecord > chatsQueue = new ConcurrentLinkedDeque <>();
5152 private final ConcurrentLinkedDeque <DeathsRecord > deathsQueue = new ConcurrentLinkedDeque <>();
5253 private final ConcurrentLinkedDeque <DeathsRecord > killsQueue = new ConcurrentLinkedDeque <>();
53- ScheduledFuture <?> processJoinsFuture ;
54- ScheduledFuture <?> processLeavesFuture ;
55- ScheduledFuture <?> processChatsFuture ;
56- ScheduledFuture <?> processDeathsFuture ;
57- ScheduledFuture <?> processKillsFuture ;
5854 RReliableTopic connectionsTopic ;
5955 RReliableTopic chatsTopic ;
6056 RReliableTopic deathsTopic ;
@@ -66,15 +62,13 @@ public WatchManager(
6662 final WatchConfigManager watchConfigManager ,
6763 final RedisClient redisClient ,
6864 final GatewayDiscordClient discordClient ,
69- final ScheduledExecutorService executorService ,
7065 final ObjectMapper objectMapper ,
7166 @ Value ("${WATCHES}" )
7267 final String watchesEnabled
7368 ) {
7469 this .watchConfigManager = watchConfigManager ;
7570 this .redisClient = redisClient ;
7671 this .discordClient = discordClient ;
77- this .executorService = executorService ;
7872 this .objectMapper = objectMapper ;
7973 this .watchesEnabled = Boolean .parseBoolean (watchesEnabled );
8074 if (this .watchesEnabled ) {
@@ -85,21 +79,17 @@ public WatchManager(
8579 LOGGER .info ("Loaded {} guild watch configs" , watchConfigManager .getAllGuildWatchConfigs ().size ());
8680 connectionsTopic = this .redisClient .getTopic ("ConnectionsTopic" );
8781 connectionsTopicId = connectionsTopic .addListener (String .class , (channel , msg ) -> connectionsTopicListener (msg ));
88- processJoinsFuture = executorService .scheduleAtFixedRate (this ::processJoinsQueue , 0 , 1 , TimeUnit .SECONDS );
89- processLeavesFuture = executorService .scheduleAtFixedRate (this ::processLeavesQueue , 0 , 1 , TimeUnit .SECONDS );
9082 chatsTopic = this .redisClient .getTopic ("ChatsTopic" );
9183 chatsTopicId = chatsTopic .addListener (String .class , (channel , msg ) -> chatsTopicListener (msg ));
92- processChatsFuture = executorService .scheduleAtFixedRate (this ::processChatsQueue , 0 , 1 , TimeUnit .SECONDS );
9384 deathsTopic = this .redisClient .getTopic ("DeathsTopic" );
9485 deathsTopicId = deathsTopic .addListener (String .class , (channel , msg ) -> deathsTopicListener (msg ));
95- processDeathsFuture = executorService .scheduleAtFixedRate (this ::processDeathsQueue , 0 , 1 , TimeUnit .SECONDS );
96- processKillsFuture = executorService .scheduleAtFixedRate (this ::processKillsQueue , 0 , 1 , TimeUnit .SECONDS );
9786 LOGGER .info ("Watch manager initialized" );
9887 } else {
9988 LOGGER .info ("Watch manager disabled" );
10089 }
10190 }
10291
92+ @ Scheduled (fixedRate = 1000 )
10393 private void processJoinsQueue () {
10494 processQueue (
10595 "Joins" ,
@@ -112,6 +102,7 @@ private void processJoinsQueue() {
112102 );
113103 }
114104
105+ @ Scheduled (fixedRate = 1000 )
115106 private void processLeavesQueue () {
116107 processQueue (
117108 "Leaves" ,
@@ -124,6 +115,7 @@ private void processLeavesQueue() {
124115 );
125116 }
126117
118+ @ Scheduled (fixedRate = 1000 )
127119 private void processKillsQueue () {
128120 processQueue (
129121 "Kills" ,
@@ -136,6 +128,7 @@ private void processKillsQueue() {
136128 );
137129 }
138130
131+ @ Scheduled (fixedRate = 1000 )
139132 private void processDeathsQueue () {
140133 processQueue (
141134 "Deaths" ,
@@ -148,6 +141,7 @@ private void processDeathsQueue() {
148141 );
149142 }
150143
144+ @ Scheduled (fixedRate = 1000 )
151145 private void processChatsQueue () {
152146 processQueue (
153147 "Chats" ,
@@ -457,15 +451,6 @@ public void destroy() throws Exception {
457451 } catch (Exception e ) {
458452 LOGGER .error ("Failed to remove Redis topic listener: {}" , deathsTopicId , e );
459453 }
460- try {
461- processJoinsFuture .cancel (true );
462- processLeavesFuture .cancel (true );
463- processChatsFuture .cancel (true );
464- processDeathsFuture .cancel (true );
465- processKillsFuture .cancel (true );
466- } catch (Exception e ) {
467- LOGGER .error ("Failed to cancel scheduled tasks" , e );
468- }
469454 }
470455
471456 public void onAllGuildsLoaded (final List <UserGuildData > guilds ) {
0 commit comments