Aries Async Service and Timeouts

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Aries Async Service and Timeouts

Lorr, Sebastian
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 : <a href="tel:%2B49%20%280%29221%20820%2007%2022" value="&#43;492218200722" target="_blank" id="LPNoLP" style="color:rgb(17,85,204)">+49 (0)221 820 07 22
----------------------------------------------------------------
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.

Reply | Threaded
Open this post in threaded view
|

Re: Aries Async Service and Timeouts

Raymond Auge
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


On Fri, Jan 25, 2019 at 7:07 AM Lorr, Sebastian <[hidden email]> 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 : <a href="tel:%2B49%20%280%29221%20820%2007%2022" value="+492218200722" id="gmail-m_6660345317421951817LPNoLP" style="color:rgb(17,85,204)" target="_blank">+49 (0)221 820 07 22
----------------------------------------------------------------
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é (@rotty3000)
Senior Software Architect Liferay, Inc. (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance (@OSGiAlliance)
Reply | Threaded
Open this post in threaded view
|

Re: Aries Async Service and Timeouts

Benjamin Edwards
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, <[hidden email]> 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


On Fri, Jan 25, 2019 at 7:07 AM Lorr, Sebastian <[hidden email]> 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
Tel.: +49 (0)221 820 07 0
Fax : <a href="tel:%2B49%20%280%29221%20820%2007%2022" value="+492218200722" id="m_7646808294985369756gmail-m_6660345317421951817LPNoLP" style="color:rgb(17,85,204)" target="_blank">+49 (0)221 820 07 22
----------------------------------------------------------------
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é (@rotty3000)
Senior Software Architect Liferay, Inc. (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance (@OSGiAlliance)
Reply | Threaded
Open this post in threaded view
|

Re: Aries Async Service and Timeouts

Raymond Auge


On Fri, Jan 25, 2019 at 10:15 AM Benjamin Edwards <[hidden email]> 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, <[hidden email]> 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


On Fri, Jan 25, 2019 at 7:07 AM Lorr, Sebastian <[hidden email]> 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
Tel.: +49 (0)221 820 07 0
Fax : <a href="tel:%2B49%20%280%29221%20820%2007%2022" value="+492218200722" id="gmail-m_-1641366967820363751m_7646808294985369756gmail-m_6660345317421951817LPNoLP" style="color:rgb(17,85,204)" target="_blank">+49 (0)221 820 07 22
----------------------------------------------------------------
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é (@rotty3000)
Senior Software Architect Liferay, Inc. (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance (@OSGiAlliance)


--
Raymond Augé (@rotty3000)
Senior Software Architect Liferay, Inc. (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance (@OSGiAlliance)
Reply | Threaded
Open this post in threaded view
|

Re: Aries Async Service and Timeouts

Timothy Ward-2

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 <[hidden email]> wrote:



On Fri, Jan 25, 2019 at 10:15 AM Benjamin Edwards <[hidden email]> 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, <[hidden email]> 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


On Fri, Jan 25, 2019 at 7:07 AM Lorr, Sebastian <[hidden email]> 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
<a href="https://maps.google.com/?q=Dillenburger&#43;Str.&#43;77&#43;%0D%0A&#43;D-51105&#43;K%C3%B6ln&amp;entry=gmail&amp;source=g" target="_blank" class="">Dillenburger Str. 77
<a href="https://maps.google.com/?q=Dillenburger&#43;Str.&#43;77&#43;%0D%0A&#43;D-51105&#43;K%C3%B6ln&amp;entry=gmail&amp;source=g" target="_blank" class="">
<a href="https://maps.google.com/?q=Dillenburger&#43;Str.&#43;77&#43;%0D%0A&#43;D-51105&#43;K%C3%B6ln&amp;entry=gmail&amp;source=g" target="_blank" class="">D-51105 Köln
Tel.: +49 (0)221 820 07 0
Fax : <a href="tel:%2B49%20%280%29221%20820%2007%2022" value="&#43;492218200722" id="gmail-m_-1641366967820363751m_7646808294985369756gmail-m_6660345317421951817LPNoLP" target="_blank" style="color: rgb(17, 85, 204);" class="">+49 (0)221 820 07 22
----------------------------------------------------------------
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é (@rotty3000)
Senior Software Architect Liferay, Inc. (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance (@OSGiAlliance)


-- 
Raymond Augé (@rotty3000)
Senior Software Architect Liferay, Inc. (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance (@OSGiAlliance)