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;
}
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);
}
Aggregations