All Products
Document Center

Using API

Last Updated: Aug 21, 2020

SOFA provides a mechanism to store programming APIs of various components, as well as a unified method for you to obtain these APIs. The unified method is the ClientFactory class. You can store and obtain the programming APIs of components through ClientFactory. SOFA provides two ways to obtain the ClientFactory class:

When using the programming related APIs of components in SOFA, ensure that the following dependency has been added to the module:

  1. <dependency>
  2. <groupId></groupId>
  3. <artifactId>rpc-enterprise-sofa-boot-starter</artifactId>
  4. </dependency>

Obtain the ClientFactory class

Implement the ClientFactoryAware interface

The first way to obtain the ClientFactory class is to implement the ClientFactoryAware interface. The following shows the code example:

  1. public class ClientFactoryBean implements ClientFactoryAware {
  2. private ClientFactory clientFactory;
  3. @Override
  4. public void setClientFactory(ClientFactory clientFactory) {
  5. this.clientFactory = clientFactory;
  6. }
  7. public ClientFactory getClientFactory() {
  8. return clientFactory;
  9. }
  10. }

Then, configure the above ClientFactoryBean as a Spring Bean.

  1. <bean id="clientFactoryBean" class=""/>

As a result, ClientFactoryBean can obtain the clientFactory object.

Use the @SofaClientFactory annotation

The second way to obtain the ClientFactory class is to use the @SofaClientFactory annotation. The following shows the code example:

  1. public class ClientAnnotatedBean {
  2. @SofaClientFactory
  3. private ClientFactory clientFactory;
  4. public ClientFactory getClientFactory() {
  5. return clientFactory;
  6. }
  7. }

Add the @SofaClientFactory annotation to the ClientFactory field, and configure ClientAnnotatedBean as a Spring Bean:

  1. <bean id="clientAnnotatedBean" class=""/>

As a result, the SOFA framework automatically injects the ClientFactory instance into the field annotated by @SofaClientFactory.

However, you can only obtain the ClientFactory object in this way. To obtain a specific client, such as ServiceFactory, you need to call the getClient method of ClientFactory. SOFA strengthens the @SofaClientFactory annotation so that you can directly use @SofaClientFactory to obtain the specific client. The following shows the code example:

  1. public class ClientAnnotatedBean {
  2. @SofaClientFactory
  3. private ServiceClient serviceClient;
  4. public ServiceClient getServiceClient() {
  5. return serviceClient;
  6. }
  7. }

When @SofaClientFactory is directly used for a specific client, this annotation can directly inject the corresponding client into the field annotated. In the above example, @SofaClientFactory is directly annotated on a ServiceClient field, and the SOFA framework directly injects the ServiceClient object into this field. You can obtain an object in this way if it exists in ClientFactory.

Programming API example

Service publication and subscription can be not only loaded statically on app startup by configuring a Spring Bean in the XML mode, but also executed dynamically by using the programming API at runtime, as follows:

Publish a Bolt service

  1. ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class);
  2. ServiceParam serviceParam = new ServiceParam();
  3. serviceParam.setInstance(sampleService);
  4. serviceParam.setInterfaceType(SampleService.class);
  5. serviceParam.setUniqueId(uniqueId);
  6. TrBindingParam trBinding = new TrBindingParam();
  7. // Correspond to the global-attrs tag.
  8. trBinding.setClientTimeout(5000);
  9. serviceParam.addBindingParam(trBinding);
  10. serviceClient.service(serviceParam);

Reference a Bolt service

  1. ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class);
  2. ReferenceParam<SampleService> referenceParam = new ReferenceParam<SampleService>();
  3. referenceParam.setInterfaceType(SampleService.class);
  4. referenceParam.setUniqueId(uniqueId);
  5. TrBindingParam trBinding = new TrBindingParam();
  6. // Correspond to the global-attrs tag.
  7. trBinding.setClientTimeout(8000);
  8. // Correspond to the method tag.
  9. TrBindingMethodInfo trMethodInfo = new TrBindingMethodInfo();
  10. trMethodInfo.setName("helloMethod");
  11. trMethodInfo.setType("callback");
  12. // The object must implement the interface.
  13. trMethodInfo.setCallbackHandler(callbackHandler);
  14. trBinding.addMethodInfo(trMethodInfo);
  15. referenceParam.setBindingParam(trBinding);
  16. SampleService proxy = referenceClient.reference(referenceParam);


  • In SOFA, the Reference object created by dynamic client is heavy. Do not create it frequently in actual practice and make sure you cache it in advance. Otherwise, memory overflow may occur.
  • The class of a programming API cannot be changed easily. To be compatible with previous usage, the class name still contains TR. However, the Bolt protocol is actually used.