mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-10-10 06:06:41 +08:00
reload再次优化
This commit is contained in:
parent
9867d68526
commit
e59842c603
@ -1,12 +0,0 @@
|
|||||||
package net.lab1024.smartadmin.service.module.support.reload.core;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SmartReloadThreadLogger 日志类
|
|
||||||
*/
|
|
||||||
public interface SmartReloadLogger {
|
|
||||||
|
|
||||||
void error(String string);
|
|
||||||
|
|
||||||
void error(String string, Throwable e);
|
|
||||||
|
|
||||||
}
|
|
@ -1,9 +1,10 @@
|
|||||||
package net.lab1024.smartadmin.service.module.support.reload.core;
|
package net.lab1024.smartadmin.service.module.support.reload.core;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.anno.SmartReload;
|
import net.lab1024.smartadmin.service.module.support.reload.core.anno.SmartReload;
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadObject;
|
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadObject;
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.thread.SmartReloadScheduler;
|
import net.lab1024.smartadmin.service.module.support.reload.core.thread.SmartReloadRunnable;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
@ -11,6 +12,8 @@ import org.springframework.util.ReflectionUtils;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SmartReloadManager 管理器
|
* SmartReloadManager 管理器
|
||||||
@ -19,28 +22,25 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*
|
*
|
||||||
* @author zhuoda
|
* @author zhuoda
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class SmartReloadManager implements BeanPostProcessor {
|
public class SmartReloadManager implements BeanPostProcessor {
|
||||||
|
|
||||||
|
private static final String THREAD_NAME_PREFIX = "smart-admin-reload";
|
||||||
|
private static final int THREAD_COUNT = 1;
|
||||||
|
|
||||||
private Map<String, ReloadObject> reloadObjectMap = new ConcurrentHashMap<>();
|
private Map<String, ReloadObject> reloadObjectMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private SmartReloadScheduler reloadScheduler;
|
private ScheduledThreadPoolExecutor threadPoolExecutor;
|
||||||
|
|
||||||
private SmartReloadLogger logger;
|
public SmartReloadManager(AbstractSmartReloadCommand reloadCommand) {
|
||||||
|
this.threadPoolExecutor = new ScheduledThreadPoolExecutor(THREAD_COUNT, r -> {
|
||||||
|
Thread t = new Thread(r, THREAD_NAME_PREFIX);
|
||||||
public SmartReloadManager(SmartReloadLogger logger,
|
if (!t.isDaemon()) {
|
||||||
AbstractSmartReloadCommand reloadCommand,
|
t.setDaemon(true);
|
||||||
int threadCount) {
|
|
||||||
|
|
||||||
if (logger == null) {
|
|
||||||
throw new ExceptionInInitializerError("SmartReloadLoggerImp cannot be null");
|
|
||||||
}
|
}
|
||||||
if (threadCount < 1) {
|
return t;
|
||||||
throw new ExceptionInInitializerError("threadCount must be greater than 1");
|
});
|
||||||
}
|
this.threadPoolExecutor.scheduleWithFixedDelay(new SmartReloadRunnable(reloadCommand), 10, 20, TimeUnit.SECONDS);
|
||||||
this.logger = logger;
|
|
||||||
this.reloadScheduler = new SmartReloadScheduler(this.logger, threadCount);
|
|
||||||
this.reloadScheduler.addCommand(reloadCommand);
|
|
||||||
reloadCommand.setReloadManager(this);
|
reloadCommand.setReloadManager(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ public class SmartReloadManager implements BeanPostProcessor {
|
|||||||
}
|
}
|
||||||
int paramCount = method.getParameterCount();
|
int paramCount = method.getParameterCount();
|
||||||
if (paramCount > 1) {
|
if (paramCount > 1) {
|
||||||
logger.error("<<SmartReloadManager>> register tag reload : " + smartReload.value() + " , param count cannot greater than one !");
|
log.error("<<SmartReloadManager>> register tag reload : " + smartReload.value() + " , param count cannot greater than one !");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String reloadTag = smartReload.value();
|
String reloadTag = smartReload.value();
|
||||||
@ -75,13 +75,14 @@ public class SmartReloadManager implements BeanPostProcessor {
|
|||||||
*/
|
*/
|
||||||
private void register(String tag, ReloadObject reloadObject) {
|
private void register(String tag, ReloadObject reloadObject) {
|
||||||
if (reloadObjectMap.containsKey(tag)) {
|
if (reloadObjectMap.containsKey(tag)) {
|
||||||
logger.error("<<SmartReloadManager>> register duplicated tag reload : " + tag + " , and it will be cover!");
|
log.error("<<SmartReloadManager>> register duplicated tag reload : " + tag + " , and it will be cover!");
|
||||||
}
|
}
|
||||||
reloadObjectMap.put(tag, reloadObject);
|
reloadObjectMap.put(tag, reloadObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取重载对象
|
* 获取重载对象
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Map<String, ReloadObject> reloadObjectMap() {
|
public Map<String, ReloadObject> reloadObjectMap() {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package net.lab1024.smartadmin.service.module.support.reload.core.thread;
|
package net.lab1024.smartadmin.service.module.support.reload.core.thread;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.AbstractSmartReloadCommand;
|
import net.lab1024.smartadmin.service.module.support.reload.core.AbstractSmartReloadCommand;
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.SmartReloadLogger;
|
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadItem;
|
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadItem;
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadObject;
|
import net.lab1024.smartadmin.service.module.support.reload.core.domain.ReloadObject;
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.domain.SmartReloadResult;
|
import net.lab1024.smartadmin.service.module.support.reload.core.domain.SmartReloadResult;
|
||||||
@ -12,16 +12,20 @@ import java.lang.reflect.Method;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* [ reload thread ]
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
* @date
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
public class SmartReloadRunnable implements Runnable {
|
public class SmartReloadRunnable implements Runnable {
|
||||||
|
|
||||||
private AbstractSmartReloadCommand abstractSmartReloadCommand;
|
private AbstractSmartReloadCommand abstractSmartReloadCommand;
|
||||||
|
|
||||||
private SmartReloadLogger logger;
|
public SmartReloadRunnable(AbstractSmartReloadCommand abstractSmartReloadCommand) {
|
||||||
|
|
||||||
public SmartReloadRunnable(AbstractSmartReloadCommand abstractSmartReloadCommand, SmartReloadLogger logger) {
|
|
||||||
this.abstractSmartReloadCommand = abstractSmartReloadCommand;
|
this.abstractSmartReloadCommand = abstractSmartReloadCommand;
|
||||||
this.logger = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -29,7 +33,7 @@ public class SmartReloadRunnable implements Runnable {
|
|||||||
try {
|
try {
|
||||||
this.doTask();
|
this.doTask();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
logger.error("", e);
|
log.error("", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,6 +59,7 @@ public class SmartReloadRunnable implements Runnable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 方法调用
|
* 方法调用
|
||||||
|
*
|
||||||
* @param reloadItem
|
* @param reloadItem
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
package net.lab1024.smartadmin.service.module.support.reload.core.thread;
|
|
||||||
|
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.AbstractSmartReloadCommand;
|
|
||||||
import net.lab1024.smartadmin.service.module.support.reload.core.SmartReloadLogger;
|
|
||||||
|
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reload 调度器
|
|
||||||
*
|
|
||||||
* @author zhuoda
|
|
||||||
*/
|
|
||||||
public class SmartReloadScheduler {
|
|
||||||
|
|
||||||
private ScheduledThreadPoolExecutor executor;
|
|
||||||
|
|
||||||
private SmartReloadLogger logger;
|
|
||||||
|
|
||||||
|
|
||||||
public SmartReloadScheduler(SmartReloadLogger logger, int threadCount) {
|
|
||||||
this.executor = new ScheduledThreadPoolExecutor(threadCount, new SmartReloadThreadFactory());
|
|
||||||
this.logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void shutdown() {
|
|
||||||
try {
|
|
||||||
executor.shutdown();
|
|
||||||
} catch (Throwable e) {
|
|
||||||
logger.error("<<SmartReloadScheduler>> shutdown ", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addCommand(AbstractSmartReloadCommand command, long initialDelay, long delay, TimeUnit unit) {
|
|
||||||
executor.scheduleWithFixedDelay(new SmartReloadRunnable(command, this.logger), initialDelay, delay, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addCommand(AbstractSmartReloadCommand command) {
|
|
||||||
executor.scheduleWithFixedDelay(new SmartReloadRunnable(command, this.logger), 10, 20, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package net.lab1024.smartadmin.service.module.support.reload.core.thread;
|
|
||||||
|
|
||||||
import java.util.concurrent.ThreadFactory;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
public class SmartReloadThreadFactory implements ThreadFactory {
|
|
||||||
|
|
||||||
private static final AtomicInteger poolNumber = new AtomicInteger(1);
|
|
||||||
|
|
||||||
private final ThreadGroup group;
|
|
||||||
|
|
||||||
private final AtomicInteger threadNumber = new AtomicInteger(1);
|
|
||||||
|
|
||||||
private final String namePrefix;
|
|
||||||
|
|
||||||
SmartReloadThreadFactory() {
|
|
||||||
SecurityManager s = System.getSecurityManager();
|
|
||||||
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
|
|
||||||
namePrefix = "smart-reload-" + poolNumber.getAndIncrement() + "-thread-";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Thread newThread(Runnable r) {
|
|
||||||
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
|
|
||||||
if (t.isDaemon()) {
|
|
||||||
t.setDaemon(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.getPriority() != Thread.NORM_PRIORITY) {
|
|
||||||
t.setPriority(Thread.NORM_PRIORITY);
|
|
||||||
}
|
|
||||||
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user