重写Sncp.getServiceType

This commit is contained in:
redkale
2023-01-29 22:22:12 +08:00
parent f41a1bc3a1
commit ae27fffdb0

View File

@@ -1,102 +1,102 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
* and open the template in the editor. * and open the template in the editor.
*/ */
package org.redkale.service; package org.redkale.service;
import java.util.concurrent.*; import java.util.concurrent.*;
import org.redkale.annotation.Resource; import org.redkale.annotation.Resource;
import org.redkale.boot.Application; import org.redkale.boot.Application;
import org.redkale.net.WorkThread; import org.redkale.net.WorkThread;
import org.redkale.net.sncp.Sncp; import org.redkale.net.sncp.Sncp;
import org.redkale.util.ThreadHashExecutor; import org.redkale.util.ThreadHashExecutor;
/** /**
* *
* @author zhangjx * @author zhangjx
*/ */
public abstract class AbstractService implements Service { public abstract class AbstractService implements Service {
//配置<executor threads="0"> APP_EXECUTOR资源为null //配置<executor threads="0"> APP_EXECUTOR资源为null
@Resource(name = Application.RESNAME_APP_EXECUTOR, required = false) @Resource(name = Application.RESNAME_APP_EXECUTOR, required = false)
private ExecutorService workExecutor; private ExecutorService workExecutor;
/** /**
* 当前Service类的原始Service类型 由于Service会动态重载所以getClass()得到的不是原始Service类型 * 当前Service类的原始Service类型 由于Service会动态重载所以getClass()得到的不是原始Service类型
* *
* @return Class * @return Class
*/ */
protected Class serviceType() { protected Class serviceType() {
return Sncp.getResourceType(this); return Sncp.getServiceType(this);
} }
/** /**
* 异步执行任务 * 异步执行任务
* *
* *
* @param command 任务 * @param command 任务
*/ */
protected void runAsync(Runnable command) { protected void runAsync(Runnable command) {
ExecutorService executor = this.workExecutor; ExecutorService executor = this.workExecutor;
if (executor != null) { if (executor != null) {
executor.execute(command); executor.execute(command);
} else { } else {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
if (thread instanceof WorkThread) { if (thread instanceof WorkThread) {
((WorkThread) thread).runAsync(command); ((WorkThread) thread).runAsync(command);
} else { } else {
ForkJoinPool.commonPool().execute(command); ForkJoinPool.commonPool().execute(command);
} }
} }
} }
/** /**
* 异步执行任务 * 异步执行任务
* *
* @param hash hash值 * @param hash hash值
* @param command 任务 * @param command 任务
*/ */
protected void runAsync(int hash, Runnable command) { protected void runAsync(int hash, Runnable command) {
ExecutorService executor = this.workExecutor; ExecutorService executor = this.workExecutor;
if (executor != null) { if (executor != null) {
if (executor instanceof ThreadHashExecutor) { if (executor instanceof ThreadHashExecutor) {
((ThreadHashExecutor) executor).execute(hash, command); ((ThreadHashExecutor) executor).execute(hash, command);
} else { } else {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
if (thread instanceof WorkThread) { if (thread instanceof WorkThread) {
((WorkThread) thread).runAsync(hash, command); ((WorkThread) thread).runAsync(hash, command);
} else { } else {
executor.execute(command); executor.execute(command);
} }
} }
} else { } else {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
if (thread instanceof WorkThread) { if (thread instanceof WorkThread) {
((WorkThread) thread).runAsync(hash, command); ((WorkThread) thread).runAsync(hash, command);
} else { } else {
ForkJoinPool.commonPool().execute(command); ForkJoinPool.commonPool().execute(command);
} }
} }
} }
/** /**
* 获取线程池 * 获取线程池
* *
* @return ExecutorService * @return ExecutorService
*/ */
protected ExecutorService getExecutor() { protected ExecutorService getExecutor() {
ExecutorService executor = this.workExecutor; ExecutorService executor = this.workExecutor;
if (executor != null) { if (executor != null) {
return executor; return executor;
} }
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
if (thread instanceof WorkThread) { if (thread instanceof WorkThread) {
ExecutorService e = ((WorkThread) thread).getWorkExecutor(); ExecutorService e = ((WorkThread) thread).getWorkExecutor();
if (e != null) { if (e != null) {
return e; return e;
} }
} }
return ForkJoinPool.commonPool(); return ForkJoinPool.commonPool();
} }
} }