mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: worker use random server address #953
This commit is contained in:
parent
4527454a7c
commit
fea1974014
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<String, Object> 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");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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<ProcessorFactory> customPF = Optional.ofNullable(runtime.getWorkerConfig().getProcessorFactoryList()).orElse(Collections.emptyList());
|
||||
List<ProcessorFactory> finalPF = Lists.newArrayList(customPF);
|
||||
|
Loading…
x
Reference in New Issue
Block a user