Activiti source code analysis (I) design mode, activiti source code
All the friends who have basic knowledge about activiti know that activiti exposes seven interfaces to provide workflow-related services. How are these interfaces implemented? View the source code and find that the implementation is roughly as follows:
public class RuntimeServiceImpl extends ServiceImpl implements RuntimeService { public ProcessInstance startProcessInstanceByKey(String processDefinitionKey) { return commandExecutor.execute(new StartProcessInstanceCmd<ProcessInstance>(processDefinitionKey, null, null, null)); } public ProcessInstance startProcessInstanceByKey(String processDefinitionKey, String businessKey) { return commandExecutor.execute(new StartProcessInstanceCmd<ProcessInstance>(processDefinitionKey, null, businessKey, null)); } ...}
In serviceall major deployment operations are completed by calling commandexecutor.exe cute (). However, if you click here, you will find that nothing is done:
public class CommandExecutorImpl implements CommandExecutor { private final CommandConfig defaultConfig; private final CommandInterceptor first; public CommandExecutorImpl(CommandConfig defaultConfig, CommandInterceptor first) { this.defaultConfig = defaultConfig; this.first = first; } public CommandInterceptor getFirst() { return first; } @Override public CommandConfig getDefaultConfig() { return defaultConfig; } @Override public <T> T execute(Command<T> command) { return execute(defaultConfig, command); } @Override public <T> T execute(CommandConfig config, Command<T> command) { return first.execute(config, command); }}
When we see this, we will find that we cannot see what this statement has done, so how does it provide services? In fact, most of activiti operations are completed based on the command mode in the design mode (the responsibility chain mode is also used to construct the command interceptor chain, used to perform a series of operations before the command is actually executed ). The following uses the source code to introduce these design ideas in detail:
The command mode of activiti may be different from the command mode I have previously viewed. The essence of the command mode is to encapsulate commands, separate issuing commands from executing commands. In the responsibility chain mode, you only need to put requests on the responsibility chain, and the handling details and transmission do not need to be considered. Activiti integrates these two modes to form the main implementation mode of its services. There are only three core components: CommandExecutor (Command executor, used to execute commands), CommandInterceptor (Command interceptor, used to build the interceptor chain), and Command (Command itself ). These three interfaces are the core of the whole part, will also involve other key classes, will be explained one by one, these three classes are in the core package of the activiti Implementation of activiti-engine.jar, location: org. activiti. engine. impl. interceptor. The following three interfaces gradually introduce related classes and specific implementations:
Source code of the three interfaces:
public interface Command <T> { T execute(CommandContext commandContext); }
/** * The command executor for internal usage. */public interface CommandExecutor { /** * @return the default {@link CommandConfig}, used if none is provided. */ CommandConfig getDefaultConfig(); /** * Execute a command with the specified {@link CommandConfig}. */ <T> T execute(CommandConfig config, Command<T> command); /** * Execute a command with the default {@link CommandConfig}. */ <T> T execute(Command<T> command); }
public interface CommandInterceptor { <T> T execute(CommandConfig config, Command<T> command); CommandInterceptor getNext(); void setNext(CommandInterceptor next);}
Export cute (commandConfig, command ). CommandInterceptor contains a set and get method, which is used to set the next (actually the next CommandInterceptor) variable. Imagine that the next interceptor chain of the interceptor chain can be found in this form, and the command can be passed on.
A brief look: one of the standard services of servicerealtime is to call commandexecutor.exe cute (new command () in the specific service (here the command is a specific command ). The execution of the command executor commandexecutor.exe cute calls the execute method of its internal variable CommandInterceptor first (the first command interceptor) (with the commandConfig parameter added ). Cute (). You can call this command to implement the final content.
Not complete, very sleepy, to be continued.