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
typeattribute of the<sofa:global-attrs>tag tofuture.<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
invokeTypeattribute of@SofaReferenceBindingtofuture.@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
typeattribute ofBoltBindingParamtofuture.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
invokeTypeattribute ofConsumerConfigtofuture.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
0specifies the timeout period for retrieving the result. You can change this value as needed. The valuetrueclears the result from the thread context. To keep the result, set this value tofalse.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
trueclears the result from the thread context. To keep the result, set this value tofalse.
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
typeattribute of the<sofa:global-attrs>tag tocallback. Then, set thecallback-reforcallback-classattribute. The following code shows an example that usescallback-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-refmust be the bean name of the callback class.Annotation method
If you reference the service using an annotation, set the
invokeTypeattribute of the@SofaReferenceBindingannotation tocallback. Then, set thecallbackClassorcallbackRefattribute. The following code shows an example that usescallbackRef:@SofaReference(binding =@SofaReferenceBinding(bindingType ="bolt", invokeType ="callback", callbackRef ="sampleCallback")) private SampleService sampleService;When you use an annotation, the value of the
callbackRefattribute 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
typeattribute ofBoltBindingParamtocallback. Then, set thecallbackClassorcallbackRefattribute. The following code shows an example that usescallbackClass: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
callbackusing thesetInvokeTypemethod, and then callsetOnReturnto 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.