rocketmq学习笔记 五 源码之rocketmq-remoting

来源:互联网 发布:会计网络教育68所院校 编辑:程序博客网 时间:2024/05/17 20:33

之前学习过netty源码,所以看rocketmq-remoting 就比较简单了

核心流程




解析请求


public void processRequestCommand(final ChannelHandlerContext ctx, final RemotingCommand cmd) {        final Pair<NettyRequestProcessor, ExecutorService> matched = this.processorTable.get(cmd.getCode());        final Pair<NettyRequestProcessor, ExecutorService> pair = null == matched ? this.defaultRequestProcessor : matched;        final int opaque = cmd.getOpaque();        if (pair != null) {            Runnable run = new Runnable() {                @Override                public void run() {                    try {                        RPCHook rpcHook = NettyRemotingAbstract.this.getRPCHook();                        if (rpcHook != null) {                            rpcHook.doBeforeRequest(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd);                        }                        final RemotingCommand response = pair.getObject1().processRequest(ctx, cmd);                        if (rpcHook != null) {                            rpcHook.doAfterResponse(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd, response);                        }                        if (!cmd.isOnewayRPC()) {                            if (response != null) {                                response.setOpaque(opaque);                                response.markResponseType();                                try {                                    ctx.writeAndFlush(response);                                } catch (Throwable e) {                                    plog.error("process request over, but response failed", e);                                    plog.error(cmd.toString());                                    plog.error(response.toString());                                }                            } else {                            }                        }                    } catch (Throwable e) {                        if (!"com.aliyun.openservices.ons.api.impl.authority.exception.AuthenticationException"                                .equals(e.getClass().getCanonicalName())) {                            plog.error("process request exception", e);                            plog.error(cmd.toString());                        }                        if (!cmd.isOnewayRPC()) {                            final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_ERROR, //                                    RemotingHelper.exceptionSimpleDesc(e));                            response.setOpaque(opaque);                            ctx.writeAndFlush(response);                        }                    }                }            };            if (pair.getObject1().rejectRequest()) {                final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_BUSY,                        "[REJECTREQUEST]system busy, start flow control for a while");                response.setOpaque(opaque);                ctx.writeAndFlush(response);                return;            }            try {                final RequestTask requestTask = new RequestTask(run, ctx.channel(), cmd);                pair.getObject2().submit(requestTask);            } catch (RejectedExecutionException e) {                if ((System.currentTimeMillis() % 10000) == 0) {                    plog.warn(RemotingHelper.parseChannelRemoteAddr(ctx.channel()) //                            + ", too many requests and system thread pool busy, RejectedExecutionException " //                            + pair.getObject2().toString() //                            + " request code: " + cmd.getCode());                }                if (!cmd.isOnewayRPC()) {                    final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.SYSTEM_BUSY,                            "[OVERLOAD]system busy, start flow control for a while");                    response.setOpaque(opaque);                    ctx.writeAndFlush(response);                }            }        } else {            String error = " request type " + cmd.getCode() + " not supported";            final RemotingCommand response =                    RemotingCommand.createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error);            response.setOpaque(opaque);            ctx.writeAndFlush(response);            plog.error(RemotingHelper.parseChannelRemoteAddr(ctx.channel()) + error);        }    }



解析相应


    public void processResponseCommand(ChannelHandlerContext ctx, RemotingCommand cmd) {        final int opaque = cmd.getOpaque();        final ResponseFuture responseFuture = responseTable.get(opaque);        if (responseFuture != null) {            responseFuture.setResponseCommand(cmd);            responseFuture.release();            responseTable.remove(opaque);            if (responseFuture.getInvokeCallback() != null) {                boolean runInThisThread = false;                ExecutorService executor = this.getCallbackExecutor();                if (executor != null) {                    try {                        executor.submit(new Runnable() {                            @Override                            public void run() {                                try {                                    responseFuture.executeInvokeCallback();                                } catch (Throwable e) {                                    plog.warn("execute callback in executor exception, and callback throw", e);                                }                            }                        });                    } catch (Exception e) {                        runInThisThread = true;                        plog.warn("execute callback in executor exception, maybe executor busy", e);                    }                } else {                    runInThisThread = true;                }                if (runInThisThread) {                    try {                        responseFuture.executeInvokeCallback();                    } catch (Throwable e) {                        plog.warn("executeInvokeCallback Exception", e);                    }                }            } else {                responseFuture.putResponse(cmd);            }        } else {            plog.warn("receive response, but not matched any request, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()));            plog.warn(cmd.toString());        }    }




0 0
原创粉丝点击