Ark container plug-in mechanism
Ark provides the interface com.alipay.sofa.ark.spi.service.PluginActivator for plug-in initiation, which is defined as follows:
public interface PluginActivator{
/**
* Start Plugin
* @param context plugin context
* @throws ArkException
*/
void start(PluginContext context) throws ArkException;
/**
* Stop Plugin
* @param context
* @throws ArkException
*/
void stop(PluginContext context) throws ArkException;
}The plug-in only needs to implement this interface and configure activator properties in the MANIFEST.MF, and it will execute the start method when it starts and the stop method when it stops.
Ark plug-in communication
The communication between Arks is done through services, and the interface between the publishing service and the reference service is provided in the input parameter type com.alipay.sofa.ark.spi.model.PluginContext of the above start interface method:
/**
* Publish Plugin Service
* @param ifClass service interface
* @param implObject service implement object
* @param <T>
* @return
*/
<T>ServiceReference<T> publishService(Class<T> ifClass, T implObject);
/**
* Get Service publish by plugin, when there are multiple services, return the highest priority plugin service
* @param ifClass service interface
* @param <T>
* @return service reference
*/
<T>ServiceReference<T> referenceService(Class<T> ifClass);
/**
* Get Service publish by one specific plugin
* @param ifClass service interface
* @param <T>
* @param pluginName the name of the plugin which publish the service
* @return service reference
*/
<T>ServiceReference<T> referenceService(Class<T> ifClass,String pluginName);
/**
* Get Service List publish by plugin
* @param ifClass service interface
* @param <T>
* @return
*/
<T>List<ServiceReference<T>> referenceServices(Class<T> ifClass);Plug-in services are based on interfaces and target the same interface:
Each plug-in can publish only one service. If the plug-in is repeatedly published, the reference of the published service is directly returned.
When multiple plug-ins publish services, if a single service is referenced through the
referenceService:If pluginName is not specified, the service with the highest priority is returned.
If pluginName is specified, the services published by the plug-in are returned.
The returned service reference ServiceReference is defined as follows:
public interface ServiceReference<T>{
/**
* get Service Object
* @return service
*/
T getService();
/**
* get Service Metadata
* @return
*/
ServiceMetadata getServiceMetadata();
}
public interface ServiceMetadata{
/**
* get Service Unique Name
* @return service name
*/
String getServiceName();
/**
* get Service Interface Class
* @return interface class
*/
Class<?> getInterfaceClass();
/**
* get ServiceProvider
* @return
*/
ServiceProvider getServiceProvider();
}getService()the entity that can obtain the service (that is, the implObject when the service is published)getServiceMetadata()can obtain the metadata of the service, including:Service Name: the interface name of the service.
The service interface.
Provider of the service: includes the provider name (plug-in name, etc.) and the provider priority (plug-in priority).