Re: [osgi-dev] Integrating promises

2018-09-20 Thread Alain Picard via osgi-dev
Awesome, thanks!

Alain


On Thu, Sep 20, 2018 at 3:30 AM Tim Ward  wrote:

> Ah, you wanted it to be the other way around (*ActionListener -> Promise*
> not *Promise -> ActionListener*)!
>
> What you have looks good, although if you use Promises 1.1 then you can do
> some optimisation with the threads if you like.
>
> // The promises/deferreds produced by this factory will run callbacks by
> “borrowing" the thread that resolves the promise
> PromiseFactory pf = new PromiseFactory(PromiseFactory.inlineExecutor());
>
> Deferred d = pf.deferred();
>
> Or, if you want to use your own thread pool then you can.
>
> // The promises/deferreds produced by this factory will run callbacks on
> the threads from the thread pool
> PromiseFactory pf = new PromiseFactory(Executors.newFixedThreadPool(8));
>
> Deferred d = pf.deferred();
>
>
>
> Tim
>
> On 19 Sep 2018, at 21:15, Alain Picard  wrote:
>
> Well I should have looked at this more closely. It's now working.
>
> I have:
> ActionListenerPromise promListener = new
> ActionListenerPromise<>();
>client.updateAsync(request, RequestOptions.DEFAULT, promListener);
> //ES call
>return promListener.getPromise();
>
> and my listener class:
> public class ActionListenerPromise implements ActionListener {
> private Deferred deferred = new Deferred<>();
>
> public ActionListenerPromise() {
> this.deferred = new Deferred<>();
> }
>
> public Promise getPromise() {
> return deferred.getPromise();
> }
>
> @Override
> public void onResponse(R response) {
> deferred.resolve(response);
> }
>
> @Override
> public void onFailure(Exception e) {
> deferred.fail(e);
> }
> }
>
> Alain
>
>
> On Wed, Sep 19, 2018 at 12:16 PM Alain Picard 
> wrote:
>
>> Tim,
>>
>> I think the secret here is the getPromise() or how to get a promise
>> implementation from the ActionListener or one of its derivative static
>> methods. Normally we use a Deferred, but here my understanding is that ES
>> manages the async execution and we want to wrap its deferred handling into
>> a Promise which is not executed by our PromiseFactory. I guess that's where
>> my limited knowledge is hitting a wall.
>>
>> Cheers,
>> Alain
>>
>>
>> On Wed, Sep 19, 2018 at 11:13 AM Tim Ward  wrote:
>>
>>> It looks like it should be pretty simple…
>>>
>>> Promise myPromise = getPromise();
>>>
>>> myPromise.onSuccess(listener::onResponse)
>>> .onFailure(listener::onFailure);
>>>
>>>
>>> Best Regards,
>>>
>>> Tim
>>>
>>> On 19 Sep 2018, at 15:16, Alain Picard via osgi-dev <
>>> osgi-dev@mail.osgi.org> wrote:
>>>
>>> We are using ElasticSearch which provide an async mode that is heavily
>>> based on promises, They even provide BiConsumer to integrate with
>>> CompletableFuture.
>>>
>>> The interface is ActionListener (
>>> https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/action/ActionListener.java
>>> ).
>>>
>>> What is the best way to tie this is to promises instead, so that we
>>> don't have to deal with different mode of handling asynchronous processing,
>>> and we are also envisioning the possibility of integrating push streams
>>> here as well.
>>>
>>> Thanks
>>> Alain
>>>
>>> ___
>>> OSGi Developer Mail List
>>> osgi-dev@mail.osgi.org
>>> https://mail.osgi.org/mailman/listinfo/osgi-dev
>>>
>>>
>>>
>
___
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Re: [osgi-dev] Integrating promises

2018-09-19 Thread Alain Picard via osgi-dev
Well I should have looked at this more closely. It's now working.

I have:
ActionListenerPromise promListener = new
ActionListenerPromise<>();
   client.updateAsync(request, RequestOptions.DEFAULT, promListener);  //ES
call
   return promListener.getPromise();

and my listener class:
public class ActionListenerPromise implements ActionListener {
private Deferred deferred = new Deferred<>();

public ActionListenerPromise() {
this.deferred = new Deferred<>();
}

public Promise getPromise() {
return deferred.getPromise();
}

@Override
public void onResponse(R response) {
deferred.resolve(response);
}

@Override
public void onFailure(Exception e) {
deferred.fail(e);
}
}

Alain


On Wed, Sep 19, 2018 at 12:16 PM Alain Picard  wrote:

> Tim,
>
> I think the secret here is the getPromise() or how to get a promise
> implementation from the ActionListener or one of its derivative static
> methods. Normally we use a Deferred, but here my understanding is that ES
> manages the async execution and we want to wrap its deferred handling into
> a Promise which is not executed by our PromiseFactory. I guess that's where
> my limited knowledge is hitting a wall.
>
> Cheers,
> Alain
>
>
> On Wed, Sep 19, 2018 at 11:13 AM Tim Ward  wrote:
>
>> It looks like it should be pretty simple…
>>
>> Promise myPromise = getPromise();
>>
>> myPromise.onSuccess(listener::onResponse)
>> .onFailure(listener::onFailure);
>>
>>
>> Best Regards,
>>
>> Tim
>>
>> On 19 Sep 2018, at 15:16, Alain Picard via osgi-dev <
>> osgi-dev@mail.osgi.org> wrote:
>>
>> We are using ElasticSearch which provide an async mode that is heavily
>> based on promises, They even provide BiConsumer to integrate with
>> CompletableFuture.
>>
>> The interface is ActionListener (
>> https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/action/ActionListener.java
>> ).
>>
>> What is the best way to tie this is to promises instead, so that we don't
>> have to deal with different mode of handling asynchronous processing, and
>> we are also envisioning the possibility of integrating push streams here as
>> well.
>>
>> Thanks
>> Alain
>>
>> ___
>> OSGi Developer Mail List
>> osgi-dev@mail.osgi.org
>> https://mail.osgi.org/mailman/listinfo/osgi-dev
>>
>>
>>
___
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Re: [osgi-dev] Integrating promises

2018-09-19 Thread Alain Picard via osgi-dev
Tim,

I think the secret here is the getPromise() or how to get a promise
implementation from the ActionListener or one of its derivative static
methods. Normally we use a Deferred, but here my understanding is that ES
manages the async execution and we want to wrap its deferred handling into
a Promise which is not executed by our PromiseFactory. I guess that's where
my limited knowledge is hitting a wall.

Cheers,
Alain


On Wed, Sep 19, 2018 at 11:13 AM Tim Ward  wrote:

> It looks like it should be pretty simple…
>
> Promise myPromise = getPromise();
>
> myPromise.onSuccess(listener::onResponse)
> .onFailure(listener::onFailure);
>
>
> Best Regards,
>
> Tim
>
> On 19 Sep 2018, at 15:16, Alain Picard via osgi-dev <
> osgi-dev@mail.osgi.org> wrote:
>
> We are using ElasticSearch which provide an async mode that is heavily
> based on promises, They even provide BiConsumer to integrate with
> CompletableFuture.
>
> The interface is ActionListener (
> https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/action/ActionListener.java
> ).
>
> What is the best way to tie this is to promises instead, so that we don't
> have to deal with different mode of handling asynchronous processing, and
> we are also envisioning the possibility of integrating push streams here as
> well.
>
> Thanks
> Alain
>
> ___
> OSGi Developer Mail List
> osgi-dev@mail.osgi.org
> https://mail.osgi.org/mailman/listinfo/osgi-dev
>
>
>
___
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Re: [osgi-dev] Integrating promises

2018-09-19 Thread Tim Ward via osgi-dev
It looks like it should be pretty simple…

Promise myPromise = getPromise();

myPromise.onSuccess(listener::onResponse)
.onFailure(listener::onFailure);

Best Regards,

Tim

> On 19 Sep 2018, at 15:16, Alain Picard via osgi-dev  
> wrote:
> 
> We are using ElasticSearch which provide an async mode that is heavily based 
> on promises, They even provide BiConsumer to integrate with CompletableFuture.
> 
> The interface is ActionListener 
> (https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/action/ActionListener.java
>  
> ).
> 
> What is the best way to tie this is to promises instead, so that we don't 
> have to deal with different mode of handling asynchronous processing, and we 
> are also envisioning the possibility of integrating push streams here as well.
> 
> Thanks
> Alain
> 
> ___
> OSGi Developer Mail List
> osgi-dev@mail.osgi.org
> https://mail.osgi.org/mailman/listinfo/osgi-dev

___
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

[osgi-dev] Integrating promises

2018-09-19 Thread Alain Picard via osgi-dev
We are using ElasticSearch which provide an async mode that is heavily
based on promises, They even provide BiConsumer to integrate with
CompletableFuture.

The interface is ActionListener (
https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/action/ActionListener.java
).

What is the best way to tie this is to promises instead, so that we don't
have to deal with different mode of handling asynchronous processing, and
we are also envisioning the possibility of integrating push streams here as
well.

Thanks
Alain
___
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev