前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊dubbo的CommandExecutor

聊聊dubbo的CommandExecutor

原创
作者头像
code4it
修改2019-07-08 11:01:27
5750
修改2019-07-08 11:01:27
举报

本文主要研究一下dubbo的CommandExecutor

CommandExecutor

dubbo-2.7.2/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/CommandExecutor.java

public interface CommandExecutor {
    /**
     * Execute one command and return the execution result
     *
     * @param commandContext command context
     * @return command execution result
     * @throws NoSuchCommandException
     */
    String execute(CommandContext commandContext) throws NoSuchCommandException;
}
  • CommandExecutor定义了execute方法,其方法传入CommandContext参数

CommandContext

dubbo-2.7.2/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/CommandContext.java

public class CommandContext {
​
    private String commandName;
    private String[] args;
    private Channel remote;
    private boolean isHttp;
    private Object originRequest;
​
    public CommandContext(String commandName) {
        this.commandName = commandName;
    }
​
    public CommandContext(String commandName, String[] args, boolean isHttp) {
        this.commandName = commandName;
        this.args = args;
        this.isHttp = isHttp;
    }
​
    public String getCommandName() {
        return commandName;
    }
​
    public void setCommandName(String commandName) {
        this.commandName = commandName;
    }
​
    public String[] getArgs() {
        return args;
    }
​
    public void setArgs(String[] args) {
        this.args = args;
    }
​
    public Channel getRemote() {
        return remote;
    }
​
    public void setRemote(Channel remote) {
        this.remote = remote;
    }
​
    public boolean isHttp() {
        return isHttp;
    }
​
    public void setHttp(boolean http) {
        isHttp = http;
    }
​
    public Object getOriginRequest() {
        return originRequest;
    }
​
    public void setOriginRequest(Object originRequest) {
        this.originRequest = originRequest;
    }
}
  • CommandContext定义了commandName、args、remote、isHttp、originRequest参数

DefaultCommandExecutor

dubbo-2.7.2/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/DefaultCommandExecutor.java

public class DefaultCommandExecutor implements CommandExecutor {
    @Override
    public String execute(CommandContext commandContext) throws NoSuchCommandException {
        BaseCommand command = null;
        try {
            command = ExtensionLoader.getExtensionLoader(BaseCommand.class).getExtension(commandContext.getCommandName());
        } catch (Throwable throwable) {
                //can't find command
        }
        if (command == null) {
            throw new NoSuchCommandException(commandContext.getCommandName());
        }
        return command.execute(commandContext, commandContext.getArgs());
    }
}
  • DefaultCommandExecutor实现了CommandExecutor接口,其execute方法会根据commandName通过ExtensionLoader来加载对应的comamnd,然后执行command的execute方法

小结

CommandExecutor定义了execute方法,其方法传入CommandContext参数;CommandContext定义了commandName、args、remote、isHttp、originRequest参数;DefaultCommandExecutor实现了CommandExecutor接口,其execute方法会根据commandName通过ExtensionLoader来加载对应的comamnd,然后执行command的execute方法

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CommandExecutor
  • CommandContext
  • DefaultCommandExecutor
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档