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

This commit is contained in:
lxy
2021-01-23 11:03:20 +08:00
parent 48db458f5e
commit 3e91d785e3
6 changed files with 57 additions and 18 deletions

View File

@@ -19,10 +19,10 @@ import java.util.logging.Level;
/**
* 生产
*
* @param <T>
* @param
*/
@RestService
public class KafakProducer<T extends Event> implements IProducer<T>, Service {
public class KafakProducer implements IProducer, Service {
private KafkaProducer<String, String> producer;
@Resource(name = "APP_HOME")
@@ -40,8 +40,9 @@ public class KafakProducer<T extends Event> implements IProducer<T>, Service {
}
}
@Deprecated
@Override
public void send(T t) {
public <T extends Event> void send(T t) {
String v = JsonConvert.root().convertTo(t.value);
if (v.startsWith("\"") && v.endsWith("\"")) {
v = v.substring(1, v.length() - 1);
@@ -49,8 +50,20 @@ public class KafakProducer<T extends Event> implements IProducer<T>, Service {
producer.send(new ProducerRecord(t.topic, v));
}
@Override
public <V> void publish(String topic, V v) {
producer.send(new ProducerRecord(topic, toStr(v)));
}
@Override
public void destroy(AnyValue config) {
producer.close();
}
private <V> String toStr(V v) {
if (v instanceof String) {
return (String) v;
}
return JsonConvert.root().convertTo(v);
}
}