Bolt protocol invocation methods

更新时间:
复制 MD 格式

SOFARPC provides multiple invocation methods for the Bolt protocol to suit different scenarios.

Synchronization

In a synchronous call, the client sends a request and waits for the server-side response before it continues. This is the default invocation method in SOFARPC and requires no configuration.

Asynchronous call

Set the invocation method

In an asynchronous call, the client sends a request and continues processing without waiting for the server-side response. SOFARPC caches the response. The client can then call an API to retrieve the result when needed. To configure a service to use asynchronous calls, set the type property in the corresponding configuration.

  • XML method

    When using XML, set the type attribute of the <sofa:global-attrs> tag to future.

    <sofa:reference interface="com.example.demo.SampleService" id="sampleService">
         <sofa:binding.bolt>
             <sofa:global-attrs type="future"/>
         </sofa:binding.bolt>
    </sofa:reference>

  • Annotation method

    When using an annotation, set the invokeType attribute of @SofaReferenceBinding to future.

    @SofaReference(binding =@SofaReferenceBinding(bindingType ="bolt", invokeType ="future"))
    private SampleService sampleService;

  • API method in a Spring environment

    When using the API in a Spring environment, set the type attribute of BoltBindingParam to future.

    BoltBindingParam boltBindingParam =new BoltBindingParam();
    boltBindingParam.setType("future");

  • API method in a non-Spring environment

    When using the raw SOFARPC API in a non-Spring environment, set the invokeType attribute of ConsumerConfig to future.

    ConsumerConfig<SampleService> consumerConfig =new ConsumerConfig<SampleService>()
    .setInterfaceId(SampleService.class.getName())
    .setRegistry(registryConfig)
    .setProtocol("bolt")
    .setInvokeType("future");

Get the call result

There are two ways to retrieve the result of an asynchronous call:

  • Retrieve the result directly

    You can retrieve the result of an asynchronous call directly as follows:

    String result =(String)SofaResponseFuture.getResponse(0,true);

    The value 0 specifies the timeout period for retrieving the result. You can change this value as needed. The value true clears the result from the thread context. To keep the result, set this value to false.

  • Retrieve a native JDK Future

    You can retrieve a native Java Development Kit (JDK) Future object as follows. You can then call this Future object from anywhere to retrieve the result.

    Future future =SofaResponseFuture.getFuture(true);

    The value true clears the result from the thread context. To keep the result, set this value to false.

Callback

The callback method of the SOFARPC Bolt protocol allows the client to make a call without waiting for a response. When the client receives the response from the server, SOFARPC automatically calls a callback interface that you implement.

To use the callback method of the SOFARPC Bolt protocol, you must first implement a callback interface. Then, you must specify the callback interface in the configuration and set the invocation method to callback.

Implement the callback interface

SOFARPC provides the com.alipay.sofa.rpc.core.invoke.SofaResponseCallback interface. To use the callback method of the SOFARPC Bolt protocol, you must first implement this interface. The interface provides the following three methods:

  • onAppResponse: SOFARPC calls this method when the client receives a normal response from the server.

  • onAppException: SOFARPC calls this method when the client receives an exception response from the server.

  • onSofaException: SOFARPC calls this method when an error occurs in SOFARPC itself, such as a routing error.

Set the callback interface

After you implement the callback interface, specify the implementation class in the service reference configuration and set the invocation method to `callback`. SOFARPC provides two ways to specify the callback interface: Callback Class and Callback Ref.

  • Callback Class: Specify the class name of the callback. SOFARPC generates an instance of the callback class by calling its default constructor.

  • Callback Ref: Directly provide an instance of the callback class.

The following code shows how to specify the callback interface using different methods:

  • XML method

    If you reference the service using XML, set the type attribute of the <sofa:global-attrs> tag to callback. Then, set the callback-ref or callback-class attribute. The following code shows an example that uses callback-ref:

    <bean id="sampleCallback" class="com.example.demo.SampleCallback"/>
    <sofa:reference interface="com.example.demo.SampleService" id="sampleService">
        <sofa:binding.bolt>
            <sofa:global-attrs type="callback" callback-ref="sampleCallback"/>
        </sofa:binding.bolt>
    </sofa:reference>

    When you use XML, the value of callback-ref must be the bean name of the callback class.

  • Annotation method

    If you reference the service using an annotation, set the invokeType attribute of the @SofaReferenceBinding annotation to callback. Then, set the callbackClass or callbackRef attribute. The following code shows an example that uses callbackRef:

    @SofaReference(binding =@SofaReferenceBinding(bindingType ="bolt",
                invokeType ="callback",
                callbackRef ="sampleCallback"))
    private SampleService sampleService;

    When you use an annotation, the value of the callbackRef attribute must be the bean name of the callback class.

  • API method in a Spring environment

    If you use the API in a Spring or Spring Boot environment, set the type attribute of BoltBindingParam to callback. Then, set the callbackClass or callbackRef attribute. The following code shows an example that uses callbackClass:

    BoltBindingParam boltBindingParam =new BoltBindingParam();
    boltBindingParam.setType("callback");
    boltBindingParam.setCallbackClass("com.example.demo.SampleCallback");
  • API method in a non-Spring environment

    If you use the raw SOFARPC API in a non-Spring environment, set the invocation type to callback using the setInvokeType method, and then call setOnReturn to specify the callback class.

    ConsumerConfig<SampleService> consumerConfig =new ConsumerConfig<SampleService>()
    .setInterfaceId(SampleService.class.getName())
    .setRegistry(registryConfig)
    .setProtocol("bolt")
    .setInvokeType("callback")
    .setOnReturn(new SampleCallback());

Set the callback interface at the call level

In addition to specifying the callback interface at the service level, you can also specify it at the call level as follows:

RpcInvokeContext.getContext().setResponseCallback(newSampleCallback());

One-way

A one-way call is used when the client sends a request but does not require a response from the server. This method returns null immediately after the call is made and ignores the server-side response.

To use a one-way call, set the invocation method to oneway. The configuration is the same as setting the method to future or callback. For more information, see the configuration methods described earlier in this document. Note that because a one-way call returns immediately, all timeout settings are invalid.