|
|
|
@@ -2,32 +2,32 @@ package com.ruoyi.common.utils.handler; |
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
import cn.hutool.core.lang.Assert; |
|
|
|
import com.google.common.eventbus.EventBus; |
|
|
|
import com.ruoyi.common.utils.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Qualifier; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import javax.annotation.PreDestroy; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
@Component |
|
|
|
public final class HandlerEngine |
|
|
|
{ |
|
|
|
private static final Logger logger = LoggerFactory.getLogger("HandlerEngine" ); |
|
|
|
private static final List<Object> _listenerList = new ArrayList<>(); |
|
|
|
private static final Map<Integer, Handler> _handlerMap = new HashMap<>(); |
|
|
|
private static final Logger logger = LoggerFactory.getLogger("HandlerEngine"); |
|
|
|
private static final List<Object> _listenerList = new ArrayList<>(); |
|
|
|
private static final Map<String, Handler> _handlerMap = new HashMap<>(); |
|
|
|
|
|
|
|
public static Object Post(Object...message) |
|
|
|
{ |
|
|
|
Handler handler = _handlerMap.values().stream().filter((x) -> null == x.Match(message)).findFirst().orElse(null); |
|
|
|
Handler handler = _handlerMap.values().stream().filter((x) -> { |
|
|
|
String err = x.Match(message); |
|
|
|
//System.err.println(err); |
|
|
|
return null == err; |
|
|
|
}).findFirst().orElse(null); |
|
|
|
Assert.notNull(handler, "没有对应处理器被注册"); |
|
|
|
return handler.Invoke(message); |
|
|
|
} |
|
|
|
@@ -35,10 +35,10 @@ public final class HandlerEngine |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public static <T> T PostT(Object...message) |
|
|
|
{ |
|
|
|
return (T)Post(message); |
|
|
|
return (T) Post(message); |
|
|
|
} |
|
|
|
|
|
|
|
public static Object Run(int protocol, Object...message) |
|
|
|
public static Object Call(String protocol, Object...message) |
|
|
|
{ |
|
|
|
Handler handler = _handlerMap.get(protocol); |
|
|
|
Assert.notNull(handler, "协议对应的处理器不存在: {}", protocol); |
|
|
|
@@ -46,16 +46,17 @@ public final class HandlerEngine |
|
|
|
} |
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public static <T> T RunT(int protocol, Object...message) |
|
|
|
public static <T> T CallT(String protocol, Object...message) |
|
|
|
{ |
|
|
|
return (T)Run(protocol, message); |
|
|
|
return (T) Call(protocol, message); |
|
|
|
} |
|
|
|
|
|
|
|
public static void Register(Object listener) |
|
|
|
{ |
|
|
|
if(null == listener) |
|
|
|
return; |
|
|
|
synchronized(_listenerList) { |
|
|
|
synchronized(_listenerList) |
|
|
|
{ |
|
|
|
if(_listenerList.contains(listener)) |
|
|
|
return; |
|
|
|
List<Handler> handlers = parseObject(listener); |
|
|
|
@@ -77,10 +78,11 @@ public final class HandlerEngine |
|
|
|
{ |
|
|
|
if(null == listener) |
|
|
|
return; |
|
|
|
synchronized(_listenerList) { |
|
|
|
synchronized(_listenerList) |
|
|
|
{ |
|
|
|
if(!_listenerList.contains(listener)) |
|
|
|
return; |
|
|
|
List<Integer> rm = new ArrayList<>(); |
|
|
|
List<String> rm = new ArrayList<>(); |
|
|
|
_handlerMap.forEach((k, v) -> { |
|
|
|
if(v.handler == listener) |
|
|
|
{ |
|
|
|
@@ -97,7 +99,8 @@ public final class HandlerEngine |
|
|
|
@PreDestroy |
|
|
|
public static void UnregisterAll() |
|
|
|
{ |
|
|
|
synchronized(_listenerList) { |
|
|
|
synchronized(_listenerList) |
|
|
|
{ |
|
|
|
logger.info("HandlerEngine::UnregisterAll -> " + _listenerList.size()); |
|
|
|
_handlerMap.clear(); |
|
|
|
_listenerList.clear(); |
|
|
|
@@ -123,7 +126,9 @@ public final class HandlerEngine |
|
|
|
CallbackHandler annotation = method.getAnnotation(CallbackHandler.class); |
|
|
|
if(null == annotation) |
|
|
|
continue; |
|
|
|
int protocol = annotation.protocol(); |
|
|
|
String protocol = annotation.protocol(); |
|
|
|
if(StringUtils.isEmpty(protocol)) |
|
|
|
protocol = GenProtocol(); |
|
|
|
Handler handler = new Handler(protocol, obj, method); |
|
|
|
list.add(handler); |
|
|
|
} |
|
|
|
@@ -136,4 +141,10 @@ public final class HandlerEngine |
|
|
|
} |
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
private static int _autoProtocolId = 0; |
|
|
|
private static String GenProtocol() |
|
|
|
{ |
|
|
|
return "__AUTO_GEN_PROTOCOL_" + _autoProtocolId++; |
|
|
|
} |
|
|
|
} |