Compare commits
5 Commits
dev/spring
...
dev/spring
Author | SHA1 | Date | |
---|---|---|---|
c2bcc00741 | |||
d28a8e4d7b | |||
e2c2b62665 | |||
e8b9140103 | |||
1786724c88 |
39
pom.xml
39
pom.xml
@@ -4,54 +4,46 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>dev.zhub</groupId>
|
<groupId>net.tccn</groupId>
|
||||||
<artifactId>zhub-client-spring</artifactId>
|
<artifactId>zhub-client-spring</artifactId>
|
||||||
<version>17.0.0423.dev</version>
|
<version>0.1.2</version>
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
|
||||||
<version>3.1.10</version>
|
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
<scope>provided</scope>
|
<version>3.2.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.10.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<!--<version>4.13.1</version>-->
|
<version>4.13.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
<!--<version>1.18.30</version>-->
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
<id>maven-release</id>
|
<id>maven-nexus</id>
|
||||||
<name>maven-nexus</name>
|
<name>maven-nexus</name>
|
||||||
<url>https://nexus.1216.top/repository/maven-public/</url>
|
<url>https://nexus.1216.top/repository/maven-public/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
<repository>
|
<repository>
|
||||||
<id>mvn-release</id>
|
<id>mvn-release</id>
|
||||||
@@ -59,12 +51,5 @@
|
|||||||
<url>https://nexus.1216.top/repository/maven-releases/</url>
|
<url>https://nexus.1216.top/repository/maven-releases/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
<!--<distributionManagement>
|
|
||||||
<repository>
|
|
||||||
<id>mvn-recloud</id>
|
|
||||||
<name>mvn-release</name>
|
|
||||||
<url>http://nexus.recloud.cn/repository/maven-releases/</url>
|
|
||||||
</repository>
|
|
||||||
</distributionManagement>-->
|
|
||||||
|
|
||||||
</project>
|
</project>
|
@@ -1,27 +0,0 @@
|
|||||||
package dev.zhub;
|
|
||||||
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public interface IType {
|
|
||||||
|
|
||||||
TypeToken<String> STRING = new TypeToken<>() {
|
|
||||||
};
|
|
||||||
|
|
||||||
TypeToken<Short> SHORT = new TypeToken<>() {
|
|
||||||
};
|
|
||||||
TypeToken<Integer> INT = new TypeToken<>() {
|
|
||||||
};
|
|
||||||
TypeToken<Long> LONG = new TypeToken<>() {
|
|
||||||
};
|
|
||||||
TypeToken<Double> DOUBLE = new TypeToken<>() {
|
|
||||||
};
|
|
||||||
|
|
||||||
TypeToken<Map<String, String>> MAP = new TypeToken<>() {
|
|
||||||
};
|
|
||||||
|
|
||||||
TypeToken<List<Map<String, String>>> LMAP = new TypeToken<List<Map<String, String>>>() {
|
|
||||||
};
|
|
||||||
}
|
|
@@ -1,13 +1,13 @@
|
|||||||
package dev.zhub;
|
package tccn;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import dev.zhub.client.Rpc;
|
import tccn.zhub.Rpc;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,7 +18,7 @@ public abstract class AbstractConsumer implements IConsumer {
|
|||||||
|
|
||||||
public Gson gson = Rpc.gson;
|
public Gson gson = Rpc.gson;
|
||||||
|
|
||||||
protected Map<String, EventType<?>> eventMap = new ConcurrentHashMap<>();
|
private Map<String, EventType> eventMap = new HashMap<>();
|
||||||
|
|
||||||
protected abstract String getGroupid();
|
protected abstract String getGroupid();
|
||||||
|
|
||||||
@@ -36,7 +36,6 @@ public abstract class AbstractConsumer implements IConsumer {
|
|||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
|
|
||||||
// topic 消息消费前处理
|
|
||||||
protected void accept(String topic, String value) {
|
protected void accept(String topic, String value) {
|
||||||
EventType eventType = eventMap.get(topic);
|
EventType eventType = eventMap.get(topic);
|
||||||
|
|
||||||
@@ -50,19 +49,6 @@ public abstract class AbstractConsumer implements IConsumer {
|
|||||||
eventType.accept(data);
|
eventType.accept(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// rpc 被调用端
|
|
||||||
protected <T> void rpcAccept(String topic, T value) {
|
|
||||||
EventType eventType = eventMap.get(topic);
|
|
||||||
|
|
||||||
/*// eventType 与 T 的类型比较、
|
|
||||||
if (!eventType.typeToken.getType().getTypeName().equals(value.getClass().getTypeName())) {
|
|
||||||
eventType.accept(toStr(value));
|
|
||||||
} else {
|
|
||||||
eventType.accept(value);
|
|
||||||
}*/
|
|
||||||
eventType.accept(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final void removeEventType(String topic) {
|
protected final void removeEventType(String topic) {
|
||||||
eventMap.remove(topic);
|
eventMap.remove(topic);
|
||||||
}
|
}
|
||||||
@@ -90,13 +76,4 @@ public abstract class AbstractConsumer implements IConsumer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String toStr(Object v) {
|
|
||||||
if (v instanceof String) {
|
|
||||||
return (String) v;
|
|
||||||
} else if (v == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return gson.toJson(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub;
|
package tccn;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发布订阅 事件
|
* 发布订阅 事件
|
||||||
@@ -19,4 +19,5 @@ public class Event<V> {
|
|||||||
return new Event<>(topic, value);
|
return new Event<>(topic, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub;
|
package tccn;
|
||||||
|
|
||||||
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub;
|
package tccn;
|
||||||
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub;
|
package tccn;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
21
src/main/java/tccn/IType.java
Normal file
21
src/main/java/tccn/IType.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package tccn;
|
||||||
|
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface IType {
|
||||||
|
|
||||||
|
TypeToken<String> STRING = new TypeToken<String>() {
|
||||||
|
};
|
||||||
|
|
||||||
|
TypeToken<Integer> INT = new TypeToken<Integer>() {
|
||||||
|
};
|
||||||
|
|
||||||
|
TypeToken<Map<String, String>> MAP = new TypeToken<Map<String, String>>() {
|
||||||
|
};
|
||||||
|
|
||||||
|
TypeToken<List<Map<String, String>>> LMAP = new TypeToken<List<Map<String, String>>>() {
|
||||||
|
};
|
||||||
|
}
|
@@ -1,7 +1,7 @@
|
|||||||
package dev.zhub.timer;
|
package tccn.timer;
|
||||||
|
|
||||||
import dev.zhub.timer.queue.TimerQueue;
|
import tccn.timer.queue.TimerQueue;
|
||||||
import dev.zhub.timer.task.Task;
|
import tccn.timer.task.Task;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@@ -24,7 +24,7 @@ public class TimerExecutor {
|
|||||||
for (Task t : task) {
|
for (Task t : task) {
|
||||||
t.setTimerExecutor(this);
|
t.setTimerExecutor(this);
|
||||||
queue.push(t);
|
queue.push(t);
|
||||||
// logger.finest("add new task : " + t.getName());
|
logger.finest("add new task : " + t.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@@ -1,8 +1,8 @@
|
|||||||
package dev.zhub.timer;
|
package tccn.timer;
|
||||||
|
|
||||||
import dev.zhub.timer.scheduled.Scheduled;
|
import tccn.timer.scheduled.Scheduled;
|
||||||
import dev.zhub.timer.task.Job;
|
import tccn.timer.task.Job;
|
||||||
import dev.zhub.timer.task.Task;
|
import tccn.timer.task.Task;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
@@ -93,10 +93,10 @@ public class TimerTask implements Task {
|
|||||||
if (!isComplete) {
|
if (!isComplete) {
|
||||||
int count = execCount.incrementAndGet(); // 执行次数+1
|
int count = execCount.incrementAndGet(); // 执行次数+1
|
||||||
|
|
||||||
// long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
job.execute(this);
|
job.execute(this);
|
||||||
// long end = System.currentTimeMillis();
|
long end = System.currentTimeMillis();
|
||||||
// logger.finest(String.format("task [%s] : not complete -> %s, time: %s ms, exec count: %s.", getName(), isComplete ? "had complete" : "not complete", end - start, count));
|
logger.finest(String.format("task [%s] : not complete -> %s, time: %s ms, exec count: %s.", getName(), isComplete ? "had complete" : "not complete", end - start, count));
|
||||||
|
|
||||||
if (!isComplete) {
|
if (!isComplete) {
|
||||||
timerExecutor.add(this, true);
|
timerExecutor.add(this, true);
|
@@ -1,6 +1,6 @@
|
|||||||
package dev.zhub.timer;
|
package tccn.timer;
|
||||||
|
|
||||||
import dev.zhub.timer.scheduled.ScheduledCycle;
|
import tccn.timer.scheduled.ScheduledCycle;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
@@ -1,6 +1,6 @@
|
|||||||
package dev.zhub.timer.queue;
|
package tccn.timer.queue;
|
||||||
|
|
||||||
import dev.zhub.timer.task.Task;
|
import tccn.timer.task.Task;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub.timer.scheduled;
|
package tccn.timer.scheduled;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub.timer.scheduled;
|
package tccn.timer.scheduled;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub.timer.scheduled;
|
package tccn.timer.scheduled;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub.timer.task;
|
package tccn.timer.task;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author: liangxianyou at 2018/12/8 17:24.
|
* @author: liangxianyou at 2018/12/8 17:24.
|
@@ -1,7 +1,7 @@
|
|||||||
package dev.zhub.timer.task;
|
package tccn.timer.task;
|
||||||
|
|
||||||
import dev.zhub.timer.TimerExecutor;
|
import tccn.timer.TimerExecutor;
|
||||||
import dev.zhub.timer.scheduled.Scheduled;
|
import tccn.timer.scheduled.Scheduled;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author: liangxianyou at 2018/8/5 19:32.
|
* @author: liangxianyou at 2018/8/5 19:32.
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub.client;
|
package tccn.zhub;
|
||||||
|
|
||||||
// ================================================== lock ==================================================
|
// ================================================== lock ==================================================
|
||||||
public class Lock {
|
public class Lock {
|
@@ -1,15 +1,8 @@
|
|||||||
package dev.zhub.client;
|
package tccn.zhub;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.annotations.Expose;
|
import com.google.gson.annotations.Expose;
|
||||||
import com.google.gson.reflect.TypeToken;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public class Rpc<T> {
|
public class Rpc<T> {
|
||||||
/*public final static Gson gson = new GsonBuilder()
|
/*public final static Gson gson = new GsonBuilder()
|
||||||
.excludeFieldsWithoutExposeAnnotation()
|
.excludeFieldsWithoutExposeAnnotation()
|
||||||
@@ -23,18 +16,48 @@ public class Rpc<T> {
|
|||||||
@Expose(deserialize = false, serialize = false)
|
@Expose(deserialize = false, serialize = false)
|
||||||
private RpcResult rpcResult;
|
private RpcResult rpcResult;
|
||||||
|
|
||||||
@Expose(deserialize = false, serialize = false)
|
public Rpc() {
|
||||||
private TypeToken typeToken;
|
}
|
||||||
|
|
||||||
/*public Rpc() {
|
public Rpc(String appname, String ruk, String topic, Object value) {
|
||||||
}*/
|
this.ruk = appname + "::" + ruk;
|
||||||
|
|
||||||
protected Rpc(String appname, String topic, T value) {
|
|
||||||
this.ruk = appname + "::" + UUID.randomUUID().toString().replaceAll("-", "");
|
|
||||||
this.topic = topic;
|
this.topic = topic;
|
||||||
|
this.value = (T) gson.toJson(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRuk() {
|
||||||
|
return ruk;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRuk(String ruk) {
|
||||||
|
this.ruk = ruk;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTopic() {
|
||||||
|
return topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTopic(String topic) {
|
||||||
|
this.topic = topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(T value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public RpcResult getRpcResult() {
|
||||||
|
return rpcResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRpcResult(RpcResult rpcResult) {
|
||||||
|
this.rpcResult = rpcResult;
|
||||||
|
}
|
||||||
|
|
||||||
public String getBackTopic() {
|
public String getBackTopic() {
|
||||||
return ruk.split("::")[0];
|
return ruk.split("::")[0];
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package dev.zhub.client;
|
package tccn.zhub;
|
||||||
|
|
||||||
public class RpcResult<R> {
|
public class RpcResult<R> {
|
||||||
private String ruk;
|
private String ruk;
|
@@ -1,13 +1,15 @@
|
|||||||
package dev.zhub.client;
|
package tccn.zhub;
|
||||||
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import dev.zhub.*;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import dev.zhub.timer.Timers;
|
import tccn.AbstractConsumer;
|
||||||
|
import tccn.Event;
|
||||||
|
import tccn.IConsumer;
|
||||||
|
import tccn.IProducer;
|
||||||
|
import tccn.timer.Timers;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@@ -15,27 +17,31 @@ import java.io.OutputStream;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.*;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer {
|
public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer {
|
||||||
|
|
||||||
public Logger logger = Logger.getLogger(ZHubClient.class.getSimpleName());
|
public Logger logger = Logger.getLogger(ZHubClient.class.getSimpleName());
|
||||||
@Setter
|
|
||||||
@Value("${zhub.addr}")
|
@Value("${zhub.addr}")
|
||||||
private String addr = "127.0.0.1:1216";
|
private String addr = "127.0.0.1:1216";
|
||||||
@Setter
|
|
||||||
@Value("${zhub.groupid}")
|
@Value("${zhub.groupid}")
|
||||||
private String groupid = "";
|
private String groupid = "";
|
||||||
@Setter
|
|
||||||
@Value("${zhub.auth}")
|
@Value("${zhub.auth}")
|
||||||
private String auth = "";
|
private String auth = "";
|
||||||
@Setter
|
|
||||||
@Value("${zhub.appid}")
|
@Value("${zhub.appid}")
|
||||||
protected String appid = "";
|
protected String appid = "";
|
||||||
|
|
||||||
@@ -44,21 +50,18 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
init(null);
|
init(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Socket client;
|
|
||||||
private OutputStream writer;
|
private OutputStream writer;
|
||||||
private BufferedReader reader;
|
private BufferedReader reader;
|
||||||
|
|
||||||
private final LinkedBlockingQueue<Timer> timerQueue = new LinkedBlockingQueue<>();
|
private final LinkedBlockingQueue<Timer> timerQueue = new LinkedBlockingQueue<>();
|
||||||
private final LinkedBlockingQueue<Event<String>> topicQueue = new LinkedBlockingQueue<>();
|
private final LinkedBlockingQueue<Event<String>> topicQueue = new LinkedBlockingQueue<>();
|
||||||
private final LinkedBlockingQueue<Event<Object>> rpcBackQueue = new LinkedBlockingQueue<>(); // RPC BACK MSG [=> Object]
|
private final LinkedBlockingQueue<Event<String>> rpcBackQueue = new LinkedBlockingQueue<>(); // RPC BACK MSG
|
||||||
private final LinkedBlockingQueue<Event<Object>> rpcCallQueue = new LinkedBlockingQueue<>(); // RPC CALL MSG [=> Object]
|
private final LinkedBlockingQueue<Event<String>> rpcCallQueue = new LinkedBlockingQueue<>(); // RPC CALL MSG
|
||||||
private final LinkedBlockingQueue<String> sendMsgQueue = new LinkedBlockingQueue<>(); // SEND MSG
|
private final LinkedBlockingQueue<String> sendMsgQueue = new LinkedBlockingQueue<>(); // SEND MSG
|
||||||
|
|
||||||
private static Map<String, ZHubClient> mainHub = new HashMap<>(); // 127.0.0.1:1216 - ZHubClient
|
/*private static boolean isFirst = true;
|
||||||
|
private boolean isMain = false;*/
|
||||||
public ZHubClient() {
|
private static final Map<String, ZHubClient> mainHub = new HashMap<>(); // 127.0.0.1:1216 - ZHubClient
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public ZHubClient(String addr, String groupid, String appid, String auth) {
|
public ZHubClient(String addr, String groupid, String appid, String auth) {
|
||||||
this.addr = addr;
|
this.addr = addr;
|
||||||
@@ -81,226 +84,241 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 设置第一个启动的 实例为主实例
|
// 设置第一个启动的 实例为主实例
|
||||||
|
/*if (isFirst) {
|
||||||
|
isMain = true;
|
||||||
|
isFirst = false;
|
||||||
|
}*/
|
||||||
if (!mainHub.containsKey(addr)) { // 确保同步执行此 init 逻辑
|
if (!mainHub.containsKey(addr)) { // 确保同步执行此 init 逻辑
|
||||||
mainHub.put(addr, this);
|
mainHub.put(addr, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletableFuture.runAsync(() -> {
|
if (!initSocket(0)) {
|
||||||
if (!initSocket(0)) {
|
return;
|
||||||
return;
|
}
|
||||||
|
// 消息 事件接收
|
||||||
|
new Thread(() -> {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
String readLine = reader.readLine();
|
||||||
|
if (readLine == null && initSocket(Integer.MAX_VALUE)) { // 连接中断 处理
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String type;
|
||||||
|
|
||||||
|
// +ping
|
||||||
|
if ("+ping".equals(readLine)) {
|
||||||
|
send("+pong");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 主题订阅消息
|
||||||
|
if ("*3".equals(readLine)) {
|
||||||
|
reader.readLine(); // $7 len()
|
||||||
|
|
||||||
|
type = reader.readLine(); // message
|
||||||
|
if (!"message".equals(type)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
reader.readLine(); //$n len(key)
|
||||||
|
String topic = reader.readLine(); // topic
|
||||||
|
|
||||||
|
String lenStr = reader.readLine();//$n len(value)
|
||||||
|
int clen = 0;
|
||||||
|
if (lenStr.startsWith("$")) {
|
||||||
|
clen = Integer.parseInt(lenStr.replace("$", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
String value = "";
|
||||||
|
do {
|
||||||
|
if (value.length() > 0) {
|
||||||
|
value += "\r\n";
|
||||||
|
}
|
||||||
|
String s = reader.readLine();
|
||||||
|
value += s; // value
|
||||||
|
} while (clen > 0 && clen > strLength(value));
|
||||||
|
|
||||||
|
|
||||||
|
// lock msg
|
||||||
|
if ("lock".equals(topic)) {
|
||||||
|
Lock lock = lockTag.get(value);
|
||||||
|
if (lock != null) {
|
||||||
|
synchronized (lock) {
|
||||||
|
lock.success = true;
|
||||||
|
lock.notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// trylock msg
|
||||||
|
if ("trylock".equals(topic)) {
|
||||||
|
Lock lock = lockTag.get(value);
|
||||||
|
if (lock != null) {
|
||||||
|
synchronized (lock) {
|
||||||
|
lock.success = false;
|
||||||
|
lock.notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// rpc back msg
|
||||||
|
if (appid.equals(topic)) {
|
||||||
|
rpcBackQueue.add(Event.of(topic, value));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// rpc call msg
|
||||||
|
if (rpcTopics.contains(topic)) {
|
||||||
|
rpcCallQueue.add(Event.of(topic, value));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// oth msg
|
||||||
|
topicQueue.add(Event.of(topic, value));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// timer 消息
|
||||||
|
if ("*2".equals(readLine)) {
|
||||||
|
reader.readLine(); // $7 len()
|
||||||
|
|
||||||
|
type = reader.readLine(); // message
|
||||||
|
if (!"timer".equals(type)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
reader.readLine(); //$n len(key)
|
||||||
|
String topic = reader.readLine(); // name
|
||||||
|
|
||||||
|
timerQueue.add(timerMap.get(topic));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.finest(readLine);
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (e instanceof SocketException) {
|
||||||
|
initSocket(Integer.MAX_VALUE);
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 消息 事件接收
|
}).start();
|
||||||
new Thread(() -> {
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
String readLine = reader.readLine();
|
|
||||||
if (readLine == null && initSocket(Integer.MAX_VALUE)) { // 连接中断 处理
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String type = "";
|
// 定时调度事件
|
||||||
|
new Thread(() -> {
|
||||||
// +ping
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
if ("+ping".equals(readLine)) {
|
while (true) {
|
||||||
send("+pong");
|
Timer timer = null;
|
||||||
continue;
|
try {
|
||||||
}
|
timer = timerQueue.take();
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
// 主题订阅消息
|
executor.submit(timer.runnable).get(5, TimeUnit.SECONDS);
|
||||||
if ("*3".equals(readLine)) {
|
long end = System.currentTimeMillis();
|
||||||
readLine = reader.readLine(); // $7 len()
|
logger.finest(String.format("timer [%s] : elapsed time %s ms", timer.name, end - start));
|
||||||
type = reader.readLine(); // message
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||||
if (!"message".equals(type)) {
|
if (e instanceof TimeoutException) {
|
||||||
continue;
|
executor = Executors.newSingleThreadExecutor();
|
||||||
}
|
logger.log(Level.WARNING, "TimeoutException [" + timer.name + "]", e);
|
||||||
reader.readLine(); //$n len(key)
|
} else {
|
||||||
String topic = reader.readLine(); // topic
|
|
||||||
|
|
||||||
String lenStr = reader.readLine();//$n len(value)
|
|
||||||
int clen = 0;
|
|
||||||
if (lenStr.startsWith("$")) {
|
|
||||||
clen = Integer.parseInt(lenStr.replace("$", ""));
|
|
||||||
}
|
|
||||||
|
|
||||||
String value = "";
|
|
||||||
do {
|
|
||||||
if (!value.isEmpty()) {
|
|
||||||
value += "\r\n";
|
|
||||||
}
|
|
||||||
String s = reader.readLine();
|
|
||||||
value += s; // value
|
|
||||||
} while (clen > 0 && clen > strLength(value));
|
|
||||||
|
|
||||||
logger.finest("topic[" + topic + "]: " + value);
|
|
||||||
|
|
||||||
// lock msg
|
|
||||||
if ("lock".equals(topic)) {
|
|
||||||
Lock lock = lockTag.get(value);
|
|
||||||
if (lock != null) {
|
|
||||||
synchronized (lock) {
|
|
||||||
lock.success = true;
|
|
||||||
lock.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// trylock msg
|
|
||||||
if ("trylock".equals(topic)) {
|
|
||||||
Lock lock = lockTag.get(value);
|
|
||||||
if (lock != null) {
|
|
||||||
synchronized (lock) {
|
|
||||||
lock.success = false;
|
|
||||||
lock.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// rpc back msg
|
|
||||||
if (appid.equals(topic)) {
|
|
||||||
rpcBackQueue.add(Event.of(topic, value));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// rpc call msg
|
|
||||||
if (rpcTopics.contains(topic)) {
|
|
||||||
rpcCallQueue.add(Event.of(topic, value));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// oth msg
|
|
||||||
topicQueue.add(Event.of(topic, value));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// timer 消息
|
|
||||||
if ("*2".equals(readLine)) {
|
|
||||||
readLine = reader.readLine(); // $7 len()
|
|
||||||
type = reader.readLine(); // message
|
|
||||||
if (!"timer".equals(type)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
reader.readLine(); //$n len(key)
|
|
||||||
String topic = reader.readLine(); // name
|
|
||||||
|
|
||||||
logger.finest("timer[" + topic + "]: ");
|
|
||||||
timerQueue.add(timerMap.get(topic));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.finest(readLine);
|
|
||||||
} catch (IOException e) {
|
|
||||||
if (e instanceof SocketException) {
|
|
||||||
initSocket(Integer.MAX_VALUE);
|
|
||||||
}
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}
|
||||||
}).thenAcceptAsync(x -> {
|
}).start();
|
||||||
// 定时调度事件,已加入耗时监控
|
|
||||||
new Thread(() -> {
|
|
||||||
ExecutorService pool = Executors.newFixedThreadPool(1);
|
|
||||||
while (true) {
|
|
||||||
Timer timer = null;
|
|
||||||
try {
|
|
||||||
timer = timerQueue.take();
|
|
||||||
|
|
||||||
long start = System.currentTimeMillis();
|
// topic msg
|
||||||
pool.submit(timer.runnable).get(5, TimeUnit.SECONDS);
|
new Thread(() -> {
|
||||||
long end = System.currentTimeMillis();
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
logger.finest(String.format("timer [%s] : elapsed time %s ms", timer.name, end - start));
|
while (true) {
|
||||||
} catch (InterruptedException e) {
|
Event<String> event = null;
|
||||||
e.printStackTrace();
|
try {
|
||||||
} catch (TimeoutException e) {
|
event = topicQueue.take();
|
||||||
logger.log(Level.SEVERE, "timer [" + timer.name + "] time out: " + 5 + " S", e);
|
logger.log(Level.FINE, "topic[" + event.topic + "] :" + event.value);
|
||||||
pool = Executors.newFixedThreadPool(1);
|
|
||||||
} catch (Exception e) {
|
String topic = event.topic;
|
||||||
logger.log(Level.WARNING, "timer [" + timer.name + "]", e);
|
String value = event.value;
|
||||||
|
executor.submit(() -> accept(topic, value)).get(5, TimeUnit.SECONDS);
|
||||||
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||||
|
if (e instanceof TimeoutException) {
|
||||||
|
executor = Executors.newSingleThreadExecutor();
|
||||||
|
logger.log(Level.WARNING, "TimeoutException, topic[" + event.topic + "], value[" + event.value + "]", e);
|
||||||
|
} else if (event != null) {
|
||||||
|
logger.log(Level.WARNING, "topic[" + event.topic + "] event accept error :" + event.value, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}
|
||||||
|
}, "ZHub-topic-accept").start();
|
||||||
|
// rpc back
|
||||||
|
new Thread(() -> {
|
||||||
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
|
while (true) {
|
||||||
|
Event<String> event = null;
|
||||||
|
try {
|
||||||
|
event = rpcBackQueue.take();
|
||||||
|
logger.info(String.format("rpc-back:[%s]: %s", event.topic, event.value));
|
||||||
|
rpcAccept(event.value);
|
||||||
|
|
||||||
// topic msg,已加入耗时监控
|
String value = event.value;
|
||||||
new Thread(() -> {
|
executor.submit(() -> rpcAccept(value)).get(5, TimeUnit.SECONDS);
|
||||||
ExecutorService pool = Executors.newFixedThreadPool(1);
|
|
||||||
while (true) {
|
|
||||||
Event<String> event = null;
|
|
||||||
try {
|
|
||||||
event = topicQueue.take();
|
|
||||||
|
|
||||||
String topic = event.topic;
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||||
String value = event.value;
|
if (e instanceof TimeoutException) {
|
||||||
pool.submit(() -> accept(topic, value)).get(5, TimeUnit.SECONDS);
|
executor = Executors.newSingleThreadExecutor();
|
||||||
} catch (InterruptedException e) {
|
logger.log(Level.WARNING, "rpc-back TimeoutException, topic[" + event.topic + "], value[" + event.value + "]", e);
|
||||||
e.printStackTrace();
|
} else if (event != null) {
|
||||||
} catch (TimeoutException e) {
|
logger.log(Level.WARNING, "rpc-back[" + event.topic + "] event accept error :" + event.value, e);
|
||||||
logger.log(Level.SEVERE, "topic[" + event.topic + "] event deal time out: " + 5 + " S, value: " + toStr(event.value), e);
|
}
|
||||||
pool = Executors.newFixedThreadPool(1);
|
throw new RuntimeException(e);
|
||||||
} catch (Exception e) {
|
}
|
||||||
logger.log(Level.WARNING, "topic[" + event.topic + "] event accept error :" + toStr(event.value), e);
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
// rpc call
|
||||||
|
new Thread(() -> {
|
||||||
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
|
while (true) {
|
||||||
|
Event<String> event = null;
|
||||||
|
try {
|
||||||
|
event = rpcCallQueue.take();
|
||||||
|
logger.info(String.format("rpc-call:[%s] %s", event.topic, event.value));
|
||||||
|
|
||||||
|
String topic = event.topic;
|
||||||
|
String value = event.value;
|
||||||
|
executor.submit(() -> accept(topic, value)).get(5, TimeUnit.SECONDS);
|
||||||
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||||
|
if (e instanceof TimeoutException) {
|
||||||
|
executor = Executors.newSingleThreadExecutor();
|
||||||
|
logger.log(Level.WARNING, "rpc-call TimeoutException, topic[" + event.topic + "], value[" + event.value + "]", e);
|
||||||
|
} else if (event != null) {
|
||||||
|
logger.log(Level.WARNING, "rpc-call[" + event.topic + "] event accept error :" + event.value, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}
|
||||||
|
}, "ZHub-rpc-call").start();
|
||||||
|
|
||||||
|
// send msg
|
||||||
|
new Thread(() -> {
|
||||||
|
while (true) {
|
||||||
|
String msg = null;
|
||||||
|
try {
|
||||||
|
msg = sendMsgQueue.take();
|
||||||
|
writer.write(msg.getBytes(UTF_8));
|
||||||
|
writer.flush();
|
||||||
|
} catch (InterruptedException | IOException e) {
|
||||||
|
logger.log(Level.WARNING, "send-msg[" + msg + "] event accept error :", e);
|
||||||
|
|
||||||
// rpc back ,仅做数据解析,暂无耗时监控
|
|
||||||
new Thread(() -> {
|
|
||||||
while (true) {
|
|
||||||
Event<Object> event = null;
|
|
||||||
try {
|
try {
|
||||||
event = rpcBackQueue.take();
|
Thread.sleep(3000);
|
||||||
//if (event)
|
assert msg != null;
|
||||||
logger.finest(String.format("rpc-back:[%s]: %s", event.topic, toStr(event.value)));
|
writer.write(msg.getBytes(UTF_8));
|
||||||
rpcAccept(event.value);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.log(Level.WARNING, "rpc-back[" + event.topic + "] event accept error :" + toStr(event.value), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
|
|
||||||
// rpc call,已加入耗时监控
|
|
||||||
new Thread(() -> {
|
|
||||||
ExecutorService pool = Executors.newFixedThreadPool(1);
|
|
||||||
while (true) {
|
|
||||||
Event<Object> event = null;
|
|
||||||
try {
|
|
||||||
event = rpcCallQueue.take();
|
|
||||||
|
|
||||||
logger.finest(String.format("rpc-call:[%s] %s", event.topic, toStr(event.value)));
|
|
||||||
String topic = event.topic;
|
|
||||||
Object value = event.value;
|
|
||||||
pool.submit(() -> rpcAccept(topic, value)).get(5, TimeUnit.SECONDS);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (TimeoutException e) {
|
|
||||||
logger.log(Level.SEVERE, "topic[" + event.topic + "] event deal time out: " + 5 + " S, value: " + toStr(event.value), e);
|
|
||||||
pool = Executors.newFixedThreadPool(1);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.log(Level.WARNING, "rpc-call[" + event.topic + "] event accept error :" + toStr(event.value), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
|
|
||||||
// send msg
|
|
||||||
new Thread(() -> {
|
|
||||||
while (true) {
|
|
||||||
String msg = null;
|
|
||||||
try {
|
|
||||||
msg = sendMsgQueue.take();
|
|
||||||
|
|
||||||
// logger.log(Level.FINEST, "send-msg: [" + msg + "]");
|
|
||||||
writer.write(msg.getBytes());
|
|
||||||
writer.flush();
|
writer.flush();
|
||||||
} catch (InterruptedException e) {
|
} catch (IOException | InterruptedException | NullPointerException ex) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (Exception e) {
|
|
||||||
logger.log(Level.WARNING, "send-msg[" + msg + "] event accept error :", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}
|
||||||
});
|
}).start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------
|
// ---------------------
|
||||||
@@ -347,6 +365,16 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
return str.length();
|
return str.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String toStr(Object v) {
|
||||||
|
if (v instanceof String) {
|
||||||
|
return (String) v;
|
||||||
|
} else if (v == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return gson.toJson(v);
|
||||||
|
}
|
||||||
|
|
||||||
protected boolean initSocket(int retry) {
|
protected boolean initSocket(int retry) {
|
||||||
for (int i = 0; i <= retry; i++) {
|
for (int i = 0; i <= retry; i++) {
|
||||||
try {
|
try {
|
||||||
@@ -354,7 +382,8 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
String host = hostPort[0];
|
String host = hostPort[0];
|
||||||
int port = Integer.parseInt(hostPort[1]);
|
int port = Integer.parseInt(hostPort[1]);
|
||||||
|
|
||||||
client = new Socket();
|
//private ReentrantLock lock = new ReentrantLock();
|
||||||
|
Socket client = new Socket();
|
||||||
client.connect(new InetSocketAddress(host, port));
|
client.connect(new InetSocketAddress(host, port));
|
||||||
client.setKeepAlive(true);
|
client.setKeepAlive(true);
|
||||||
|
|
||||||
@@ -368,7 +397,9 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
send("auth", auth);
|
send("auth", auth);
|
||||||
send("groupid " + groupid);
|
send("groupid " + groupid);
|
||||||
|
|
||||||
StringBuilder buf = new StringBuilder("subscribe lock trylock");
|
StringBuffer buf = new StringBuffer("subscribe lock trylock");
|
||||||
|
/*if (isMain) {
|
||||||
|
}*/
|
||||||
if (mainHub.containsValue(this)) {
|
if (mainHub.containsValue(this)) {
|
||||||
buf.append(" ").append(appid);
|
buf.append(" ").append(appid);
|
||||||
}
|
}
|
||||||
@@ -380,13 +411,13 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
// 重连 timer 订阅
|
// 重连 timer 订阅
|
||||||
timerMap.forEach((name, timer) -> send("timer", name));
|
timerMap.forEach((name, timer) -> send("timer", name));
|
||||||
if (retry > 0) {
|
if (retry > 0) {
|
||||||
logger.warning(String.format("ZHubClient[%s][%s] %s Succeed!", getGroupid(), i + 1, "reconnection"));
|
logger.warning(String.format("ZHubClient[%s][%s] %s Succeed!", getGroupid(), i + 1, retry > 0 ? "reconnection" : "init"));
|
||||||
} else {
|
} else {
|
||||||
logger.fine(String.format("ZHubClient[%s] %s Succeed!", getGroupid(), "init"));
|
logger.fine(String.format("ZHubClient[%s] %s Succeed!", getGroupid(), retry > 0 ? "reconnection" : "init"));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (i == 0) {
|
if (retry == 0 || i == 0) {
|
||||||
logger.log(Level.WARNING, String.format("ZHubClient[%s] %s Failed 初始化失败!", getGroupid(), retry == 0 ? "init" : "reconnection"), e);
|
logger.log(Level.WARNING, String.format("ZHubClient[%s] %s Failed 初始化失败!", getGroupid(), retry == 0 ? "init" : "reconnection"), e);
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
@@ -413,15 +444,11 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean publish(String topic, Object v) {
|
public boolean publish(String topic, Object v) {
|
||||||
/*if (eventMap.containsKey(topic)) { // 本地调用
|
|
||||||
topicQueue.add(Event.of(topic, v));
|
|
||||||
return true;
|
|
||||||
}*/
|
|
||||||
return send("publish", topic, toStr(v));
|
return send("publish", topic, toStr(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcast(String topic, Object v) {
|
public void broadcast(String topic, Object v) {
|
||||||
send("broadcast", topic, toStr(v)); // 广播必须走远端模式
|
send("broadcast", topic, toStr(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送 publish 主题消息,若多次发送的 topic + "-" + value 相同,将会做延时重置
|
// 发送 publish 主题消息,若多次发送的 topic + "-" + value 相同,将会做延时重置
|
||||||
@@ -447,16 +474,22 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("M".equals(endchar)) {
|
switch (endchar) {
|
||||||
delay *= (1000 * 60 * 60 * 24 * 30);
|
case "M":
|
||||||
} else if ("d".equals(endchar)) {
|
delay *= (1000 * 60 * 60 * 24 * 30);
|
||||||
delay *= (1000 * 60 * 60 * 24);
|
break;
|
||||||
} else if ("H".equals(endchar)) {
|
case "d":
|
||||||
delay *= (1000 * 60 * 60);
|
delay *= (1000 * 60 * 60 * 24);
|
||||||
} else if ("m".equals(endchar)) {
|
break;
|
||||||
delay *= (1000 * 60);
|
case "H":
|
||||||
} else if ("s".equals(endchar)) {
|
delay *= (1000 * 60 * 60);
|
||||||
delay *= 1000;
|
break;
|
||||||
|
case "m":
|
||||||
|
delay *= (1000 * 60);
|
||||||
|
break;
|
||||||
|
case "s":
|
||||||
|
delay *= 1000;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(topic, v, delay);
|
delay(topic, v, delay);
|
||||||
@@ -473,7 +506,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ================================================== lock ==================================================
|
// ================================================== lock ==================================================
|
||||||
private Map<String, Lock> lockTag = new ConcurrentHashMap<>();
|
private final Map<String, Lock> lockTag = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 尝试加锁,立即返回,
|
* 尝试加锁,立即返回,
|
||||||
@@ -514,7 +547,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ================================================== timer ==================================================
|
// ================================================== timer ==================================================
|
||||||
private ConcurrentHashMap<String, Timer> timerMap = new ConcurrentHashMap();
|
private final ConcurrentHashMap<String, Timer> timerMap = new ConcurrentHashMap();
|
||||||
|
|
||||||
class Timer {
|
class Timer {
|
||||||
String name;
|
String name;
|
||||||
@@ -542,7 +575,6 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
send("timer", name);
|
send("timer", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public void reloadTimer() {
|
public void reloadTimer() {
|
||||||
send("cmd", "reload-timer");
|
send("cmd", "reload-timer");
|
||||||
}
|
}
|
||||||
@@ -550,7 +582,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
// ================================================== rpc ==================================================
|
// ================================================== rpc ==================================================
|
||||||
// -- 调用端 --
|
// -- 调用端 --
|
||||||
private static final Map<String, Rpc> rpcMap = new ConcurrentHashMap<>();
|
private static final Map<String, Rpc> rpcMap = new ConcurrentHashMap<>();
|
||||||
// private static final Map<String, TypeToken> rpcRetType = new ConcurrentHashMap<>();
|
private static final Map<String, TypeToken> rpcRetType = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
// rpc call
|
// rpc call
|
||||||
public RpcResult<Void> rpc(String topic, Object v) {
|
public RpcResult<Void> rpc(String topic, Object v) {
|
||||||
@@ -564,18 +596,14 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
|
|
||||||
// rpc call
|
// rpc call
|
||||||
public <T, R> RpcResult<R> rpc(String topic, T v, TypeToken<R> typeToken, long timeout) {
|
public <T, R> RpcResult<R> rpc(String topic, T v, TypeToken<R> typeToken, long timeout) {
|
||||||
Rpc rpc = new Rpc<>(appid, topic, v);
|
Rpc rpc = new Rpc<>(appid, UUID.randomUUID().toString().replaceAll("-", ""), topic, v);
|
||||||
rpc.setTypeToken(typeToken);
|
|
||||||
|
|
||||||
String ruk = rpc.getRuk();
|
String ruk = rpc.getRuk();
|
||||||
rpcMap.put(ruk, rpc);
|
rpcMap.put(ruk, rpc);
|
||||||
|
if (typeToken != null) {
|
||||||
|
rpcRetType.put(ruk, typeToken);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if (eventMap.containsKey(topic)) { // 本地调用
|
publish(topic, rpc); // send("rpc", topic, toStr(rpc));
|
||||||
rpcCallQueue.add(Event.of(topic, rpc));
|
|
||||||
} else {
|
|
||||||
rpc.setValue(toStr(rpc.getValue()));
|
|
||||||
publish(topic, rpc); // send("rpc", topic, toStr(rpc));
|
|
||||||
}
|
|
||||||
synchronized (rpc) {
|
synchronized (rpc) {
|
||||||
if (timeout <= 0) {
|
if (timeout <= 0) {
|
||||||
timeout = 1000 * 15;
|
timeout = 1000 * 15;
|
||||||
@@ -625,30 +653,8 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
|
|
||||||
// RpcResult: {ruk:xxx-xxxx, retcode:0}
|
// RpcResult: {ruk:xxx-xxxx, retcode:0}
|
||||||
// rpc call back consumer
|
// rpc call back consumer
|
||||||
private <T> void rpcAccept(T value) {
|
private void rpcAccept(String value) {
|
||||||
// 接收到 本地调用返回的 RpcResult
|
RpcResult resp = gson.fromJson(value, new TypeToken<RpcResult<String>>() {
|
||||||
if (value instanceof RpcResult) {
|
|
||||||
String ruk = ((RpcResult) value).getRuk();
|
|
||||||
Rpc rpc = rpcMap.remove(ruk);
|
|
||||||
if (rpc == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO, 本地模式下返回的数据对象类型需要和处理端一致,不然会出现类型转换异常 - 解决办法,当出现不一致的情况取数据做转换
|
|
||||||
TypeToken typeToken = rpc.getTypeToken();
|
|
||||||
if (typeToken.getType() != ((RpcResult<?>) value).getResult().getClass()) {
|
|
||||||
Object result = gson.fromJson(toStr(((RpcResult<?>) value).getResult()), typeToken.getType());
|
|
||||||
((RpcResult<Object>) value).setResult(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
rpc.setRpcResult((RpcResult) value);
|
|
||||||
synchronized (rpc) {
|
|
||||||
rpc.notify();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
RpcResult resp = gson.fromJson((String) value, new TypeToken<RpcResult<String>>() {
|
|
||||||
}.getType());
|
}.getType());
|
||||||
|
|
||||||
String ruk = resp.getRuk();
|
String ruk = resp.getRuk();
|
||||||
@@ -656,10 +662,10 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
if (rpc == null) {
|
if (rpc == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
TypeToken typeToken = rpc.getTypeToken();
|
TypeToken typeToken = rpcRetType.get(ruk);
|
||||||
|
|
||||||
Object result = resp.getResult();
|
Object result = resp.getResult();
|
||||||
if (result != null && typeToken != null && !"java.lang.String".equals(typeToken.getType().getTypeName()) && !"java.lang.Void".equals(typeToken.getType().getTypeName())) {
|
if (result != null && typeToken != null && !"java.lang.String".equals(typeToken.getType().getTypeName())) {
|
||||||
result = gson.fromJson((String) resp.getResult(), typeToken.getType());
|
result = gson.fromJson((String) resp.getResult(), typeToken.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -671,38 +677,22 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// -- 订阅端 --
|
// -- 订阅端 --
|
||||||
private Set<String> rpcTopics = new HashSet();
|
private final HashSet rpcTopics = new HashSet();
|
||||||
|
|
||||||
// rpc call consumer
|
|
||||||
public <R> void rpcSubscribe(String topic, Function<Rpc<String>, RpcResult<R>> fun) {
|
|
||||||
rpcSubscribe(topic, IType.STRING, fun);
|
|
||||||
}
|
|
||||||
|
|
||||||
// rpc call consumer
|
// rpc call consumer
|
||||||
public <T, R> void rpcSubscribe(String topic, TypeToken<T> typeToken, Function<Rpc<T>, RpcResult<R>> fun) {
|
public <T, R> void rpcSubscribe(String topic, TypeToken<T> typeToken, Function<Rpc<T>, RpcResult<R>> fun) {
|
||||||
Consumer<T> consumer = v -> {
|
Consumer<String> consumer = v -> {
|
||||||
Rpc<T> rpc = null;
|
Rpc<T> rpc = null;
|
||||||
try {
|
try {
|
||||||
if (v instanceof String) {
|
rpc = gson.fromJson(v, new TypeToken<Rpc<String>>() {
|
||||||
rpc = gson.fromJson((String) v, new TypeToken<Rpc<String>>() {
|
}.getType());
|
||||||
}.getType());
|
|
||||||
} else {
|
|
||||||
rpc = (Rpc<T>) v;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 参数转换
|
// 参数转换
|
||||||
if (rpc.getValue() instanceof String && !"java.lang.String".equals(typeToken.getType().getTypeName())) {
|
T paras = gson.fromJson((String) rpc.getValue(), typeToken.getType());
|
||||||
T paras = gson.fromJson((String) rpc.getValue(), typeToken.getType());
|
rpc.setValue(paras);
|
||||||
rpc.setValue(paras);
|
|
||||||
}
|
|
||||||
|
|
||||||
RpcResult result = fun.apply(rpc);
|
RpcResult result = fun.apply(rpc);
|
||||||
if (appid.equals(rpc.getBackTopic())) {
|
result.setResult(toStr(result.getResult()));
|
||||||
rpcBackQueue.add(Event.of(topic, result));
|
publish(rpc.getBackTopic(), result);
|
||||||
} else {
|
|
||||||
result.setResult(toStr(result.getResult())); // 远程模式 结果转换
|
|
||||||
publish(rpc.getBackTopic(), result);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.WARNING, "rpc call consumer error: " + v, e);
|
logger.log(Level.WARNING, "rpc call consumer error: " + v, e);
|
||||||
publish(rpc.getBackTopic(), rpc.retError("服务调用失败!"));
|
publish(rpc.getBackTopic(), rpc.retError("服务调用失败!"));
|
||||||
@@ -711,12 +701,6 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
};
|
};
|
||||||
|
|
||||||
rpcTopics.add(topic);
|
rpcTopics.add(topic);
|
||||||
subscribe(topic, typeToken, consumer);
|
subscribe(topic, consumer);
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
System.out.println(IType.INT.getType());
|
|
||||||
Integer a = 1;
|
|
||||||
System.out.println(a.getClass());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,8 +1,8 @@
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import net.tccn.IType;
|
import tccn.IType;
|
||||||
import net.tccn.zhub.Lock;
|
import tccn.zhub.Lock;
|
||||||
import net.tccn.zhub.ZHubClient;
|
import tccn.zhub.ZHubClient;
|
||||||
|
|
||||||
// @RestService(automapping = true)
|
// @RestService(automapping = true)
|
||||||
public class HelloService {
|
public class HelloService {
|
||||||
|
Reference in New Issue
Block a user