新增:订阅 subscribe 方法

This commit is contained in:
lxy 2021-01-22 17:53:38 +08:00
parent d83dcd37c2
commit 69be5ec3d7
5 changed files with 41 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package com.zdemo;
import org.redkale.util.TypeToken;
import java.util.Collection;
import java.util.function.Consumer;
public interface IConsumer {
TypeToken<String> TYPE_TOKEN_STRING = new TypeToken<String>() {
@ -22,4 +23,8 @@ public interface IConsumer {
* @param topic
*/
void unsubscribe(String topic);
void subscribe(String topic, Consumer<String> consumer);
<T> void subscribe(String topic, TypeToken<T> typeToken, Consumer<T> consumer);
}

View File

@ -9,6 +9,7 @@ import org.apache.kafka.common.errors.WakeupException;
import org.redkale.net.http.RestService;
import org.redkale.service.Service;
import org.redkale.util.AnyValue;
import org.redkale.util.TypeToken;
import javax.annotation.Resource;
import java.io.File;
@ -17,6 +18,7 @@ import java.io.IOException;
import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -99,4 +101,14 @@ public abstract class KafakConsumer extends AbstractConsumer implements IConsume
public void unsubscribe(String topic) {
queue.add(() -> eventMap.remove(topic)); // 加入延时执行队列下一次订阅变更检查周期执行
}
@Override
public void subscribe(String topic, Consumer<String> consumer) {
addEventType(EventType.of(topic, consumer));
}
@Override
public <T> void subscribe(String topic, TypeToken<T> typeToken, Consumer<T> consumer) {
addEventType(EventType.of(topic, typeToken, consumer));
}
}

View File

@ -5,6 +5,7 @@ import com.zdemo.EventType;
import com.zdemo.IConsumer;
import org.redkale.service.Service;
import org.redkale.util.AnyValue;
import org.redkale.util.TypeToken;
import javax.annotation.Resource;
import java.io.BufferedReader;
@ -13,6 +14,7 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -119,4 +121,14 @@ public class RedisConsumer extends AbstractConsumer implements IConsumer, Servic
logger.log(Level.WARNING, "", e);
}
}
@Override
public void subscribe(String topic, Consumer<String> consumer) {
addEventType(EventType.of(topic, consumer));
}
@Override
public <T> void subscribe(String topic, TypeToken<T> typeToken, Consumer<T> consumer) {
addEventType(EventType.of(topic, typeToken, consumer));
}
}

View File

@ -4,6 +4,7 @@ import com.zdemo.*;
import org.redkale.convert.json.JsonConvert;
import org.redkale.service.Service;
import org.redkale.util.AnyValue;
import org.redkale.util.TypeToken;
import javax.annotation.Resource;
import java.io.BufferedReader;
@ -15,6 +16,7 @@ import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -219,4 +221,14 @@ public abstract class ZHubClient extends AbstractConsumer implements IConsumer,
this.runnable = runnable;
}
}
@Override
public void subscribe(String topic, Consumer<String> consumer) {
addEventType(EventType.of(topic, consumer));
}
@Override
public <T> void subscribe(String topic, TypeToken<T> typeToken, Consumer<T> consumer) {
addEventType(EventType.of(topic, typeToken, consumer));
}
}

View File

@ -9,13 +9,10 @@ import org.redkale.util.Utility;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import static java.util.Arrays.asList;
/**
* 消息发布订阅测试
*/