Search in sources :

Example 1 with OnMessage

use of com.terran4j.commons.reflux.OnMessage in project commons by terran4j.

the class MessageHandler method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Class<?> targetClass = Classes.getTargetClass(bean);
    Method[] methods = Classes.getMethods(OnMessage.class, targetClass);
    if (methods != null && methods.length > 0) {
        for (Method method : methods) {
            OnMessage onMessage = method.getAnnotation(OnMessage.class);
            Class<?>[] paramTypes = method.getParameterTypes();
            if (paramTypes == null || paramTypes.length != 1) {
                throw new BeanDefinitionValidationException("@OnMessage修饰的方法,必须有且仅有一个参数");
            }
            Class<?> paramType = paramTypes[0];
            String msgType = onMessage.type();
            if (StringUtils.isEmpty(msgType)) {
                msgType = paramType.getName();
            }
            Class<?> returnType = method.getReturnType();
            if (returnType != null && !("void".equals(returnType.getName()) || String.class.equals(returnType))) {
                throw new BeanDefinitionValidationException("@OnMessage修饰的方法,返回类型只能是 String 或 void 。");
            }
            String key = msgType;
            OnMessageInvoker invoker = new OnMessageInvoker();
            invoker.method = method;
            invoker.bean = bean;
            invoker.paramType = paramType;
            invokers.put(key, invoker);
        }
    }
    return bean;
}
Also used : BeanDefinitionValidationException(org.springframework.beans.factory.support.BeanDefinitionValidationException) Method(java.lang.reflect.Method) OnMessage(com.terran4j.commons.reflux.OnMessage)

Example 2 with OnMessage

use of com.terran4j.commons.reflux.OnMessage in project commons by terran4j.

the class MessageHandler method onMessage.

public String onMessage(String message) throws BusinessException {
    JsonElement element = jsonParser.parse(message);
    JsonObject json = element.getAsJsonObject();
    Message result = new Message();
    String msgId = json.get("id").getAsString();
    result.setId(msgId);
    String msgType = json.get("type").getAsString();
    result.setType(msgType);
    OnMessageInvoker invoker = invokers.get(msgType);
    if (invoker == null) {
        throw // 
        new BusinessException(ErrorCodes.RESOURCE_NOT_FOUND).put("msgType", msgType);
    }
    String msgContent = json.get("content").toString();
    Object param = gson.fromJson(msgContent, invoker.paramType);
    Object reply = null;
    try {
        reply = invoker.method.invoke(invoker.bean, param);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw // 
        new BusinessException(CommonErrorCode.INTERNAL_ERROR, e).put("msgType", msgType).put("msgContent", msgContent);
    }
    if (reply == null) {
        return null;
    }
    String content = reply.toString();
    result.setContent(content);
    result.setStatus(0);
    return gson.toJson(result);
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) Message(com.terran4j.commons.reflux.Message) OnMessage(com.terran4j.commons.reflux.OnMessage) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

OnMessage (com.terran4j.commons.reflux.OnMessage)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Message (com.terran4j.commons.reflux.Message)1 BusinessException (com.terran4j.commons.util.error.BusinessException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 BeanDefinitionValidationException (org.springframework.beans.factory.support.BeanDefinitionValidationException)1