multithreading - 执行 pub sub 时 Redis 崩溃

标签 multithreading redis bukkit

当我从 jedis 执行函数 getResource 时,它因以下问题而崩溃:

[17:04:58] [Paper Watchdog Thread/ERROR]: Current Thread: Server thread [17:04:58] [Paper Watchdog Thread/ERROR]: PID: 18 | Suspended: false | Native: false | State: WAITING [17:04:58] [Paper Watchdog Thread/ERROR]: Thread is waiting on monitor(s): [17:04:58] [Paper Watchdog Thread/ERROR]: Locked on:org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:499) [17:04:58] [Paper Watchdog Thread/ERROR]: Stack: [17:04:58] [Paper Watchdog Thread/ERROR]: sun.misc.Unsafe.park(Native Method) [17:04:58] [Paper Watchdog Thread/ERROR]: java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) [17:04:58] [Paper Watchdog Thread/ERROR]: java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) [17:04:58] [Paper Watchdog Thread/ERROR]: org.apache.commons.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:583) [17:04:58] [Paper Watchdog Thread/ERROR]: org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442) [17:04:58] [Paper Watchdog Thread/ERROR]: org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) [17:04:58] [Paper Watchdog Thread/ERROR]: redis.clients.util.Pool.getResource(Pool.java:49) [17:04:58] [Paper Watchdog Thread/ERROR]: redis.clients.jedis.JedisPool.getResource(JedisPool.java:226) [17:04:58] [Paper Watchdog Thread/ERROR]: pl.imoobler.sectors.managers.RedisManager.updatePosition(RedisManager.java:44) [17:04:58] [Paper Watchdog Thread/ERROR]: pl.imoobler.sectors.utils.BungeeUtils.teleportServer(BungeeUtils.java:28) [17:04:58] [Paper Watchdog Thread/ERROR]: pl.imoobler.sectors.listeners.PlayerMoveListener.detectSectorSwap(PlayerMoveListener.java:88) [17:04:58] [Paper Watchdog Thread/ERROR]: com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor6.execute(Unknown Source) [17:04:58] [Paper Watchdog Thread/ERROR]: org.bukkit.plugin.EventExecutor$1.execute(EventExecutor.java:44) [17:04:58] [Paper Watchdog Thread/ERROR]: co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) [17:04:58] [Paper Watchdog Thread/ERROR]: org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) [17:04:58] [Paper Watchdog Thread/ERROR]: org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:514) [17:04:58] [Paper Watchdog Thread/ERROR]: org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:499) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:649) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.PacketPlayInFlying.a(SourceFile:126) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.PlayerConnectionUtils$$Lambda$199/2109184961.run(Unknown Source) [17:04:58] [Paper Watchdog Thread/ERROR]: java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [17:04:58] [Paper Watchdog Thread/ERROR]: java.util.concurrent.FutureTask.run(FutureTask.java:266) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:842) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:766) [17:04:58] [Paper Watchdog Thread/ERROR]: net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:664) [17:04:58] [Paper Watchdog Thread/ERROR]: java.lang.Thread.run(Thread.java:748) [17:04:58] [Paper Watchdog Thread/ERROR]: ------------------------------

我的代码:

package pl.imoobler.sectors.managers;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.bukkit.Bukkit;
import pl.imoobler.sectors.SectorsPlugin;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.util.UUID;

public class RedisManager {

SectorsPlugin plugin;
JedisPool pool;
Gson gson = new Gson();

public RedisManager(SectorsPlugin plugin, String redisHost) {
    this.plugin = plugin;
    pool = new JedisPool(redisHost);
}

public void subscribe(final JedisPubSub pubSub, final String... channels) {
    Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
        public void run() {
            Jedis jedis = pool.getResource();
            try {
                jedis.select(0);
                jedis.subscribe(pubSub, channels);
            } catch (JedisConnectionException ex) {
                pool.returnBrokenResource(jedis);
            } finally {
                pool.returnResource(jedis);
            }
        }
    });
}

public void updatePosition(UUID uuid, String position, String target) {
    Jedis subscriber = null;
    try {
        subscriber = pool.getResource();
        subscriber.set(("sectors:" + "positions" + ":" + uuid.toString()), position);
        JsonObject object = new JsonObject();
        object.addProperty("position_player_uuid", uuid.toString());
        object.addProperty("position_player_target", target);
        subscriber.publish("NewPlayerLocationChannel", gson.toJson(object));
    }catch(JedisConnectionException ex) {
        pool.returnBrokenResource(subscriber);
    } finally {
        pool.returnResource(subscriber);
    }
}

public String getPosition(UUID uuid) {
    Jedis subscriber = pool.getResource();
    if(subscriber.exists(("sectors:" + "positions" + ":" + uuid.toString()))) {
        return subscriber.get(("sectors:" + "positions" + ":" + uuid.toString()));
    }
    return null;
}


}

最佳答案

您达到 JedisPool 大小了吗?默认的 JedisPool 大小非常小。

关于multithreading - 执行 pub sub 时 Redis 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45331002/

相关文章:

java - 转换泛型类型的 map 列表

java - 目录层次结构必须与包层次结构相对应

java - Thread.sleep 不 hibernate 当前线程

multithreading - 在Vec上实现并行/多线程合并排序

Laravel Socket.io,Redis 事件正在广播但未在客户端显示

redis - 如何增加redis中值的大小?

hash - Redis HMSET 文档 : What does it mean by 'hash' ?

c - 在同时运行的线程中调用 printf 是否线程安全?

android - 在线程中的小部件中显示 Toast

Java Bukkit 插件 "Cannot be resolved to a variable"