Compare commits
3 Commits
f4771aadf2
...
e2c2b62665
Author | SHA1 | Date | |
---|---|---|---|
e2c2b62665 | |||
e8b9140103 | |||
1786724c88 |
12
pom.xml
12
pom.xml
@ -5,8 +5,8 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>net.tccn</groupId>
|
<groupId>net.tccn</groupId>
|
||||||
<artifactId>zhub-cli</artifactId>
|
<artifactId>zhub-client-spring</artifactId>
|
||||||
<version>1.0</version>
|
<version>0.1.1</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
@ -33,11 +33,11 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<!--<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
<id>maven-nexus</id>
|
<id>maven-nexus</id>
|
||||||
<name>maven-nexus</name>
|
<name>maven-nexus</name>
|
||||||
<url>http://47.106.237.198:8081/repository/maven-public/</url>
|
<url>https://nexus.1216.top/repository/maven-public/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
@ -46,8 +46,8 @@
|
|||||||
<repository>
|
<repository>
|
||||||
<id>mvn-release</id>
|
<id>mvn-release</id>
|
||||||
<name>mvn-release</name>
|
<name>mvn-release</name>
|
||||||
<url>http://47.106.237.198:8081/repository/maven-releases/</url>
|
<url>https://nexus.1216.top/repository/maven-releases/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>-->
|
</distributionManagement>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -15,8 +15,8 @@ public class Event<V> {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <V> Event of(String topic, V value) {
|
public static <V> Event<V> of(String topic, V value) {
|
||||||
return new Event<V>(topic, value);
|
return new Event<>(topic, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,15 +21,14 @@ import java.util.HashMap;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
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 {
|
||||||
|
|
||||||
@ -60,12 +59,6 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
private final LinkedBlockingQueue<Event<String>> rpcCallQueue = new LinkedBlockingQueue<>(); // RPC CALL MSG
|
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 final BiConsumer<Runnable, Integer> threadBuilder = (r, n) -> {
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
new Thread(() -> r.run()).start();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/*private static boolean isFirst = true;
|
/*private static boolean isFirst = true;
|
||||||
private boolean isMain = false;*/
|
private boolean isMain = false;*/
|
||||||
private static final Map<String, ZHubClient> mainHub = new HashMap<>(); // 127.0.0.1:1216 - ZHubClient
|
private static final Map<String, ZHubClient> mainHub = new HashMap<>(); // 127.0.0.1:1216 - ZHubClient
|
||||||
@ -212,98 +205,119 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
// 定时调度事件
|
// 定时调度事件
|
||||||
threadBuilder.accept(() -> {
|
new Thread(() -> {
|
||||||
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
while (true) {
|
while (true) {
|
||||||
Timer timer = null;
|
Timer timer = null;
|
||||||
try {
|
try {
|
||||||
if ((timer = timerQueue.take()) == null) {
|
timer = timerQueue.take();
|
||||||
return;
|
|
||||||
}
|
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
timer.runnable.run();
|
executor.submit(timer.runnable).get(5, TimeUnit.SECONDS);
|
||||||
long end = System.currentTimeMillis();
|
long end = System.currentTimeMillis();
|
||||||
logger.finest(String.format("timer [%s] : elapsed time %s ms", timer.name, end - start));
|
logger.finest(String.format("timer [%s] : elapsed time %s ms", timer.name, end - start));
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||||
|
if (e instanceof TimeoutException) {
|
||||||
|
executor = Executors.newSingleThreadExecutor();
|
||||||
|
logger.log(Level.WARNING, "TimeoutException [" + timer.name + "]", e);
|
||||||
|
} else {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (Exception e) {
|
|
||||||
logger.log(Level.WARNING, "timer [" + timer.name + "]", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 1);
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
// topic msg
|
// topic msg
|
||||||
threadBuilder.accept(() -> {
|
new Thread(() -> {
|
||||||
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
while (true) {
|
while (true) {
|
||||||
Event<String> event = null;
|
Event<String> event = null;
|
||||||
try {
|
try {
|
||||||
if ((event = topicQueue.take()) == null) {
|
event = topicQueue.take();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
logger.log(Level.FINE, "topic[" + event.topic + "] :" + event.value);
|
logger.log(Level.FINE, "topic[" + event.topic + "] :" + event.value);
|
||||||
accept(event.topic, event.value);
|
|
||||||
} catch (InterruptedException e) {
|
String topic = event.topic;
|
||||||
e.printStackTrace();
|
String value = event.value;
|
||||||
} catch (Exception e) {
|
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);
|
logger.log(Level.WARNING, "topic[" + event.topic + "] event accept error :" + event.value, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 1);
|
}
|
||||||
|
}, "ZHub-topic-accept").start();
|
||||||
// rpc back
|
// rpc back
|
||||||
threadBuilder.accept(() -> {
|
new Thread(() -> {
|
||||||
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
while (true) {
|
while (true) {
|
||||||
Event<String> event = null;
|
Event<String> event = null;
|
||||||
try {
|
try {
|
||||||
if ((event = rpcBackQueue.take()) == null) {
|
event = rpcBackQueue.take();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
//if (event)
|
|
||||||
logger.info(String.format("rpc-back:[%s]: %s", event.topic, event.value));
|
logger.info(String.format("rpc-back:[%s]: %s", event.topic, event.value));
|
||||||
rpcAccept(event.value);
|
rpcAccept(event.value);
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
String value = event.value;
|
||||||
} catch (Exception e) {
|
executor.submit(() -> rpcAccept(value)).get(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||||
|
if (e instanceof TimeoutException) {
|
||||||
|
executor = Executors.newSingleThreadExecutor();
|
||||||
|
logger.log(Level.WARNING, "rpc-back TimeoutException, topic[" + event.topic + "], value[" + event.value + "]", e);
|
||||||
|
} else if (event != null) {
|
||||||
logger.log(Level.WARNING, "rpc-back[" + event.topic + "] event accept error :" + event.value, e);
|
logger.log(Level.WARNING, "rpc-back[" + event.topic + "] event accept error :" + event.value, e);
|
||||||
}
|
}
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}, 1);
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
// rpc call
|
// rpc call
|
||||||
threadBuilder.accept(() -> {
|
new Thread(() -> {
|
||||||
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
while (true) {
|
while (true) {
|
||||||
Event<String> event = null;
|
Event<String> event = null;
|
||||||
try {
|
try {
|
||||||
if ((event = rpcCallQueue.take()) == null) {
|
event = rpcCallQueue.take();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
logger.info(String.format("rpc-call:[%s] %s", event.topic, event.value));
|
logger.info(String.format("rpc-call:[%s] %s", event.topic, event.value));
|
||||||
accept(event.topic, event.value);
|
|
||||||
} catch (InterruptedException e) {
|
String topic = event.topic;
|
||||||
e.printStackTrace();
|
String value = event.value;
|
||||||
} catch (Exception e) {
|
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);
|
logger.log(Level.WARNING, "rpc-call[" + event.topic + "] event accept error :" + event.value, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 1);
|
}
|
||||||
|
}, "ZHub-rpc-call").start();
|
||||||
|
|
||||||
// send msg
|
// send msg
|
||||||
threadBuilder.accept(() -> {
|
new Thread(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
String msg = null;
|
String msg = null;
|
||||||
try {
|
try {
|
||||||
if ((msg = sendMsgQueue.take()) == null) {
|
msg = sendMsgQueue.take();
|
||||||
continue;
|
writer.write(msg.getBytes(UTF_8));
|
||||||
}
|
|
||||||
// logger.log(Level.FINEST, "send-msg: [" + msg + "]");
|
|
||||||
writer.write(msg.getBytes());
|
|
||||||
writer.flush();
|
writer.flush();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException | IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.log(Level.WARNING, "send-msg[" + msg + "] event accept error :", e);
|
logger.log(Level.WARNING, "send-msg[" + msg + "] event accept error :", e);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(3000);
|
||||||
|
assert msg != null;
|
||||||
|
writer.write(msg.getBytes(UTF_8));
|
||||||
|
writer.flush();
|
||||||
|
} catch (IOException | InterruptedException | NullPointerException ex) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 1);
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user