Search in sources :

Example 1 with Message

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;
}
Also used : Message(com.terran4j.commons.reflux.Message) IOException(java.io.IOException)

Example 2 with Message

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

Message (com.terran4j.commons.reflux.Message)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 OnMessage (com.terran4j.commons.reflux.OnMessage)1 BusinessException (com.terran4j.commons.util.error.BusinessException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1