2020-05-10 06:52:06 +00:00
package net.limework.skLimework.Events ;
import net.limework.skLimework.AddonPlugin ;
import org.bukkit.Bukkit ;
import org.bukkit.ChatColor ;
import org.json.JSONObject ;
import redis.clients.jedis.Jedis ;
import redis.clients.jedis.JedisPubSub ;
2020-05-10 17:44:10 +00:00
import redis.clients.jedis.exceptions.JedisConnectionException ;
2020-05-10 06:52:06 +00:00
import java.util.List ;
2020-05-10 19:10:45 +00:00
public class RedisSub extends JedisPubSub implements Runnable {
2020-05-10 06:52:06 +00:00
private AddonPlugin plugin ;
private Jedis j ;
2020-05-10 19:10:45 +00:00
private String [ ] channels ;
2020-06-05 09:28:26 +00:00
private Boolean isShuttingDown = false ;
2020-05-10 19:10:45 +00:00
2020-05-10 06:52:06 +00:00
public RedisSub ( AddonPlugin plugin , Jedis j , List < String > channels ) {
this . plugin = plugin ;
this . j = j ;
2020-05-10 19:10:45 +00:00
this . channels = channels . toArray ( new String [ 0 ] ) ;
}
2020-05-10 17:44:10 +00:00
2020-05-10 19:10:45 +00:00
@Override
public void run ( ) {
try {
this . j . subscribe ( this , channels ) ;
2020-05-17 21:57:36 +00:00
} catch ( Exception je ) {
2020-05-10 19:10:45 +00:00
plugin . getLogger ( ) . warning ( " Lost connection to redis! " ) ;
newJedis ( ) ;
}
}
2020-05-10 17:44:10 +00:00
2020-05-10 19:10:45 +00:00
private void newJedis ( ) {
2020-05-12 10:35:30 +00:00
//this.unsubscribe();
2020-05-10 19:10:45 +00:00
this . j . close ( ) ;
2020-06-05 09:28:26 +00:00
while ( ! isShuttingDown ) {
2020-05-10 19:10:45 +00:00
try {
plugin . getLogger ( ) . info ( " reconnecting to Redis! " ) ;
this . j = plugin . getJedisPool ( ) . getResource ( ) ;
plugin . getLogger ( ) . info ( " Connected! " ) ;
break ;
2020-05-17 21:57:36 +00:00
} catch ( Exception e ) {
2020-05-10 19:10:45 +00:00
plugin . getLogger ( ) . warning ( " reconnecting to Redis has Failed! retrying in 4 seconds! " ) ;
try { Thread . sleep ( 4000 ) ; } catch ( InterruptedException ignored ) { }
}
}
2020-06-05 09:28:26 +00:00
if ( isShuttingDown ) return ;
2020-05-10 19:10:45 +00:00
plugin . getJedisExecutionService ( ) . execute ( this ) ;
2020-05-10 06:52:06 +00:00
}
2020-05-10 19:10:45 +00:00
2020-05-10 06:52:06 +00:00
@Override
public void onMessage ( String channel , String message ) {
try {
JSONObject json = new JSONObject ( message ) ;
//System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString());
plugin . getServer ( ) . getPluginManager ( ) . callEvent ( new onRedisMessage ( channel , json . getString ( " Message " ) ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
Bukkit . getLogger ( ) . warning ( ChatColor . translateAlternateColorCodes ( '&' , " &2[&aGBot&a] &cI Got a Message that Was empty from channel " + channel + " Please check your code that you used to send the message. ^ ignore the error. " ) ) ;
}
}
2020-05-10 19:10:45 +00:00
public void shutdown ( ) {
2020-06-05 09:28:26 +00:00
isShuttingDown = true ;
2020-05-10 06:52:06 +00:00
this . unsubscribe ( ) ;
j . close ( ) ;
}
2020-05-10 19:10:45 +00:00
}