当前位置:网站首页>DataX tutorial (10) - hot plug principle of dataX plug-in
DataX tutorial (10) - hot plug principle of dataX plug-in
2022-06-25 06:28:00 【Yanglinwei】
List of articles
01 introduction
Through the previous blog , We are right. DataX With a certain in-depth solution :
- 《DataX course (01)- introduction 》
- 《DataX course (02)- IDEA function DataX Complete process ( Fill all the holes )》
- 《DataX course (03)- Source code interpretation ( Super detailed Edition )
- 《DataX course (04)- The configuration is complete 》
- 《DataX course (05)- DataX Web Project practice 》
- 《DataX course (06)- DataX tuning 》
- 《DataX course (07)- The illustration DataX Task assignment and execution process 》
- 《DataX course (08)- Monitoring and reporting 》
- 《DataX course (09)- DataX How to limit the speed ?》
This article mainly talks about DataX The principle of plug-in loading , In understanding DataX Before , We need to know about “ Parent delegate mechanism ”.
02 Parent delegate mechanism
I have written relevant articles before , You can refer to :《 In depth understanding of JVM Series of tutorials (11) - Class loader 》
2.1 Class loader relationship
Java The compiled class Bytecode is loaded by the classloader , And in the JVM Inside , There are several classloaders that come with the system , Take a look at the following diagram of class loaders :
About the above class loaders , The relationship between them :
- Start class loader (
BootStrap ClassLoader): from C++ Realization , No parent . It will be responsible for<JAVA_HOME>/libThe core class library or-XbootclasspathUnder the path specified by the parameterjarThe package is loaded into memory , Note that the virtual machine is loaded according to the file namejarBag , Such asrt.jar; - Extend the classloader (
Extension ClassLoader): from Java Language implementation , No parent loader . It's responsible for loading<JAVA_HOME>/lib/extDirectory or by system variables-Djava.ext.dirSpecifies the class library in the bit path ; - system class loader (
Application ClassLoader): fromJavaLanguage implementation , The parent loader isExtension ClassLoader. It is responsible for loading the system classpathjava -classpathor-D java.class.pathSpecify the class library under the path , That is what we often useclasspathroute , Developers can use the system classloader directly , In general, this class loading is the default class loader in the program , adoptClassLoader#getSystemClassLoader()Method to get the class loader . - Custom class loaders (
Custom ClassLoader): The parent loader isApplication ClassLoader.
I see the concept , What is the parental delegation mechanism ?
2.2 Parent delegation mechanism process
Process description :
- If a class loader receives a class load request , It doesn't load itself first , Instead, the request is delegated to the loader of the parent class ;
- If the parent loader still has its parent loader , Then further entrust , Recursion in turn , The request will eventually reach the top-level boot loader ;
- If the parent loader can complete the class loading task , You're back , If the parent loader cannot complete this load task , Sub loader will try to load by itself , This is the parent delegation model ;
Simply put : Every son is lazy , Every time I have a job, I leave it to my father , Until my father said I couldn't do it , My son tries to finish it by himself .
So why do you do this ?
- The advantage is
JavaClass has a hierarchical relationship with priority along with its classloader , Through this level, we can avoid the repeated loading of classes , When the father has loaded the class , There is no needClassLoaderLoad again . - Secondly, considering the safety factors ,
javaThe coreapiThe type defined in will not be replaced at will , Let's say you pass a message over the network calledjava.lang.IntegerClass , Passed through the parent delegate mode to the boot class loader , And the boot loader is at the coreJava APIThe class that found the name , Found that the class has been loaded , It doesn't reload the networkjava.lang.Integer, And directly return the loadedInteger.class, This prevents the coreAPIThe library was tampered with at will .
Disadvantages of the parental delegation mechanism :
- Limited by load range , The parent loader could not load into the required file , With
DriverInterface, for example , becauseDriverInterface defined injdkIn the middle of , And its implementation is provided by the service providers of various databases , such asmysqlThat's itMYSQL CONNECTOR, So here's the problem ,DriverManager( Also byjdkProvide ) To load the various implementationsDriverImplementation class of interface , And then manage , howeverDriverManagerLoaded by boot class loader , Load onlyJAVA_HOMEOflibThe file , And its implementation is provided by the service provider , Loaded by the system class loader .
It's time to break the parental delegation , Start the classloader to delegate the subclass loader to load Driver Realization , That's the famous one SPI(SERVICE PROVIDER INTERFACE) Mechanism .
2.3 be based on SPI The mechanism destroys parental delegation
principle : be based on “ Interface programming + The strategy pattern + The configuration file ” Dynamic loading mechanism of composite implementation .
No, SPI when :
- You can now
classpathRigamysql-connector-java.jar - Then write like this
Class clz = Class.forName("com.mysql.jdbc.Driver"); Driver d = (Driver) clz.newInstance();That's no problem ; - Reuse
Application ClassloaderTo load themysql-connector-java.jarOfcom.mysql.jdbc.Driver.
problem : The hard coded , Be sure to load "
com.mysql.jdbc.Driver", Not very elegant , Can't achieve “ Programming with interfaces , Automatic instantiation really realizes “ This coding form of .
Use SPI after :
- The code goes something like this :
Connection connection = DriverManager.getConnection("jdbc:mysql://xxxxxx/xxx", "xxxx", "xxxxx"); DriverManagerAccording to "jdbc:mysql" This prompt goes to the specific implementation .
ok, At this point, we will return to the theme of this article , About DataX How to load plug-ins ?
I'm sorry to say ,DataX There is no use SPI To sabotage parental delegation , But in another way ( Hot plug-in principle : Load class =》 Get the plug-in class name and path through the configuration file =》 Instantiate the plug-in UrlClassLoader => Switch the thread context loader to UrlClassLoader And save the original thread context loader =》 Load plug-in implementation class =》 Complete the operation based on the implementation class =》 Restore the original thread context loader ), Let's talk about that .
03 DataX Plug in hot plug
stay JobContainer have a look reader How the plug-in is loaded , Let's take a look at loading reader The code method of the plug-in :
private Reader.Job initJobReader(
JobPluginCollector jobPluginCollector) {
this.readerPluginName = this.configuration.getString(
CoreConstant.DATAX_JOB_CONTENT_READER_NAME);
classLoaderSwapper.setCurrentThreadClassLoader(LoadUtil.getJarLoader(
PluginType.READER, this.readerPluginName));
Reader.Job jobReader = (Reader.Job) LoadUtil.loadJobPlugin(
PluginType.READER, this.readerPluginName);
// Set up reader Of jobConfig
jobReader.setPluginJobConf(this.configuration.getConfiguration(
CoreConstant.DATAX_JOB_CONTENT_READER_PARAMETER));
// Set up reader Of readerConfig
jobReader.setPeerPluginJobConf(this.configuration.getConfiguration(
CoreConstant.DATAX_JOB_CONTENT_WRITER_PARAMETER));
jobReader.setJobPluginCollector(jobPluginCollector);
jobReader.init();
classLoaderSwapper.restoreCurrentThreadClassLoader();
return jobReader;
}
In fact, its process is very simple ,
- Read
job.jsonThe name of the profile plug-in ; - Use
LoadUtilaccording to Plug in type + The plugin name Get custom class loaderJarLoader(JarLoaderInherited fromjdkInsideURLClassLoader); ClassLoaderSwapperThread class loader switch class The generated in the previous stepJarLoaderThe classloader is set into the current context classloader ( Be careful : The original thread context loader will be saved before saving );- And then use
LoadUtilAdd plug-ins , Then the plug-in performs some initialization operations ; - Finally using
ClassLoaderSwapperRestore the original thread context loader .
You can see that there are two core classes , Respectively :
ClassLoaderSwapper( Thread class loader management class )LoadUtil( Plug in loading tool class )
Continue with the two classes .
3.1 ClassLoaderSwapper Thread class loader Management
First look at the source code :
public final class ClassLoaderSwapper {
private ClassLoader storeClassLoader = null;
private ClassLoaderSwapper() {
}
public static ClassLoaderSwapper newCurrentThreadClassLoaderSwapper() {
return new ClassLoaderSwapper();
}
/** * Save the current classLoader, And classLoader Set to given classLoader * * @param * @return */
public ClassLoader setCurrentThreadClassLoader(ClassLoader classLoader) {
this.storeClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
return this.storeClassLoader;
}
/** * Set the class loader of the current thread to the saved class load * @return */
public ClassLoader restoreCurrentThreadClassLoader() {
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.storeClassLoader);
return classLoader;
}
}
According to the source code , You can see that its function is mainly aimed at The classloader of the system and Custom class loader Of , Mainly done These two kinds of loaders are loaded in the current thread “ Switch ” And “ preservation ” The operation of .
3.2 LoadUtil Plug in loader
LoadUtil Plug in loaded , According to the type of plug-ins, they are divided into :
- reader
- writer
- transformer( Unrealized )
LoadUtil Plug in loaded , It is divided into... According to the type of operation :
- Job
- Task
3.2.1 Gets the class loader
Let's first look at the method of getting the class loader :
public static synchronized JarLoader getJarLoader(PluginType pluginType,
String pluginName) {
Configuration pluginConf = getPluginConf(pluginType, pluginName);
JarLoader jarLoader = jarLoaderCenter.get(generatePluginKey(pluginType,
pluginName));
if (null == jarLoader) {
String pluginPath = pluginConf.getString("path");
if (StringUtils.isBlank(pluginPath)) {
throw DataXException.asDataXException(
FrameworkErrorCode.RUNTIME_ERROR,
String.format(
"%s plug-in unit [%s] Illegal path !",
pluginType, pluginName));
}
jarLoader = new JarLoader(new String[]{
pluginPath});
jarLoaderCenter.put(generatePluginKey(pluginType, pluginName),
jarLoader);
}
return jarLoader;
}
getJarLoader() The main method is to directly follow the path of the plug-in new One. JarLoader, Take a closer look JarLoader Method view of :
JarLoader It's based on jdk Inside URLClassLoader Carry out secondary implementation .
3.2.2 Add plug-ins
Put on LoadUtil The code loaded by the plug-in :
public static AbstractJobPlugin loadJobPlugin(PluginType pluginType,
String pluginName) {
Class<? extends AbstractPlugin> clazz = LoadUtil.loadPluginClass(
pluginType, pluginName, ContainerType.Job);
try {
AbstractJobPlugin jobPlugin = (AbstractJobPlugin) clazz
.newInstance();
jobPlugin.setPluginConf(getPluginConf(pluginType, pluginName));
return jobPlugin;
} catch (Exception e) {
throw DataXException.asDataXException(
FrameworkErrorCode.RUNTIME_ERROR,
String.format("DataX find plugin[%s] Of Job To configure .",
pluginName), e);
}
}
The code is very clear , It is through clazz.newInstance() Method to generate an instance ( The strategist model ).
Come here LoadUtil The code of is basically explained .
04 At the end of the article
This article is about DataX Principle analysis of plug-in loading , If you have any questions about children's shoes, please leave a message , Thank you for reading , The end of this paper !
边栏推荐
- Investment opportunities and operational risk assessment report of China's engineering consulting industry during the 14th Five Year Plan period 2022-2028
- RM command – remove file or directory
- Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
- Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
- [kicad image] download and installation
- Wireless industrial Internet of things data monitoring terminal
- After five years of software testing in ByteDance, I was dismissed in December to remind my brother of paddling
- Laravel8+ wechat applet generates QR code
- The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
- Understand what ICMP Protocol is
猜你喜欢

Rhcsa--- day 6 operation

Cannot activate inspection type when SAP retail uses transaction code mm41 to create commodity master data?

Lesson 9: workspace introduction

Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things

Digitalization, transformation?
![[kicad image] download and installation](/img/88/cebf8cc55cb8904c91f9096312859a.jpg)
[kicad image] download and installation

The elephant turns around and starts the whole body. Ali pushes Maoxiang not only to Jingdong
![[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]](/img/36/ad86f403b47731670879f01299b416.jpg)
[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]

Socket, network model notes

Microsoft issued a document to celebrate Net 20th anniversary!
随机推荐
Methods for obtaining some information of equipment
How two hosts in different network segments directly connected communicate
Rhcsa--- day 6 operation
北京网上开股票账户安全吗?
Uni app wechat applet customer service chat function
What is the IP address
Monitoring access: how to grant minimum WMI access to the monitoring service account
Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things
Which of the top ten securities companies has the lowest Commission and is the most safe and reliable? Do you know anything
Curl command – file transfer tool
[network security] sharing of experience and ideas of an emergency battle
An easy problem
cacacahe
Report on strategic suggestions on investment direction and Prospect of global and Chinese marine biological industry (2022 Edition)
HCIP Day 16
十大券商公司哪个佣金最低,最安全可靠?有知道的吗
Cat command – display the file contents on the terminal device
Exercise: completion
SAP QM executes the transaction code qp01, and the system reports an error -material type food is not defined for task list type Q-
Face++ realizes face detection by flow