新增:主题发布标准方法 publish 替换原 send 方法
This commit is contained in:
@@ -20,6 +20,7 @@ public abstract class AbstractConsumer implements IConsumer {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void addEventType(EventType... eventType) {
|
||||
for (EventType type : eventType) {
|
||||
String[] topics = type.topic.split(",");
|
||||
|
@@ -2,9 +2,11 @@ package com.zdemo;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public interface IProducer<T extends Event> {
|
||||
public interface IProducer {
|
||||
Logger logger = Logger.getLogger(IProducer.class.getSimpleName());
|
||||
|
||||
void send(T t);
|
||||
@Deprecated
|
||||
<T extends Event> void send(T t);
|
||||
|
||||
<V> void publish(String topic, V v);
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -231,6 +231,15 @@ public abstract class ZHubClient extends AbstractConsumer implements IConsumer,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public <V> void publish(String topic, V v) {
|
||||
send("publish", topic, toStr(v));
|
||||
}
|
||||
|
||||
public <V> void broadcast(String topic, V v) {
|
||||
send("broadcast", topic, toStr(v));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribe(String topic, Consumer<String> consumer) {
|
||||
addEventType(EventType.of(topic, consumer));
|
||||
|
Reference in New Issue
Block a user