|
@@ -0,0 +1,90 @@
|
|
|
+package cn.com.qmth.examcloud.ws.core;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.boot.ApplicationArguments;
|
|
|
+import org.springframework.boot.ApplicationRunner;
|
|
|
+import org.springframework.core.annotation.AnnotationUtils;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 消息处理器 管理器
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2019年11月22日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Order(100)
|
|
|
+public class MessageHandlerHolder implements ApplicationRunner {
|
|
|
+
|
|
|
+ private static Map<String, Method> map = Maps.newConcurrentMap();
|
|
|
+
|
|
|
+ public static Method getMethod(String path) {
|
|
|
+ return map.get(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run(ApplicationArguments args) throws Exception {
|
|
|
+
|
|
|
+ Map<String, Object> map = SpringContextHolder.getApplicationContext()
|
|
|
+ .getBeansWithAnnotation(MessageHandler.class);
|
|
|
+
|
|
|
+ for (Object bean : map.values()) {
|
|
|
+ MessageHandler messageHandlerOfClass = AnnotationUtils.findAnnotation(bean.getClass(),
|
|
|
+ MessageHandler.class);
|
|
|
+ String valueOfClass = messageHandlerOfClass.value();
|
|
|
+
|
|
|
+ Method[] methods = bean.getClass().getDeclaredMethods();
|
|
|
+
|
|
|
+ for (Method method : methods) {
|
|
|
+ MessageHandler messageHandlerOfMethod = method.getAnnotation(MessageHandler.class);
|
|
|
+ if (null == messageHandlerOfMethod) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String valueOfMethod = messageHandlerOfMethod.value();
|
|
|
+
|
|
|
+ String path = join(valueOfClass, valueOfMethod);
|
|
|
+ map.put(path, method);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String join(String valueOfClass, String valueOfMethod) {
|
|
|
+ valueOfClass = valueOfClass.trim();
|
|
|
+ valueOfMethod = valueOfMethod.trim();
|
|
|
+
|
|
|
+ if (valueOfClass.startsWith("/")) {
|
|
|
+ valueOfClass = valueOfClass.substring(1);
|
|
|
+ }
|
|
|
+ if (valueOfClass.endsWith("/")) {
|
|
|
+ valueOfClass = valueOfClass.substring(0, valueOfClass.length() - 1);
|
|
|
+ }
|
|
|
+ if (valueOfMethod.startsWith("/")) {
|
|
|
+ valueOfMethod = valueOfMethod.substring(1);
|
|
|
+ }
|
|
|
+ if (valueOfMethod.endsWith("/")) {
|
|
|
+ valueOfMethod = valueOfMethod.substring(0, valueOfMethod.length() - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(valueOfClass) && StringUtils.isNotEmpty(valueOfMethod)) {
|
|
|
+ return valueOfClass + "/" + valueOfMethod;
|
|
|
+ } else if (StringUtils.isNotEmpty(valueOfClass)) {
|
|
|
+ return valueOfClass;
|
|
|
+ } else if (StringUtils.isNotEmpty(valueOfMethod)) {
|
|
|
+ return valueOfMethod;
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|