共计 2447 个字符,预计需要花费 7 分钟才能阅读完成。
序
本文主要研究一下 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
- CommandExecutor
正文完