You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@aries.apache.org by "Lorr, Sebastian" <Se...@its-digital.de> on 2019/01/25 12:07:12 UTC

Aries Async Service and Timeouts

Hi there,



I want to call a Service and wait for a given time for a response.

So I did a async servicecall like this:



// Create mediator, to call service asynchronously

IMyService mediated = asyncService.mediate(myService, IMyService.class);



// Call service and await promise to be fulfilled

Promise<Result> promise = asyncService.call(mediated.process());

boolean timeoutExceeded = false;

long startTime = Calendar.getInstance().getTimeInMillis();



serviceTimeoutMillis = 100;

// Wait until timeout for promise to be done

while (!promise.isDone()) {

                Thread.sleep(10);

                if (Calendar.getInstance().getTimeInMillis() > startTime + serviceTimeoutMillis) {

                               timeoutExceeded = true;

                               break;

                }

}

if (timeoutExceeded) {
    throw new TimeoutException("Timeout exceeded");
}



Now the question: is there a better way for waiting for promise-resolve until a given timeout?

Maybe some feature of Async that i missed?





Thanks in advance,



Sebastian



----------------------------------------------------------------
Sebastian Lorr
ITS Digital Solutions GmbH
Dillenburger Str. 77
D-51105 Köln
Tel.: +49 (0)221 820 07 0
Fax : +49 (0)221 820 07 22<tel:%2B49%20%280%29221%20820%2007%2022>
Mail: info@its-telco.de<ma...@its-telco.de>
Web : http://www.its-telco.de<http://www.its-telco.de/>
----------------------------------------------------------------
Sitz der Gesellschaft: Dortmund
Amtsgericht Dortmund, HRB 28563
Geschäftsführer: Gunnar Haack, Ludger Schulte, Heinrich Toben, Raimund Schipp, Ralf Petersilka
----------------------------------------------------------------

Diese E-Mail enthält vertrauliche Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist nicht gestattet.

This e-mail may contain confidential information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.


Re: Aries Async Service and Timeouts

Posted by Timothy Ward <ti...@apache.org>.
Hi Sebastian,


Maybe some feature of Async that i missed?

For the moment, no. The Aries Async project hasn’t had many calls for new functionality, and so it hasn’t been updated to support OSGi R7. The Promises support is mostly done, and with a day or two of effort I’m sure it would be back up to date.

I'm not sure if Aries Async uses the Promise API in a substitutable way, but the latest version of the Promise API has timeout methods [1].

If you did have access to the OSGi R7 API then this would be a much neater and easier way to deal with the timeout. Note that while this would implement the timeout from the client side, it would not actually cancel the running work

One option is simply to try to use the lastest Promise API along with Aries Async.

Unfortunately this won’t work because the Async implementation requires the use of Aries promises (supplying threads). This restriction would no longer be necessary with Promises 1.1 because of the PromiseFactory, but this would require some more (again not huge amounts) work in the Async impl.


A suggestion that would work with the current impl is as follows:


// Create mediator, to call service asynchronously, both this and the Executor should be
// cached and reused if possible.
IMyService mediated = asyncService.mediate(myService, IMyService.class);


ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();


// Call service asynchronously
Promise<Result> promise = asyncService.call(mediated.process());

// Create a holder for our “timeout” result
Deferred<Result> d = new Deferred<>();

// Register the timeout
final ScheduledFuture<?> sf = ses.schedule(
        () -> d.fail(new TimeoutException("Timeout exceeded”), 100, TimeUnit.MILLISECONDS);

// Clean up the timeout if we resolve first
promise.onResolve(() -> sf.cancel(false));

// Resolve with the real result if it beats the timeout.
d.resolveWith(promise);

// This is the promise that will fail if there is a timeout or succeed if not
Promise<Result> promiseWithTimeout = d.getPromise();


The whole thing would clearly be simpler if some updates were made to the Aries Async Service implementation. If anyone has the time to make a patch then I’d be happy to review it!

Best Regards,

Tim
On 25 Jan 2019, at 16:02, Raymond Auge <ra...@liferay.com>> wrote:



On Fri, Jan 25, 2019 at 10:15 AM Benjamin Edwards <ed...@gmail.com>> wrote:
I'd also suggest that blocking the thread either by polling in a loop or using a dedicated timeout method is somewhat antithetical to the point of an asynchronous call. Better usually to do something like resolve a promise either with the service result or an exception after a threshold. This allows you to chain promises together nicely without blocking but also keeps things ticking along. It seems a shame that OSGi promises don't support a notion of cancellation (which is helpful when racing a long running call with a timeout).

If you have access to the deferred that the promise came from you can "cancel" by self-resolving using:

org.osgi.util.promise.Deferred.resolveWith(Promise<? extends T>)

See org.osgi.util.promise.Promises for handy factory methods you can pair that with to get the effect you want, a failure or success with a value.

- Ray


Can't have everything I guess.

Ben

On Fri, 25 Jan 2019, 2:58 pm Raymond Auge, <ra...@liferay.com>> wrote:
I'm not sure if Aries Async uses the Promise API in a substitutable way, but the latest version of the Promise API has timeout methods [1].

Perhaps Tim Ward could shed some light on this?
One option is simply to try to use the lastest Promise API along with Aries Async.

Sincerely,
- Ray

[1] https://osgi.org/specification/osgi.cmpn/7.0.0/util.promise.html#util.promise-timing.methods

On Fri, Jan 25, 2019 at 7:07 AM Lorr, Sebastian <Se...@its-digital.de>> wrote:
Hi there,


I want to call a Service and wait for a given time for a response.

So I did a async servicecall like this:



// Create mediator, to call service asynchronously

IMyService mediated = asyncService.mediate(myService, IMyService.class);



// Call service and await promise to be fulfilled

Promise<Result> promise = asyncService.call(mediated.process());

boolean timeoutExceeded = false;

long startTime = Calendar.getInstance().getTimeInMillis();



serviceTimeoutMillis = 100;

// Wait until timeout for promise to be done

while (!promise.isDone()) {

                Thread.sleep(10);

                if (Calendar.getInstance().getTimeInMillis() > startTime + serviceTimeoutMillis) {

                               timeoutExceeded = true;

                               break;

                }

}

if (timeoutExceeded) {
    throw new TimeoutException("Timeout exceeded");
}





Now the question: is there a better way for waiting for promise-resolve until a given timeout?

Maybe some feature of Async that i missed?




Thanks in advance,



Sebastian




----------------------------------------------------------------
Sebastian Lorr
ITS Digital Solutions GmbH
Dillenburger Str. 77<https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
<https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
D-51105 Köln<https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
Tel.: +49 (0)221 820 07 0
Fax : +49 (0)221 820 07 22<tel:%2B49%20%280%29221%20820%2007%2022>
Mail: info@its-telco.de<ma...@its-telco.de>
Web : http://www.its-telco.de<http://www.its-telco.de/>
----------------------------------------------------------------
Sitz der Gesellschaft: Dortmund
Amtsgericht Dortmund, HRB 28563
Geschäftsführer: Gunnar Haack, Ludger Schulte, Heinrich Toben, Raimund Schipp, Ralf Petersilka
----------------------------------------------------------------

Diese E-Mail enthält vertrauliche Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist nicht gestattet.

This e-mail may contain confidential information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.




--
Raymond Augé<http://www.liferay.com/web/raymond.auge/profile> (@rotty3000)
Senior Software Architect Liferay, Inc.<http://www.liferay.com/> (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance<http://osgi.org/> (@OSGiAlliance)


--
Raymond Augé<http://www.liferay.com/web/raymond.auge/profile> (@rotty3000)
Senior Software Architect Liferay, Inc.<http://www.liferay.com/> (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance<http://osgi.org/> (@OSGiAlliance)


Re: Aries Async Service and Timeouts

Posted by Raymond Auge <ra...@liferay.com>.
On Fri, Jan 25, 2019 at 10:15 AM Benjamin Edwards <ed...@gmail.com>
wrote:

> I'd also suggest that blocking the thread either by polling in a loop or
> using a dedicated timeout method is somewhat antithetical to the point of
> an asynchronous call. Better usually to do something like resolve a promise
> either with the service result or an exception after a threshold. This
> allows you to chain promises together nicely without blocking but also
> keeps things ticking along. It seems a shame that OSGi promises don't
> support a notion of cancellation (which is helpful when racing a long
> running call with a timeout).


If you have access to the deferred that the promise came from you can
"cancel" by self-resolving using:

org.osgi.util.promise.Deferred.resolveWith(Promise<? extends T>)

See org.osgi.util.promise.Promises for handy factory methods you can pair
that with to get the effect you want, a failure or success with a value.

- Ray


>
> Can't have everything I guess.
>
> Ben
>
> On Fri, 25 Jan 2019, 2:58 pm Raymond Auge, <ra...@liferay.com>
> wrote:
>
>> I'm not sure if Aries Async uses the Promise API in a substitutable way,
>> but the latest version of the Promise API has timeout methods [1].
>>
>> Perhaps Tim Ward could shed some light on this?
>> One option is simply to try to use the lastest Promise API along with
>> Aries Async.
>>
>> Sincerely,
>> - Ray
>>
>> [1]
>> https://osgi.org/specification/osgi.cmpn/7.0.0/util.promise.html#util.promise-timing.methods
>>
>> On Fri, Jan 25, 2019 at 7:07 AM Lorr, Sebastian <
>> Sebastian.Lorr@its-digital.de> wrote:
>>
>>> Hi there,
>>>
>>>
>>>
>>> I want to call a Service and wait for a given time for a response.
>>>
>>> So I did a async servicecall like this:
>>>
>>>
>>>
>>> // Create mediator, to call service asynchronously
>>>
>>> IMyService mediated = asyncService.mediate(myService, IMyService.class);
>>>
>>>
>>>
>>> // Call service and await promise to be fulfilled
>>>
>>> Promise<Result> promise = asyncService.call(mediated.process());
>>>
>>> boolean timeoutExceeded = false;
>>>
>>> long startTime = Calendar.getInstance().getTimeInMillis();
>>>
>>>
>>>
>>> serviceTimeoutMillis = 100;
>>>
>>> // Wait until timeout for promise to be done
>>>
>>> while (!promise.isDone()) {
>>>
>>>                 Thread.sleep(10);
>>>
>>>                 if (Calendar.getInstance().getTimeInMillis() >
>>> startTime + serviceTimeoutMillis) {
>>>
>>>                                timeoutExceeded = true;
>>>
>>>                                break;
>>>
>>>                 }
>>>
>>> }
>>>
>>> if (timeoutExceeded) {
>>>     throw new TimeoutException("Timeout exceeded");
>>> }
>>>
>>>
>>> Now the question: is there a better way for waiting for promise-resolve
>>> until a given timeout?
>>>
>>> Maybe some feature of Async that i missed?
>>>
>>>
>>>
>>>
>>>
>>> Thanks in advance,
>>>
>>>
>>>
>>> Sebastian
>>>
>>>
>>> ----------------------------------------------------------------
>>> Sebastian Lorr
>>> ITS Digital Solutions GmbH
>>> Dillenburger Str. 77
>>> <https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
>>>
>>> <https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
>>> D-51105 Köln
>>> <https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
>>> Tel.: +49 (0)221 820 07 0
>>> Fax : +49 (0)221 820 07 22 <%2B49%20%280%29221%20820%2007%2022>
>>> Mail: info@its-telco.de
>>> Web : http://www.its-telco.de
>>> ----------------------------------------------------------------
>>> Sitz der Gesellschaft: Dortmund
>>> Amtsgericht Dortmund, HRB 28563
>>> Geschäftsführer: Gunnar Haack, Ludger Schulte, Heinrich Toben, Raimund
>>> Schipp, Ralf Petersilka
>>> ----------------------------------------------------------------
>>>
>>> Diese E-Mail enthält vertrauliche Informationen. Wenn Sie nicht der
>>> richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben,
>>> informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail.
>>> Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist
>>> nicht gestattet.
>>>
>>> This e-mail may contain confidential information. If you are not the
>>> intended recipient (or have received this e-mail in error) please notify
>>> the sender immediately and destroy this e-mail. Any unauthorised copying,
>>> disclosure or distribution of the material in this e-mail is strictly
>>> forbidden.
>>>
>>>
>>
>> --
>> *Raymond Augé* <http://www.liferay.com/web/raymond.auge/profile>
>>  (@rotty3000)
>> Senior Software Architect *Liferay, Inc.* <http://www.liferay.com>
>>  (@Liferay)
>> Board Member & EEG Co-Chair, OSGi Alliance <http://osgi.org>
>> (@OSGiAlliance)
>>
>

-- 
*Raymond Augé* <http://www.liferay.com/web/raymond.auge/profile>
 (@rotty3000)
Senior Software Architect *Liferay, Inc.* <http://www.liferay.com>
 (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance <http://osgi.org> (@OSGiAlliance)

Re: Aries Async Service and Timeouts

Posted by Benjamin Edwards <ed...@gmail.com>.
I'd also suggest that blocking the thread either by polling in a loop or
using a dedicated timeout method is somewhat antithetical to the point of
an asynchronous call. Better usually to do something like resolve a promise
either with the service result or an exception after a threshold. This
allows you to chain promises together nicely without blocking but also
keeps things ticking along. It seems a shame that OSGi promises don't
support a notion of cancellation (which is helpful when racing a long
running call with a timeout).

Can't have everything I guess.

Ben

On Fri, 25 Jan 2019, 2:58 pm Raymond Auge, <ra...@liferay.com> wrote:

> I'm not sure if Aries Async uses the Promise API in a substitutable way,
> but the latest version of the Promise API has timeout methods [1].
>
> Perhaps Tim Ward could shed some light on this?
> One option is simply to try to use the lastest Promise API along with
> Aries Async.
>
> Sincerely,
> - Ray
>
> [1]
> https://osgi.org/specification/osgi.cmpn/7.0.0/util.promise.html#util.promise-timing.methods
>
> On Fri, Jan 25, 2019 at 7:07 AM Lorr, Sebastian <
> Sebastian.Lorr@its-digital.de> wrote:
>
>> Hi there,
>>
>>
>>
>> I want to call a Service and wait for a given time for a response.
>>
>> So I did a async servicecall like this:
>>
>>
>>
>> // Create mediator, to call service asynchronously
>>
>> IMyService mediated = asyncService.mediate(myService, IMyService.class);
>>
>>
>>
>> // Call service and await promise to be fulfilled
>>
>> Promise<Result> promise = asyncService.call(mediated.process());
>>
>> boolean timeoutExceeded = false;
>>
>> long startTime = Calendar.getInstance().getTimeInMillis();
>>
>>
>>
>> serviceTimeoutMillis = 100;
>>
>> // Wait until timeout for promise to be done
>>
>> while (!promise.isDone()) {
>>
>>                 Thread.sleep(10);
>>
>>                 if (Calendar.getInstance().getTimeInMillis() > startTime
>> + serviceTimeoutMillis) {
>>
>>                                timeoutExceeded = true;
>>
>>                                break;
>>
>>                 }
>>
>> }
>>
>> if (timeoutExceeded) {
>>     throw new TimeoutException("Timeout exceeded");
>> }
>>
>>
>> Now the question: is there a better way for waiting for promise-resolve
>> until a given timeout?
>>
>> Maybe some feature of Async that i missed?
>>
>>
>>
>>
>>
>> Thanks in advance,
>>
>>
>>
>> Sebastian
>>
>>
>> ----------------------------------------------------------------
>> Sebastian Lorr
>> ITS Digital Solutions GmbH
>> Dillenburger Str. 77
>> <https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
>>
>> <https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
>> D-51105 Köln
>> <https://maps.google.com/?q=Dillenburger+Str.+77+%0D%0A+D-51105+K%C3%B6ln&entry=gmail&source=g>
>> Tel.: +49 (0)221 820 07 0
>> Fax : +49 (0)221 820 07 22 <%2B49%20%280%29221%20820%2007%2022>
>> Mail: info@its-telco.de
>> Web : http://www.its-telco.de
>> ----------------------------------------------------------------
>> Sitz der Gesellschaft: Dortmund
>> Amtsgericht Dortmund, HRB 28563
>> Geschäftsführer: Gunnar Haack, Ludger Schulte, Heinrich Toben, Raimund
>> Schipp, Ralf Petersilka
>> ----------------------------------------------------------------
>>
>> Diese E-Mail enthält vertrauliche Informationen. Wenn Sie nicht der
>> richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben,
>> informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail.
>> Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist
>> nicht gestattet.
>>
>> This e-mail may contain confidential information. If you are not the
>> intended recipient (or have received this e-mail in error) please notify
>> the sender immediately and destroy this e-mail. Any unauthorised copying,
>> disclosure or distribution of the material in this e-mail is strictly
>> forbidden.
>>
>>
>
> --
> *Raymond Augé* <http://www.liferay.com/web/raymond.auge/profile>
>  (@rotty3000)
> Senior Software Architect *Liferay, Inc.* <http://www.liferay.com>
>  (@Liferay)
> Board Member & EEG Co-Chair, OSGi Alliance <http://osgi.org>
> (@OSGiAlliance)
>

Re: Aries Async Service and Timeouts

Posted by Raymond Auge <ra...@liferay.com>.
I'm not sure if Aries Async uses the Promise API in a substitutable way,
but the latest version of the Promise API has timeout methods [1].

Perhaps Tim Ward could shed some light on this?
One option is simply to try to use the lastest Promise API along with Aries
Async.

Sincerely,
- Ray

[1]
https://osgi.org/specification/osgi.cmpn/7.0.0/util.promise.html#util.promise-timing.methods

On Fri, Jan 25, 2019 at 7:07 AM Lorr, Sebastian <
Sebastian.Lorr@its-digital.de> wrote:

> Hi there,
>
>
>
> I want to call a Service and wait for a given time for a response.
>
> So I did a async servicecall like this:
>
>
>
> // Create mediator, to call service asynchronously
>
> IMyService mediated = asyncService.mediate(myService, IMyService.class);
>
>
>
> // Call service and await promise to be fulfilled
>
> Promise<Result> promise = asyncService.call(mediated.process());
>
> boolean timeoutExceeded = false;
>
> long startTime = Calendar.getInstance().getTimeInMillis();
>
>
>
> serviceTimeoutMillis = 100;
>
> // Wait until timeout for promise to be done
>
> while (!promise.isDone()) {
>
>                 Thread.sleep(10);
>
>                 if (Calendar.getInstance().getTimeInMillis() > startTime
> + serviceTimeoutMillis) {
>
>                                timeoutExceeded = true;
>
>                                break;
>
>                 }
>
> }
>
> if (timeoutExceeded) {
>     throw new TimeoutException("Timeout exceeded");
> }
>
>
> Now the question: is there a better way for waiting for promise-resolve
> until a given timeout?
>
> Maybe some feature of Async that i missed?
>
>
>
>
>
> Thanks in advance,
>
>
>
> Sebastian
>
>
> ----------------------------------------------------------------
> Sebastian Lorr
> ITS Digital Solutions GmbH
> Dillenburger Str. 77
> D-51105 Köln
> Tel.: +49 (0)221 820 07 0
> Fax : +49 (0)221 820 07 22 <%2B49%20%280%29221%20820%2007%2022>
> Mail: info@its-telco.de
> Web : http://www.its-telco.de
> ----------------------------------------------------------------
> Sitz der Gesellschaft: Dortmund
> Amtsgericht Dortmund, HRB 28563
> Geschäftsführer: Gunnar Haack, Ludger Schulte, Heinrich Toben, Raimund
> Schipp, Ralf Petersilka
> ----------------------------------------------------------------
>
> Diese E-Mail enthält vertrauliche Informationen. Wenn Sie nicht der
> richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben,
> informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail.
> Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist
> nicht gestattet.
>
> This e-mail may contain confidential information. If you are not the
> intended recipient (or have received this e-mail in error) please notify
> the sender immediately and destroy this e-mail. Any unauthorised copying,
> disclosure or distribution of the material in this e-mail is strictly
> forbidden.
>
>

-- 
*Raymond Augé* <http://www.liferay.com/web/raymond.auge/profile>
 (@rotty3000)
Senior Software Architect *Liferay, Inc.* <http://www.liferay.com>
 (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance <http://osgi.org> (@OSGiAlliance)