All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hyf.hotrefresh.remoting.message.handler.AbstractMessageHandler Maven / Gradle / Ivy

package com.hyf.hotrefresh.remoting.message.handler;

import com.hyf.hotrefresh.common.Log;
import com.hyf.hotrefresh.remoting.exception.RpcException;
import com.hyf.hotrefresh.remoting.message.Message;
import com.hyf.hotrefresh.remoting.message.MessageFactory;
import com.hyf.hotrefresh.remoting.rpc.RpcMessage;
import com.hyf.hotrefresh.remoting.rpc.RpcMessageFactory;
import com.hyf.hotrefresh.remoting.rpc.RpcMessageHandler;
import com.hyf.hotrefresh.remoting.rpc.payload.RpcErrorResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * @author baB_hyf
 * @date 2022/05/15
 */
public abstract class AbstractMessageHandler implements MessageHandler {

    /** message type -> handler */
    private Map> handlers = new HashMap<>();

    @SuppressWarnings("unchecked")
    @Override
    public Message handle(Message request) throws Exception {
        try {
            RpcMessage rpcMessage = (RpcMessage) request.getBody();
            RpcMessageHandler rpcMessageHandler = (RpcMessageHandler) getHandlers().get(request.getMessageType());
            if (rpcMessageHandler == null) {
                throw new RpcException("Unknown message type code: " + request.getMessageType());
            }
            Class rpcMessageClassType = RpcMessageFactory.getRpcMessageClassType((Class>) rpcMessageHandler.getClass());
            if (!(rpcMessageClassType == rpcMessage.getClass())) {
                throw new IllegalStateException("Rpc message type related handler not exist: " + rpcMessage.getClass());
            }
            RpcMessage response = rpcMessageHandler.handle(rpcMessage);
            return MessageFactory.createMessage(response);
        } catch (Throwable t) {
            if (Log.isDebugMode()) {
                Log.error("Handle message failed", t);
            }
            RpcErrorResponse rpcErrorResponse = new RpcErrorResponse();
            rpcErrorResponse.setThrowable(t);
            return MessageFactory.createMessage(rpcErrorResponse);
        }
    }

    protected Map> getHandlers() {
        return handlers;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy