新增:主题发布标准方法 publish 替换原 send 方法
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user