新增:主题发布标准方法 publish 替换原 send 方法

This commit is contained in:
lxy
2021-01-23 11:03:20 +08:00
parent 361ef58d98
commit 91e4e48aa2
6 changed files with 57 additions and 18 deletions

View File

@@ -13,7 +13,7 @@ import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.logging.Level;
public class RedisProducer<T extends Event> implements IProducer<T>, Service {
public class RedisProducer implements IProducer, Service {
@Resource(name = "property.redis.host")
private String host = "127.0.0.1";
@@ -39,8 +39,9 @@ public class RedisProducer<T extends Event> implements IProducer<T>, Service {
}
}
@Deprecated
@Override
public void send(T t) {
public <T extends Event> void send(T t) {
try {
String v = JsonConvert.root().convertTo(t.value);
if (v.startsWith("\"") && v.endsWith("\"")) {
@@ -53,4 +54,22 @@ public class RedisProducer<T extends Event> implements IProducer<T>, Service {
}
}
@Override
public <V> void publish(String topic, V v) {
try {
osw.write("PUBLISH " + topic + " '" + toStr(v) + "' \r\n");
osw.flush();
} catch (IOException e) {
logger.log(Level.WARNING, "", e);
}
}
private <V> String toStr(V v) {
if (v instanceof String) {
return (String) v;
}
return JsonConvert.root().convertTo(v);
}
}