当我尝试发送消息时,突然出现此错误。我不知道为什么这已经工作了一段时间了,但是当我尝试发送消息时,它只是停止了工作。有时,即使抛出错误,它实际上也会发送消息,但是在侦听器中,该消息为null。我不知道为什么会这样。
redis.clients.jedis.exceptions.JedisDataException: ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context
at redis.clients.jedis.Protocol.processError(Protocol.java:127) ~[?:?]
at redis.clients.jedis.Protocol.process(Protocol.java:161) ~[?:?]
at redis.clients.jedis.Protocol.read(Protocol.java:215) ~[?:?]
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) ~[?:?]
at redis.clients.jedis.Connection.getIntegerReply(Connection.java:265) ~[?:?]
at redis.clients.jedis.Jedis.publish(Jedis.java:2690) ~[?:?]
at com.cursehq.core.communicator.CoreCommunicator.sendMessage(CoreCommunicator.java:117) ~[?:?]
这里是沟通者课 包com.cursehq.core.communicator;
import com.cursehq.core.Core;
import com.cursehq.core.api.communicator.Communicator;
import com.cursehq.core.api.communicator.listener.CommunicatorListener;
import com.cursehq.core.communicator.listeners.*;
import com.cursehq.core.communicator.message.Message;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;
import java.util.HashSet;
import java.util.Set;
public class CoreCommunicator implements Communicator {
private final Set<CommunicatorListener> listeners = new HashSet<>();
private final Gson gson = new Gson();
private JedisPool pool;
private String server;
public boolean load() {
String host = Core.getCore().getConfig().getString("redis.host");
int port = Core.getCore().getConfig().getInt("redis.port");
String password = Core.getCore().getConfig().getString("redis.password");
server = Core.getCore().getConfig().getString("redis.server");
try {
pool = new JedisPool(new GenericObjectPoolConfig(), host, port, 20000, password);
loadServer();
loadListener();
loadListeners();
Core.log("&7Successfully connected to the redis communicator!");
} catch (Exception e) {
return false;
}
return true;
}
public void unload() {
try (Jedis jedis = pool.getResource()) {
String json = jedis.get("servers");
Set<String> set = gson.fromJson(json, new TypeToken<Set<String>>(){}.getType());
set.remove(server.toLowerCase());
jedis.set("servers", gson.toJson(set));
jedis.persist("servers");
}
}
private void loadServer() {
try (Jedis jedis = pool.getResource()) {
Set<String> set;
String json = jedis.get("servers");
if (json == null)
set = new HashSet<>();
else
set = gson.fromJson(json, new TypeToken<Set<String>>(){}.getType());
set.add(server.toLowerCase());
jedis.set("servers", gson.toJson(set));
jedis.persist("servers");
}
}
private void loadListener() {
new Thread(() -> {
try (Jedis jedis = pool.getResource()) {
jedis.subscribe(new JedisPubSub() {
public void onMessage(String channel, String msg1) {
Message message = gson.fromJson(msg1, Message.class);
String type = message.getType();
String msg = message.getMessage();
String from = Core.getCore().getUtils().capitalize(message.getFrom());
listeners.stream()
.filter(listener -> listener.getType().equalsIgnoreCase(type))
.forEach(listener -> listener.onMessageReceive(msg, from));
}
}, "all", server.toLowerCase());
}
}).start();
}
private void loadListeners() {
new AnnouncementListener();
new CommandListener();
new RankUpdateListener();
new ServerStatusListener();
new StaffChatListener();
}
public JedisPool getPool() {
return pool;
}
public String getServerName() {
return server;
}
public String getServers() {
try (Jedis jedis = pool.getResource()) {
String json = jedis.get("servers");
Set<String> servers = gson.fromJson(json, new TypeToken<Set<String>>(){}.getType());
return String.join(", ", servers);
}
}
public boolean serverExists(String string) {
try (Jedis jedis = pool.getResource()) {
Set<String> servers = gson.fromJson(jedis.get("servers"), new TypeToken<Set<String>>(){}.getType());
return servers.contains(string.toLowerCase());
}
}
public void sendMessage(String type, String msg) {
sendMessage(type, msg, "all");
}
public void sendMessage(String type, String msg, String to) {
try (Jedis jedis = pool.getResource()) {
Message message = new Message(type, msg);
String json = gson.toJson(message);
jedis.publish(to.toLowerCase(), json);
}
}
public Set<CommunicatorListener> getListeners() {
return listeners;
}
}
如果有人知道可能导致此错误的原因,请分享。