@@ -1,8 +1,6 @@ | |||
package com.ruoyi.agentcenter.listener; | |||
import cn.hutool.core.lang.Assert; | |||
import com.alibaba.fastjson2.JSONObject; | |||
import com.google.common.eventbus.Subscribe; | |||
import com.nsgk.agentcentersdk.core.NSProtocol; | |||
import com.nsgk.agentcentersdk.entity.NSContractionEntity; | |||
import com.ruoyi.agentcenter.domain.TAgentContraction; | |||
@@ -13,7 +11,6 @@ import com.ruoyi.agentcenter.object.Result; | |||
import com.ruoyi.agentcenter.service.ITAgentContractionService; | |||
import com.ruoyi.agentcenter.service.ITAgentTaskService; | |||
import com.ruoyi.common.core.domain.entity.SysDept; | |||
import com.ruoyi.common.utils.EventBusEngine; | |||
import com.ruoyi.common.utils.handler.CallbackHandler; | |||
import com.ruoyi.common.utils.handler.HandlerEngine; | |||
import com.ruoyi.common.utils.sql.SqlUtil; | |||
@@ -40,7 +37,7 @@ public class ContractionListener | |||
HandlerEngine.Register(this); | |||
} | |||
@CallbackHandler(protocol = NSProtocol.NS_PROTOCOL_CONTRACTION) | |||
@CallbackHandler(protocol = "" + NSProtocol.NS_PROTOCOL_CONTRACTION) | |||
public void handle(ContractionSession session) | |||
{ | |||
TAgentContraction contraction = conv(session.message); | |||
@@ -20,6 +20,7 @@ import com.ruoyi.common.utils.handler.HandlerEngine; | |||
import org.springframework.stereotype.Service; | |||
import javax.servlet.http.HttpServletRequest; | |||
import java.util.Date; | |||
@Service | |||
public class AgentCenterImpl implements IAgentCenter | |||
@@ -19,5 +19,5 @@ public @interface CallbackHandler | |||
/** | |||
* 协议 | |||
*/ | |||
public int protocol(); | |||
public String protocol() default ""; | |||
} |
@@ -1,25 +1,25 @@ | |||
package com.ruoyi.common.utils.handler; | |||
import cn.hutool.core.lang.Assert; | |||
import cn.hutool.core.util.StrUtil; | |||
import com.ruoyi.common.utils.spring.SpringUtils; | |||
import java.lang.reflect.InvocationTargetException; | |||
import java.lang.reflect.Method; | |||
final class Handler | |||
{ | |||
public final int protocol; | |||
public final String protocol; | |||
public final Object handler; | |||
public final Method method; | |||
public Handler(int protocol, Object handler, Method method) | |||
public Handler(String protocol, Object handler, Method method) | |||
{ | |||
this.protocol = protocol; | |||
this.handler = handler; | |||
this.method = method; | |||
} | |||
public Handler(int protocol, Object handler, String methodName) | |||
public Handler(String protocol, Object handler, String methodName) | |||
{ | |||
this.protocol = protocol; | |||
this.handler = handler; | |||
@@ -27,8 +27,6 @@ final class Handler | |||
{ | |||
method = handler.getClass().getDeclaredMethod(methodName); | |||
Class<?>[] parameterTypes = method.getParameterTypes(); | |||
if(parameterTypes.length < 1) | |||
throw new RuntimeException("处理器函数参数至少有一个参数"); | |||
} | |||
catch(Exception e) | |||
{ | |||
@@ -36,7 +34,7 @@ final class Handler | |||
} | |||
} | |||
public Handler(int protocol, String beanName, String methodName) | |||
public Handler(String protocol, String beanName, String methodName) | |||
{ | |||
this.protocol = protocol; | |||
this.handler = SpringUtils.getBean(beanName); | |||
@@ -44,8 +42,6 @@ final class Handler | |||
{ | |||
method = handler.getClass().getDeclaredMethod(methodName); | |||
Class<?>[] parameterTypes = method.getParameterTypes(); | |||
if(parameterTypes.length < 1) | |||
throw new RuntimeException("处理器函数参数至少有一个参数"); | |||
} | |||
catch(Exception e) | |||
{ | |||
@@ -53,7 +49,7 @@ final class Handler | |||
} | |||
} | |||
public Handler(int protocol, Class<?> beanClazz, String methodName) | |||
public Handler(String protocol, Class<?> beanClazz, String methodName) | |||
{ | |||
this.protocol = protocol; | |||
this.handler = SpringUtils.getBean(beanClazz); | |||
@@ -77,6 +73,11 @@ final class Handler | |||
{ | |||
return method.invoke(handler, args); | |||
} | |||
catch(InvocationTargetException e) | |||
{ | |||
e.printStackTrace(); | |||
throw new RuntimeException(e.getTargetException()); | |||
} | |||
catch(Exception e) | |||
{ | |||
e.printStackTrace(); | |||
@@ -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++; | |||
} | |||
} |