use of com.terran4j.commons.reflux.Message in project commons by terran4j.
the class RefluxServerImpl method sendContent.
final <T> boolean sendContent(T content, ClientConnectionInfo info) {
if (info == null) {
return false;
}
RefluxServerEndpoint conn = info.getConnection();
if (conn == null) {
return false;
}
Message message = new Message();
message.setId(Message.generateId());
message.setStatus(Message.STATUS_REQUEST);
String type = Classes.getTargetClass(content).getName();
message.setType(type);
message.setContent(content);
String messageText = gson.toJson(message);
if (log.isInfoEnabled()) {
log.info("send message to client: {}", conn.getClientId());
}
try {
if (log.isInfoEnabled()) {
log.info("sending message: {}", messageText);
}
conn.sendMessage(messageText);
return true;
} catch (IOException e) {
e.printStackTrace();
// TODO: 处理异常。
}
return false;
}
use of com.terran4j.commons.reflux.Message 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