浏览代码

异步业务执行器放入基类,方便调用

johnclot69 4 月之前
父节点
当前提交
e5241e7031

+ 27 - 1
incubator-common/src/main/java/com/incubator/common/game/AbstractService.java

@@ -6,16 +6,20 @@ import org.slf4j.Logger;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executor;
 
 
 public abstract class AbstractService implements IService {
 
-	protected final Logger logger = Log4jUtil.getLogger(getClass());
+	protected final static Logger logger = Log4jUtil.getLogger(AbstractService.class);
 
 	private final Map<String, Object> attributes = new HashMap<>();
 
 	private ServiceState state = ServiceState.STOPED;
 
+	/** 异步业务执行器 */
+	protected static Executor asyncExec;
+
 	/** 非后台进程防止jvm退出 **/
 	private final ThreadHolder threadHolder = new ThreadHolder();
 
@@ -108,6 +112,28 @@ public abstract class AbstractService implements IService {
 		this.state = state;
 	}
 
+	public static void asyncExec(final Runnable command) {
+		if (command == null) {
+			logger.error("asyncExec command is null!");
+			return;
+		}
+		asyncExec.execute(new Runnable() {
+			@Override
+			public void run() {
+				try {
+					long lStartTime = System.currentTimeMillis();
+					command.run();
+					long lTimeUse = System.currentTimeMillis() - lStartTime;
+					if (lTimeUse > 1000) {
+						logger.info("asyncExec时间过长:{}, {}", lTimeUse, command);
+					}
+				} catch (Exception e) {
+					logger.error("GGame asyncExec catch- ", e);
+				}
+			}
+		});
+	}
+
 	/**
 	 * 增加一个关闭钩子
 	 * 

+ 0 - 26
incubator-game/src/main/java/com/incubator/game/GGame.java

@@ -34,7 +34,6 @@ import org.slf4j.Logger;
 
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.Executor;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
@@ -98,9 +97,6 @@ public class GGame extends AbstractService {
     /** 本服所有在线玩家 */
     public static Map<String, GPlayer> onlinePlayers = new ConcurrentHashMap<>();
 
-    /** 异步业务执行器 */
-    protected Executor asyncExec;
-
     public static GlobalInfoPO globalInfoPO = new GlobalInfoPO();
 
     private static volatile GGame instance;
@@ -126,28 +122,6 @@ public class GGame extends AbstractService {
         return lsDBHandler;
     }
 
-    public void asyncExec(final Runnable command) {
-        if (command == null) {
-            logger.error("asyncExec command is null!");
-            return;
-        }
-        asyncExec.execute(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    long lStartTime = System.currentTimeMillis();
-                    command.run();
-                    long lTimeUse = System.currentTimeMillis() - lStartTime;
-                    if (lTimeUse > 1000) {
-                        logger.info("asyncExec时间过长:{}, {}", lTimeUse, command);
-                    }
-                } catch (Exception e) {
-                    logger.error("GGame asyncExec catch- ", e);
-                }
-            }
-        });
-    }
-
     @Override
     protected void doStart() throws Throwable {
         // 1.初始化Properties配置