4 Commits

Author SHA1 Message Date
efdf42c4f2 修改:包结构名称 2024-04-24 20:05:12 +08:00
4d125f8e46 修复:rpc 数据解析错误 2024-04-22 00:13:56 +08:00
8a5f1c10e1 优化:1、本地模式返回结果与目标类型不一致的转换
2、其他优化
2024-04-09 02:27:14 +08:00
9d23c84cba . 2024-04-07 23:35:31 +08:00
26 changed files with 81 additions and 60 deletions

View File

@@ -4,9 +4,9 @@
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>
<groupId>net.tccn</groupId>
<groupId>dev.zhub</groupId>
<artifactId>zhub-client-redkale</artifactId>
<version>0.1.1-dev</version>
<version>x.1.1.dev</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>

View File

@@ -1,4 +1,4 @@
package net.tccn;
package dev.zhub;
import org.redkale.convert.json.JsonConvert;
import org.redkale.util.Resourcable;

View File

@@ -1,4 +1,4 @@
package net.tccn;
package dev.zhub;
/**
* 发布订阅 事件

View File

@@ -1,4 +1,4 @@
package net.tccn;
package dev.zhub;
import org.redkale.util.TypeToken;

View File

@@ -1,4 +1,4 @@
package net.tccn;
package dev.zhub;
import org.redkale.util.TypeToken;

View File

@@ -1,4 +1,4 @@
package net.tccn;
package dev.zhub;
import java.util.logging.Logger;

View File

@@ -1,4 +1,4 @@
package net.tccn;
package dev.zhub;
import org.redkale.util.TypeToken;

View File

@@ -1,4 +1,4 @@
package net.tccn;
package dev.zhub;
import org.junit.Test;
import org.redkale.net.AsyncIOGroup;

View File

@@ -1,4 +1,4 @@
package net.tccn;
package dev.zhub;
import org.redkale.boot.Application;
import org.redkale.boot.NodeServer;
@@ -8,7 +8,6 @@ import org.redkale.service.Service;
import org.redkale.util.ResourceEvent;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

View File

@@ -1,6 +1,6 @@
package net.tccn;
package dev.zhub;
import net.tccn.zhub.ZHubClient;
import dev.zhub.client.ZHubClient;
import org.redkale.annotation.Priority;
import org.redkale.cluster.ClusterAgent;
import org.redkale.cluster.ClusterAgentProvider;

View File

@@ -1,4 +1,4 @@
package net.tccn.zhub;
package dev.zhub.client;
// ================================================== lock ==================================================
public class Lock {

View File

@@ -1,7 +1,9 @@
package net.tccn.zhub;
package dev.zhub.client;
import org.redkale.convert.ConvertColumn;
import org.redkale.service.RetResult;
import org.redkale.util.TypeToken;
import org.redkale.util.Utility;
public class Rpc<T> {
private String ruk; // request unique key:
@@ -10,11 +12,13 @@ public class Rpc<T> {
private RpcResult rpcResult;
private TypeToken typeToken;
public Rpc() {
}
protected Rpc(String appname, String ruk, String topic, T value) {
this.ruk = appname + "::" + ruk;
protected Rpc(String appname, String topic, T value) {
this.ruk = appname + "::" + Utility.uuid();
this.topic = topic;
this.value = value;
}
@@ -52,6 +56,16 @@ public class Rpc<T> {
this.rpcResult = rpcResult;
}
@ConvertColumn(ignore = true)
public TypeToken getTypeToken() {
return typeToken;
}
@ConvertColumn(ignore = true)
public void setTypeToken(TypeToken typeToken) {
this.typeToken = typeToken;
}
@ConvertColumn(ignore = true)
public String getBackTopic() {
return ruk.split("::")[0];

View File

@@ -1,4 +1,4 @@
package net.tccn.zhub;
package dev.zhub.client;
public class RpcResult<R> {
private String ruk;

View File

@@ -1,7 +1,7 @@
package net.tccn.zhub;
package dev.zhub.client;
import net.tccn.*;
import net.tccn.timer.Timers;
import dev.zhub.*;
import dev.zhub.timer.Timers;
import org.redkale.annotation.AutoLoad;
import org.redkale.annotation.ResourceType;
import org.redkale.service.Local;
@@ -9,7 +9,6 @@ import org.redkale.service.Service;
import org.redkale.util.AnyValue;
import org.redkale.util.Comment;
import org.redkale.util.TypeToken;
import org.redkale.util.Utility;
import java.io.BufferedReader;
import java.io.IOException;
@@ -100,12 +99,11 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
}
CompletableFuture.runAsync(() -> {
if (!initSocket(0)) {
return;
}
// 消息 事件接收
new Thread(() -> {
if (!initSocket(0)) {
return;
}
while (true) {
try {
String readLine = reader.readLine();
@@ -410,7 +408,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
send("auth", auth);
send("groupid " + groupid);
StringBuffer buf = new StringBuffer("subscribe lock trylock");
StringBuilder buf = new StringBuilder("subscribe lock trylock");
if (mainHub.containsValue(this)) {
buf.append(" " + APP_NAME);
}
@@ -424,9 +422,9 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
send("timer", name);
});
if (retry > 0) {
logger.warning(String.format("ZHubClient[%s][%s] %s Succeed", getGroupid(), i + 1, retry > 0 ? "reconnection" : "init"));
logger.warning(String.format("ZHubClient[%s][%s] %s Succeed", getGroupid(), i + 1, "reconnection"));
} else {
logger.fine(String.format("ZHubClient[%s] %s Succeed", getGroupid(), retry > 0 ? "reconnection" : "init"));
logger.fine(String.format("ZHubClient[%s] %s Succeed", getGroupid(), "init"));
}
return true;
} catch (Exception e) {
@@ -594,7 +592,6 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
// ================================================== rpc ==================================================
// -- 调用端 --
private static Map<String, Rpc> rpcMap = new ConcurrentHashMap<>();
private static Map<String, TypeToken> rpcRetType = new ConcurrentHashMap<>();
@Comment("rpc call")
public RpcResult<Void> rpc(String topic, Object v) {
@@ -608,12 +605,11 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
@Comment("rpc call")
public <T, R> RpcResult<R> rpc(String topic, T v, TypeToken<R> typeToken, long timeout) {
Rpc rpc = new Rpc<>(APP_NAME, Utility.uuid(), topic, v);
Rpc rpc = new Rpc<>(APP_NAME, topic, v);
rpc.setTypeToken(typeToken);
String ruk = rpc.getRuk();
rpcMap.put(ruk, rpc);
if (typeToken != null) {
rpcRetType.put(ruk, typeToken);
}
try {
if (eventMap.containsKey(topic)) { // 本地调用
rpcCallQueue.add(Event.of(topic, rpc));
@@ -671,10 +667,21 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
// RpcResult: {ruk:xxx-xxxx, retcode:0}
@Comment("rpc call back consumer")
private <T> void rpcAccept(T value) {
// 接收到 本地调用返回的 RpcResult TODO, 本地模式下返回的数据对象类型需要和处理端一致不然会出现类型转换异常 - 解决办法当出现不一致的情况取数据做转换
// 接收到 本地调用返回的 RpcResult
if (value instanceof RpcResult) {
String ruk = ((RpcResult) value).getRuk();
Rpc rpc = rpcMap.remove(ruk);
if (rpc == null) {
return;
}
// 本地模式下返回的数据对象类型需要和处理端一致不然会出现类型转换异常 - 解决办法当出现不一致的情况取数据做转换
TypeToken typeToken = rpc.getTypeToken();
if (typeToken.getType() != ((RpcResult<?>) value).getResult().getClass()) {
Object result = convert.convertFrom(typeToken.getType(), toStr(((RpcResult<?>) value).getResult()));
((RpcResult<Object>) value).setResult(result);
}
rpc.setRpcResult((RpcResult) value);
synchronized (rpc) {
rpc.notify();
@@ -690,7 +697,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
if (rpc == null) {
return;
}
TypeToken typeToken = rpcRetType.get(ruk);
TypeToken typeToken = rpc.getTypeToken();
Object result = resp.getResult();
if (result != null && typeToken != null && !"java.lang.String".equals(typeToken.getType().getTypeName()) && !"java.lang.Void".equals(typeToken.getType().getTypeName())) {
@@ -718,7 +725,8 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
Rpc<T> rpc = null;
try {
if (v instanceof String) {
rpc = convert.convertFrom(IType.STRING.getType(), (String) v);
rpc = convert.convertFrom(new TypeToken<Rpc<String>>() {
}.getType(), (String) v);
} else {
rpc = (Rpc<T>) v;
}

View File

@@ -1,7 +1,7 @@
package net.tccn.timer;
package dev.zhub.timer;
import net.tccn.timer.queue.TimerQueue;
import net.tccn.timer.task.Task;
import dev.zhub.timer.queue.TimerQueue;
import dev.zhub.timer.task.Task;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

View File

@@ -1,8 +1,8 @@
package net.tccn.timer;
package dev.zhub.timer;
import net.tccn.timer.scheduled.Scheduled;
import net.tccn.timer.task.Job;
import net.tccn.timer.task.Task;
import dev.zhub.timer.scheduled.Scheduled;
import dev.zhub.timer.task.Job;
import dev.zhub.timer.task.Task;
import java.time.LocalDateTime;
import java.time.ZoneId;

View File

@@ -1,6 +1,6 @@
package net.tccn.timer;
package dev.zhub.timer;
import net.tccn.timer.scheduled.ScheduledCycle;
import dev.zhub.timer.scheduled.ScheduledCycle;
import org.redkale.util.Utility;
import java.util.function.Supplier;

View File

@@ -1,6 +1,6 @@
package net.tccn.timer.queue;
package dev.zhub.timer.queue;
import net.tccn.timer.task.Task;
import dev.zhub.timer.task.Task;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

View File

@@ -1,4 +1,4 @@
package net.tccn.timer.scheduled;
package dev.zhub.timer.scheduled;
import java.time.LocalDateTime;

View File

@@ -1,4 +1,4 @@
package net.tccn.timer.scheduled;
package dev.zhub.timer.scheduled;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

View File

@@ -1,4 +1,4 @@
package net.tccn.timer.scheduled;
package dev.zhub.timer.scheduled;
import java.time.LocalDate;
import java.time.LocalDateTime;

View File

@@ -1,4 +1,4 @@
package net.tccn.timer.task;
package dev.zhub.timer.task;
/**
* @author: liangxianyou at 2018/12/8 17:24.

View File

@@ -1,7 +1,7 @@
package net.tccn.timer.task;
package dev.zhub.timer.task;
import net.tccn.timer.TimerExecutor;
import net.tccn.timer.scheduled.Scheduled;
import dev.zhub.timer.TimerExecutor;
import dev.zhub.timer.scheduled.Scheduled;
/**
* @author: liangxianyou at 2018/8/5 19:32.

View File

@@ -1 +1 @@
net.tccn.ZhubProvider
dev.zhub.ZhubProvider

View File

@@ -1,7 +1,7 @@
package net.tccn.mq;
import net.tccn.Event;
import net.tccn.timer.Timers;
import dev.zhub.timer.Timers;
import org.junit.Test;
import org.redkale.convert.json.JsonConvert;

View File

@@ -27,15 +27,15 @@ public class HelloService implements Service {
@Resource(name = "vvvvhub2")
private ZHubClient zhub2;*/
//private net.tccn.zhub.ZHubClient zhubx = null;
//private dev.zhub.client.ZHubClient zhubx = null;
@Override
public void init(AnyValue config) {
/*CompletableFuture.runAsync(() -> {
zhubx = new net.tccn.zhub.ZHubClient("127.0.0.1", 1216, "g-dev", "DEV-LOCAL");
//zhubx = new net.tccn.zhub.ZHubClient("47.111.150.118", 6066, "g-dev", "DEV-LOCAL");
zhubx = new dev.zhub.client.ZHubClient("127.0.0.1", 1216, "g-dev", "DEV-LOCAL");
//zhubx = new dev.zhub.client.ZHubClient("47.111.150.118", 6066, "g-dev", "DEV-LOCAL");
});*/
// Function<Rpc<T>, RpcResult<R>> fun