Ark service mechanism

Updated at:

SOFAArk defines two service types to solve communication problems between applications and plug-ins and between applications. This topic describes the two types of services.

Plug-in services

SOFAArk allows you to publish and reference services in a Plugin by using PluginContext. You can also reference services @ArkInject using annotations. To facilitate the development of advanced features, SOFAArk containers publish internal functional components as services by default, including Biz management, plug-in management, event management, and service registration management. Currently, Biz is not allowed to publish services. Only plug-in services can be referenced. The following describes how to publish and reference plug-in services and the services published by the SOFAArk container by default.

Publish service

Each plug-in can define a unique plug-in entry. You need to implement PluginActivator interface and declare it in the packaging plug-in configuration. The interface is defined as follows:

public interface PluginActivator{

/**
     * Start Plugin
     * @param context plugin context
     * @throws ArkRuntimeException
     */
void start(PluginContext context);

/**
     * Stop Plugin
     * @param context
     * @throws ArkRuntimeException
     */
void stop(PluginContext context);
}

When a SOFAArk container starts a plug-in, it calls the plug-in start entry (if any). Therefore, if the plug-in implementation needs to publish the plug-in service for other plug-ins or Biz to call, you can use the input parameter PluginContext to publish the service. PluginContext provides two methods to publish the service:

/**
     * Publish Plugin Service
     * @param ifClass service interface
     * @param implObject service implement object
     * @param <T>
     * @return
     */
<T>ServiceReference<T> publishService(Class<T> ifClass, T implObject);

/**
     * Publish Plugin Service
     * @param ifClass service interface
     * @param implObject service implement object
     * @param uniqueId service implementation id
     * @param <T>
     * @return
     */
<T>ServiceReference<T> publishService(Class<T> ifClass, T implObject,String uniqueId);

The difference between the two methods is whether to specify a uniqueId. Because there may be multiple service implementations for the same interface, you need to use uniqueId difference. Similarly, you also need to specify a uniqueId on the reference side. The default uniqueId is empty.

Reference service

SOFAArk provides two methods to reference plug-in services. Inside the plug-in, you can directly use the PluginContext to reference the service. PluginContext provide two simple ways to reference the service:

/**
     * 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 uniqueId service implementation
     * @return service reference
     */
<T>ServiceReference<T> referenceService(Class<T> ifClass,String uniqueId);

In Biz, if it is a Spring Boot or SOFABoot application, you can directly use the annotation @ArkInject to reference the service. The annotation declaration is as follows:

@java.lang.annotation.Target(ElementType.FIELD)
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
public@interfaceArkInject{
/**
     * ark service interface
     * @return
     */
Class<?> interfaceType()defaultvoid.class;

/**
     * ark service uniqueId
     *
     * @return return reference unique-id
     */
String uniqueId()default"";
}

SOFAArk provides the Spring Boot and SOFABoot functions. You can @ArkInject fields and specify the interface type and uniqueId to complete automatic injection. For ease of use, if no interfacetType type is specified, the annotated Field type is used by default.

In the plug-in, sometimes can also use @ArkInject reference service, that is, when the plug-in publishes a service, the service can directly use @ArkInject reference service; It should be noted that if the referenced service is published by other plug-ins, it must be satisfied that other plug-ins give priority to the current plug-in startup.

Default service

To facilitate the development of advanced features by Plugin and Biz, SOFAArk publishes internal functional components as services, including:

  • BizManageService:Biz Manager for managing and querying Biz information.

  • BizFactoryService:Biz parser for parsing Biz files.

  • PluginManagerService:Plugin manager, which is used to manage and query Plugin information.

  • PluginFactoryService:Plugin parser for parsing Plugin files.

  • EventAdminService:SOFAArk event manager, which is used to manage event listeners and publish events.

  • RegistryService: Plug-in service manager, which is really used to publish and reference plug-in services. Biz is not recommend to use it to publish services. Biz can only reference services. Otherwise, Biz may not be completely uninstalled.

The preceding services are released by default for the SOFAArk container. Other plugins and Biz services can be referenced. We recommend that you use them after you understand the principles.

Application services

The plug-in service is designed to solve the communication problem between Plugin and Biz. The communication between Biz depends on the runtime-sofa-boot-plugin provided by SOFABoot, which provides SofaService and SofaReference. For more information, see Service communication.