wangwei vor 5 Jahren
Ursprung
Commit
711c52049f

+ 3 - 3
src/main/java/cn/com/qmth/examcloud/ws/core/Messagehandler.java → src/main/java/cn/com/qmth/examcloud/ws/core/MessageHandler.java

@@ -13,10 +13,10 @@ import java.lang.annotation.Target;
  * @date 2019年3月15日
  * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  */
-@Target({ElementType.METHOD})
+@Target({ElementType.METHOD, ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
-public @interface Messagehandler {
+public @interface MessageHandler {
 
-	String value();
+	String value() default "";
 }

+ 90 - 0
src/main/java/cn/com/qmth/examcloud/ws/core/MessageHandlerHolder.java

@@ -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 "";
+		}
+	}
+
+}

+ 3 - 2
src/main/java/cn/com/qmth/examcloud/ws/handler/TestHandler.java

@@ -2,12 +2,13 @@ package cn.com.qmth.examcloud.ws.handler;
 
 import org.springframework.stereotype.Component;
 
-import cn.com.qmth.examcloud.ws.core.Messagehandler;
+import cn.com.qmth.examcloud.ws.core.MessageHandler;
 
 @Component
+@MessageHandler("test")
 public class TestHandler {
 
-	@Messagehandler("test")
+	@MessageHandler
 	public void test() {
 
 	}