From fea1974014926faa5d740ceefc6cacbc5abde4c0 Mon Sep 17 00:00:00 2001 From: tjq Date: Sun, 11 Aug 2024 11:32:28 +0800 Subject: [PATCH] feat: worker use random server address #953 --- .../powerjob/common/utils/CommonUtils.java | 11 ++++++ .../tech/powerjob/common/utils/MapUtils.java | 8 ++++ .../common/utils/CommonUtilsTest.java | 39 +++++++++++++++++++ .../tech/powerjob/worker/PowerJobWorker.java | 8 +++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 powerjob-common/src/test/java/tech/powerjob/common/utils/CommonUtilsTest.java diff --git a/powerjob-common/src/main/java/tech/powerjob/common/utils/CommonUtils.java b/powerjob-common/src/main/java/tech/powerjob/common/utils/CommonUtils.java index 60507c6d..d787f337 100644 --- a/powerjob-common/src/main/java/tech/powerjob/common/utils/CommonUtils.java +++ b/powerjob-common/src/main/java/tech/powerjob/common/utils/CommonUtils.java @@ -8,6 +8,7 @@ import org.apache.commons.lang3.time.DateFormatUtils; import java.util.Collection; import java.util.Date; +import java.util.Map; import java.util.UUID; import java.util.function.Supplier; @@ -129,6 +130,16 @@ public class CommonUtils { throw new PowerJobException(msg); } } + if (obj instanceof Collection) { + if (CollectionUtils.isEmpty((Collection) obj)) { + throw new PowerJobException(msg); + } + } + if (obj instanceof Map) { + if (MapUtils.isEmpty((Map) obj)) { + throw new PowerJobException(msg); + } + } return obj; } diff --git a/powerjob-common/src/main/java/tech/powerjob/common/utils/MapUtils.java b/powerjob-common/src/main/java/tech/powerjob/common/utils/MapUtils.java index 2b02bc65..dd57bfcd 100644 --- a/powerjob-common/src/main/java/tech/powerjob/common/utils/MapUtils.java +++ b/powerjob-common/src/main/java/tech/powerjob/common/utils/MapUtils.java @@ -75,4 +75,12 @@ public class MapUtils { return null; } + + public static boolean isEmpty(Map map) { + return map == null || map.isEmpty(); + } + + public static boolean isNotEmpty(Map map) { + return !isEmpty(map); + } } diff --git a/powerjob-common/src/test/java/tech/powerjob/common/utils/CommonUtilsTest.java b/powerjob-common/src/test/java/tech/powerjob/common/utils/CommonUtilsTest.java new file mode 100644 index 00000000..26453dd4 --- /dev/null +++ b/powerjob-common/src/test/java/tech/powerjob/common/utils/CommonUtilsTest.java @@ -0,0 +1,39 @@ +package tech.powerjob.common.utils; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.junit.jupiter.api.Test; +import tech.powerjob.common.exception.PowerJobException; + +import java.util.Collections; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * CommonUtilsTest + * + * @author tjq + * @since 2024/8/11 + */ +class CommonUtilsTest { + + @Test + void testRequireNonNull() { + + assertThrowsExactly(PowerJobException.class, () -> CommonUtils.requireNonNull(null, "NULL_OBJ")); + assertThrowsExactly(PowerJobException.class, () -> CommonUtils.requireNonNull("", "EMPTY_STR")); + assertThrowsExactly(PowerJobException.class, () -> CommonUtils.requireNonNull(Lists.newArrayList(), "EMPTY_COLLECTION")); + assertThrowsExactly(PowerJobException.class, () -> CommonUtils.requireNonNull(Collections.emptyMap(), "EMPTY_MAP")); + + Map map = Maps.newHashMap(); + map.put("1", 1); + + CommonUtils.requireNonNull(1, "NORMAL"); + CommonUtils.requireNonNull("1", "NORMAL"); + CommonUtils.requireNonNull(Lists.newArrayList("1"), "NORMAL"); + CommonUtils.requireNonNull(map, "NORMAL"); + + } + +} \ No newline at end of file diff --git a/powerjob-worker/src/main/java/tech/powerjob/worker/PowerJobWorker.java b/powerjob-worker/src/main/java/tech/powerjob/worker/PowerJobWorker.java index 7d6e0e61..faa0ed34 100644 --- a/powerjob-worker/src/main/java/tech/powerjob/worker/PowerJobWorker.java +++ b/powerjob-worker/src/main/java/tech/powerjob/worker/PowerJobWorker.java @@ -55,7 +55,7 @@ public class PowerJobWorker { public PowerJobWorker(PowerJobWorkerConfig config) { this.workerRuntime = new WorkerRuntime(); this.remoteEngine = new PowerJobRemoteEngine(); - workerRuntime.setWorkerConfig(config); + workerRuntime.setWorkerConfig(reConfig(config)); } public void init() throws Exception { @@ -147,6 +147,12 @@ public class PowerJobWorker { } } + private PowerJobWorkerConfig reConfig(PowerJobWorkerConfig config) { + CommonUtils.requireNonNull(config.getServerAddress(), "ServerAddress can't be null or empty!"); + Collections.shuffle(config.getServerAddress()); + return config; + } + private ProcessorLoader buildProcessorLoader(WorkerRuntime runtime) { List customPF = Optional.ofNullable(runtime.getWorkerConfig().getProcessorFactoryList()).orElse(Collections.emptyList()); List finalPF = Lists.newArrayList(customPF);