From ae27fffdb0c79548777572f69e36a8a68ade0253 Mon Sep 17 00:00:00 2001 From: redkale Date: Sun, 29 Jan 2023 22:22:12 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99Sncp.getServiceType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/redkale/service/AbstractService.java | 204 +++++++++--------- 1 file changed, 102 insertions(+), 102 deletions(-) diff --git a/src/main/java/org/redkale/service/AbstractService.java b/src/main/java/org/redkale/service/AbstractService.java index fc09c12b0..e055d95f6 100644 --- a/src/main/java/org/redkale/service/AbstractService.java +++ b/src/main/java/org/redkale/service/AbstractService.java @@ -1,102 +1,102 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkale.service; - -import java.util.concurrent.*; -import org.redkale.annotation.Resource; -import org.redkale.boot.Application; -import org.redkale.net.WorkThread; -import org.redkale.net.sncp.Sncp; -import org.redkale.util.ThreadHashExecutor; - -/** - * - * @author zhangjx - */ -public abstract class AbstractService implements Service { - - //配置 APP_EXECUTOR资源为null - @Resource(name = Application.RESNAME_APP_EXECUTOR, required = false) - private ExecutorService workExecutor; - - /** - * 当前Service类的原始Service类型, 由于Service会动态重载,所以getClass()得到的不是原始Service类型 - * - * @return Class - */ - protected Class serviceType() { - return Sncp.getResourceType(this); - } - - /** - * 异步执行任务 - * - * - * @param command 任务 - */ - protected void runAsync(Runnable command) { - ExecutorService executor = this.workExecutor; - if (executor != null) { - executor.execute(command); - } else { - Thread thread = Thread.currentThread(); - if (thread instanceof WorkThread) { - ((WorkThread) thread).runAsync(command); - } else { - ForkJoinPool.commonPool().execute(command); - } - } - } - - /** - * 异步执行任务 - * - * @param hash hash值 - * @param command 任务 - */ - protected void runAsync(int hash, Runnable command) { - ExecutorService executor = this.workExecutor; - if (executor != null) { - if (executor instanceof ThreadHashExecutor) { - ((ThreadHashExecutor) executor).execute(hash, command); - } else { - Thread thread = Thread.currentThread(); - if (thread instanceof WorkThread) { - ((WorkThread) thread).runAsync(hash, command); - } else { - executor.execute(command); - } - } - } else { - Thread thread = Thread.currentThread(); - if (thread instanceof WorkThread) { - ((WorkThread) thread).runAsync(hash, command); - } else { - ForkJoinPool.commonPool().execute(command); - } - } - } - - /** - * 获取线程池 - * - * @return ExecutorService - */ - protected ExecutorService getExecutor() { - ExecutorService executor = this.workExecutor; - if (executor != null) { - return executor; - } - Thread thread = Thread.currentThread(); - if (thread instanceof WorkThread) { - ExecutorService e = ((WorkThread) thread).getWorkExecutor(); - if (e != null) { - return e; - } - } - return ForkJoinPool.commonPool(); - } -} +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.service; + +import java.util.concurrent.*; +import org.redkale.annotation.Resource; +import org.redkale.boot.Application; +import org.redkale.net.WorkThread; +import org.redkale.net.sncp.Sncp; +import org.redkale.util.ThreadHashExecutor; + +/** + * + * @author zhangjx + */ +public abstract class AbstractService implements Service { + + //配置 APP_EXECUTOR资源为null + @Resource(name = Application.RESNAME_APP_EXECUTOR, required = false) + private ExecutorService workExecutor; + + /** + * 当前Service类的原始Service类型, 由于Service会动态重载,所以getClass()得到的不是原始Service类型 + * + * @return Class + */ + protected Class serviceType() { + return Sncp.getServiceType(this); + } + + /** + * 异步执行任务 + * + * + * @param command 任务 + */ + protected void runAsync(Runnable command) { + ExecutorService executor = this.workExecutor; + if (executor != null) { + executor.execute(command); + } else { + Thread thread = Thread.currentThread(); + if (thread instanceof WorkThread) { + ((WorkThread) thread).runAsync(command); + } else { + ForkJoinPool.commonPool().execute(command); + } + } + } + + /** + * 异步执行任务 + * + * @param hash hash值 + * @param command 任务 + */ + protected void runAsync(int hash, Runnable command) { + ExecutorService executor = this.workExecutor; + if (executor != null) { + if (executor instanceof ThreadHashExecutor) { + ((ThreadHashExecutor) executor).execute(hash, command); + } else { + Thread thread = Thread.currentThread(); + if (thread instanceof WorkThread) { + ((WorkThread) thread).runAsync(hash, command); + } else { + executor.execute(command); + } + } + } else { + Thread thread = Thread.currentThread(); + if (thread instanceof WorkThread) { + ((WorkThread) thread).runAsync(hash, command); + } else { + ForkJoinPool.commonPool().execute(command); + } + } + } + + /** + * 获取线程池 + * + * @return ExecutorService + */ + protected ExecutorService getExecutor() { + ExecutorService executor = this.workExecutor; + if (executor != null) { + return executor; + } + Thread thread = Thread.currentThread(); + if (thread instanceof WorkThread) { + ExecutorService e = ((WorkThread) thread).getWorkExecutor(); + if (e != null) { + return e; + } + } + return ForkJoinPool.commonPool(); + } +}