Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. package zone.potion.redis;
  2.  
  3. import com.google.common.collect.Maps;
  4. import com.google.common.collect.Sets;
  5. import lombok.Getter;
  6. import org.apache.commons.lang3.tuple.ImmutablePair;
  7. import org.apache.commons.lang3.tuple.Pair;
  8. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
  9. import redis.clients.jedis.Jedis;
  10. import redis.clients.jedis.JedisPool;
  11. import redis.clients.jedis.JedisPubSub;
  12. import zone.potion.CorePlugin;
  13. import zone.potion.redis.annotation.RedisHandler;
  14. import zone.potion.redis.message.RedisMessage;
  15.  
  16. import java.lang.annotation.Annotation;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.lang.reflect.Method;
  19. import java.util.HashSet;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.stream.Collectors;
  23. import java.util.stream.Stream;
  24.  
  25. @Getter
  26. public class RedisMessenger {
  27.  
  28. private final JedisPool jedisPool;
  29. private final CorePlugin plugin;
  30. private Set<Object> listeners = new HashSet<>();
  31.  
  32.  
  33. public RedisMessenger(CorePlugin plugin, String host, int port, int timeout, String password) {
  34. this.plugin = plugin;
  35. GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  36. config.setMaxTotal(20);
  37. this.jedisPool = new JedisPool(config, host, port, timeout, password);
  38.  
  39. }
  40.  
  41. public void initialize() {
  42.  
  43. //In order to avoid subscribing twice to the same redis channel, we add a simple set.
  44. Set<String> subscribedChannels = Sets.newHashSet();
  45. Map<String, Pair<Object, Method>> map = Maps.newHashMap();
  46.  
  47. //We schedule an asynchronous task to handle our subscriptions.
  48. listeners.forEach(listener -> {
  49. //After looping through each listener, we get that listener's methods, and try to find where the RedisHandler annotation is used, we add that to a set.
  50. Set<Method> methods = getMethodsOfAnnotation(listener.getClass(), RedisHandler.class);
  51.  
  52. for (Method method : methods) {
  53. //For each of these sets, we get the redis handler, check if we're already subscribed, if not, we subscribe to the channel.
  54. RedisHandler handler = method.getAnnotation(RedisHandler.class);
  55. if (!subscribedChannels.contains(handler.value())) {
  56. map.put(handler.value(), new ImmutablePair<>(listener, method));
  57. subscribedChannels.add(handler.value());
  58. }
  59. }
  60. });
  61.  
  62. plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
  63. try (Jedis jedis = jedisPool.getResource()) {
  64. jedis.subscribe(new JedisPubSub() {
  65. @Override
  66. public void onMessage(String channel, String message) {
  67. map.forEach((c, pair) -> {
  68. if (channel.equalsIgnoreCase(c)) {
  69. try {
  70. pair.getValue().invoke(pair.getKey(), RedisMessage.deserialize(message));
  71. } catch (IllegalAccessException | InvocationTargetException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. });
  76. }
  77. }, subscribedChannels.toArray(new String[0]));
  78. }
  79. });
  80. }
  81.  
  82. public void send(String channel, Map<String, Object> message) {
  83. plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
  84. try (Jedis jedis = jedisPool.getResource()) {
  85. jedis.publish(channel, RedisMessage.serialize(message));
  86. }
  87. });
  88. }
  89.  
  90. public void registerListeners(Object... objects) {
  91. for (Object object : objects) {
  92. getListeners().add(object);
  93. }
  94. }
  95.  
  96. private Set<Method> getMethodsOfAnnotation(Class<?> clazz, Class<? extends Annotation> annotation) {
  97. return Stream.of(clazz.getDeclaredMethods())
  98. .filter(method -> method.isAnnotationPresent(annotation))
  99. .collect(Collectors.toSet());
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement