feat: prevent worker from being repeatedly initialized

This commit is contained in:
tjq 2021-03-11 23:28:33 +08:00
parent 48860e3172
commit 4c89a1e69c

View File

@ -43,6 +43,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* 客户端启动类 * 客户端启动类
@ -55,6 +56,7 @@ public class OhMyWorker implements ApplicationContextAware, InitializingBean, Di
private ScheduledExecutorService timingPool; private ScheduledExecutorService timingPool;
private final WorkerRuntime workerRuntime = new WorkerRuntime(); private final WorkerRuntime workerRuntime = new WorkerRuntime();
private final AtomicBoolean initialized = new AtomicBoolean();
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
@ -68,6 +70,11 @@ public class OhMyWorker implements ApplicationContextAware, InitializingBean, Di
public void init() throws Exception { public void init() throws Exception {
if (!initialized.compareAndSet(false, true)) {
log.warn("[OhMyWorker] please do not repeat the initialization");
return;
}
Stopwatch stopwatch = Stopwatch.createStarted(); Stopwatch stopwatch = Stopwatch.createStarted();
log.info("[OhMyWorker] start to initialize OhMyWorker..."); log.info("[OhMyWorker] start to initialize OhMyWorker...");