Skip to content
xquan edited this page Sep 27, 2016 · 3 revisions

Navigation

中文

Home

Introduction

Many of service invocation in the framework is synchronous which means the caller code and called code are running at same thread, so the caller code must wait until the called code return. In many cases we hope the service invocation is asynchronous based, for instance call a service method which has long time execution or receive data from a remote service ...

Get Start

First we define a generic service:

@Service
public class RealService {

    public String getTitle(String name) {
        return "Test";
    }
}

The service is simple, it contains only one method which accept a string argument and return another string. Then we can using IAsyncService to make a service method call from synchronous to asynchronous:

@Service
public class AsyncServiceCall {

    @Inject
    protected RealService _svc;

    @Inject
    protected IAsyncService _asyncSvc;

    public void callService() {
        this._asyncSvc.call(
                () -> this._svc.getTitle("abc"),
                (callId, result) -> {
                    assert "1".equals(callId);
                    assert "Test".equals(result);
                });
    }
}

First we inject RealService, then we inject a instance of IAsyncService, using IAsyncService's call method to invoke RealService to profile asynchronous functionality. The call method has multiple override version, here we use one of them. The call method return a string called Call ID which used to identify each call, using Call ID, you can find out which call can mapped call result in the call back function.

The first argument of call is Runnable interface which used to invoke service, here we use it to invoke RealService.getTitle method, the second argument is ICallSucceed functional interface, when service calling succeed (call RealService.getTitle), the interface will be called to notify caller, the functional interface has two arguments, one is Call ID, the other is the calling result.

IAsyncService's call method support an optional argument called options, currently it support one option named TimeOut, it is used to specify the call time out, if the option is not specified then the call has no time out, the option's type is IntervalTime.

配置

Configurable Path Configuration Type Description Is Required Default Value (Behavior)
service.async.time-of-check IntervalTime Specify asynchronous call time out check time interval No 10s
Clone this wiki locally