From 13d284184dda70cba6f154046a8010f8a6c51393 Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 2 Sep 2023 20:29:08 +0800 Subject: [PATCH] del[thread]: delete SafeRunnable --- .../java/com/zfoo/event/manager/EventBus.java | 4 +- .../zfoo/net/router/answer/AsyncAnswer.java | 10 ++--- .../zfoo/net/router/answer/IAsyncAnswer.java | 3 +- .../main/java/com/zfoo/net/task/TaskBus.java | 4 +- .../zfoo/scheduler/manager/SchedulerBus.java | 5 +-- .../main/java/com/zfoo/util/ThreadUtils.java | 14 +++++++ .../java/com/zfoo/util/EnumUtilsTest.java | 37 ------------------- 7 files changed, 26 insertions(+), 51 deletions(-) delete mode 100644 util/src/test/java/com/zfoo/util/EnumUtilsTest.java diff --git a/event/src/main/java/com/zfoo/event/manager/EventBus.java b/event/src/main/java/com/zfoo/event/manager/EventBus.java index cd88635e..878ba9ed 100644 --- a/event/src/main/java/com/zfoo/event/manager/EventBus.java +++ b/event/src/main/java/com/zfoo/event/manager/EventBus.java @@ -18,8 +18,8 @@ import com.zfoo.protocol.collection.CollectionUtils; import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject; import com.zfoo.protocol.util.AssertionUtils; import com.zfoo.protocol.util.StringUtils; -import com.zfoo.util.SafeRunnable; import com.zfoo.protocol.util.RandomUtils; +import com.zfoo.util.ThreadUtils; import io.netty.util.concurrent.FastThreadLocalThread; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -134,7 +134,7 @@ public abstract class EventBus { * Use the event thread specified by the hashcode to execute the task */ public static void execute(int executorHash, Runnable runnable) { - executors[Math.abs(executorHash % EXECUTORS_SIZE)].execute(SafeRunnable.valueOf(runnable)); + executors[Math.abs(executorHash % EXECUTORS_SIZE)].execute(ThreadUtils.safeRunnable(runnable)); } /** diff --git a/net/src/main/java/com/zfoo/net/router/answer/AsyncAnswer.java b/net/src/main/java/com/zfoo/net/router/answer/AsyncAnswer.java index 7b935ceb..905349be 100644 --- a/net/src/main/java/com/zfoo/net/router/answer/AsyncAnswer.java +++ b/net/src/main/java/com/zfoo/net/router/answer/AsyncAnswer.java @@ -15,7 +15,7 @@ package com.zfoo.net.router.answer; import com.zfoo.net.router.attachment.SignalAttachment; import com.zfoo.protocol.IPacket; -import com.zfoo.util.SafeRunnable; +import com.zfoo.util.ThreadUtils; import java.util.ArrayList; import java.util.List; @@ -34,7 +34,7 @@ public class AsyncAnswer implements IAsyncAnswer { private Runnable askCallback; - private SafeRunnable notCompleteCallback; + private Runnable notCompleteCallback; @Override @@ -52,8 +52,8 @@ public class AsyncAnswer implements IAsyncAnswer { } @Override - public IAsyncAnswer notComplete(SafeRunnable notCompleteCallback) { - this.notCompleteCallback = notCompleteCallback; + public IAsyncAnswer notComplete(Runnable notCompleteCallback) { + this.notCompleteCallback = ThreadUtils.safeRunnable(notCompleteCallback); return this; } @@ -85,7 +85,7 @@ public class AsyncAnswer implements IAsyncAnswer { this.askCallback = askCallback; } - public SafeRunnable getNotCompleteCallback() { + public Runnable getNotCompleteCallback() { return notCompleteCallback; } } diff --git a/net/src/main/java/com/zfoo/net/router/answer/IAsyncAnswer.java b/net/src/main/java/com/zfoo/net/router/answer/IAsyncAnswer.java index 545edbe1..133a4031 100644 --- a/net/src/main/java/com/zfoo/net/router/answer/IAsyncAnswer.java +++ b/net/src/main/java/com/zfoo/net/router/answer/IAsyncAnswer.java @@ -14,7 +14,6 @@ package com.zfoo.net.router.answer; import com.zfoo.protocol.IPacket; -import com.zfoo.util.SafeRunnable; import java.util.function.Consumer; @@ -36,6 +35,6 @@ public interface IAsyncAnswer { * EN:If the asynchronous request does not return successfully, the method is called * CN:如果异步请求没有成功返回,那么就会回调该方法 */ - IAsyncAnswer notComplete(SafeRunnable notCompleteCallback); + IAsyncAnswer notComplete(Runnable notCompleteCallback); } diff --git a/net/src/main/java/com/zfoo/net/task/TaskBus.java b/net/src/main/java/com/zfoo/net/task/TaskBus.java index d5772b4f..81b4d7f3 100644 --- a/net/src/main/java/com/zfoo/net/task/TaskBus.java +++ b/net/src/main/java/com/zfoo/net/task/TaskBus.java @@ -24,8 +24,8 @@ import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject; import com.zfoo.protocol.util.AssertionUtils; import com.zfoo.protocol.util.StringUtils; import com.zfoo.scheduler.manager.SchedulerBus; -import com.zfoo.util.SafeRunnable; import com.zfoo.protocol.util.RandomUtils; +import com.zfoo.util.ThreadUtils; import io.netty.util.concurrent.FastThreadLocalThread; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -172,7 +172,7 @@ public final class TaskBus { } public static void execute(int taskExecutorHash, Runnable runnable) { - executors[calTaskExecutorHash(taskExecutorHash)].execute(SafeRunnable.valueOf(runnable)); + executors[calTaskExecutorHash(taskExecutorHash)].execute(ThreadUtils.safeRunnable(runnable)); } public static void execute(Object argument, Runnable runnable) { diff --git a/scheduler/src/main/java/com/zfoo/scheduler/manager/SchedulerBus.java b/scheduler/src/main/java/com/zfoo/scheduler/manager/SchedulerBus.java index eac314e6..a475c470 100644 --- a/scheduler/src/main/java/com/zfoo/scheduler/manager/SchedulerBus.java +++ b/scheduler/src/main/java/com/zfoo/scheduler/manager/SchedulerBus.java @@ -18,7 +18,6 @@ import com.zfoo.protocol.util.StringUtils; import com.zfoo.scheduler.SchedulerContext; import com.zfoo.scheduler.model.vo.SchedulerDefinition; import com.zfoo.scheduler.util.TimeUtils; -import com.zfoo.util.SafeRunnable; import com.zfoo.util.ThreadUtils; import io.netty.util.concurrent.FastThreadLocalThread; import org.slf4j.Logger; @@ -168,7 +167,7 @@ public abstract class SchedulerBus { */ public static ScheduledFuture scheduleAtFixedRate(Runnable runnable, long period, TimeUnit unit) { - return executor.scheduleAtFixedRate(SafeRunnable.valueOf(runnable), 0, period, unit); + return executor.scheduleAtFixedRate(ThreadUtils.safeRunnable(runnable), 0, period, unit); } @@ -177,7 +176,7 @@ public abstract class SchedulerBus { */ public static ScheduledFuture schedule(Runnable runnable, long delay, TimeUnit unit) { - return executor.schedule(SafeRunnable.valueOf(runnable), delay, unit); + return executor.schedule(ThreadUtils.safeRunnable(runnable), delay, unit); } /** diff --git a/util/src/main/java/com/zfoo/util/ThreadUtils.java b/util/src/main/java/com/zfoo/util/ThreadUtils.java index d41deda1..9f39028e 100644 --- a/util/src/main/java/com/zfoo/util/ThreadUtils.java +++ b/util/src/main/java/com/zfoo/util/ThreadUtils.java @@ -111,5 +111,19 @@ public abstract class ThreadUtils { }).start(); } + public static Runnable safeRunnable(Runnable runnable) { + return new Runnable() { + @Override + public void run() { + try { + runnable.run(); + } catch (Exception e) { + logger.error("unknown exception", e); + } catch (Throwable t) { + logger.error("unknown error", t); + } + } + }; + } } diff --git a/util/src/test/java/com/zfoo/util/EnumUtilsTest.java b/util/src/test/java/com/zfoo/util/EnumUtilsTest.java deleted file mode 100644 index fe44a7cd..00000000 --- a/util/src/test/java/com/zfoo/util/EnumUtilsTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and limitations under the License. - */ - -package com.zfoo.util; - -import org.junit.Assert; -import org.junit.Test; - -/** - * @author godotg - * @version 3.0 - */ - -enum Num { - ONE, - TWO, - THREE -} - -public class EnumUtilsTest { - - @Test - public void isInEnumsTest() { - Assert.assertTrue(EnumUtils.isInEnums("ONE", new Num[]{Num.ONE, Num.TWO, Num.THREE})); - } - -}