You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Marco Zapata <mr...@gmail.com> on 2013/05/14 18:21:12 UTC

Help with REST Async Service

Hello,

I'm currently trying to implement an async REST  service,  but I'm having
trouble getting it to work.

I was using the "@UseAsyncMethod", but the when I call the service from the
AsyncClient, the service only does the synchronous logic.

I was wondering if you have and example on How to the the async service on
REST


My service look like

@UseAsyncMethod
@Override
public Device getDevice(int orderID) {
 LOG.info("Executing operation getOrder synchronously");
Order o = new Order();
  //ORder Logic
return o;
}


public Future<?> getOrderAsync( final int  orderID) {
 LOG.info("Executing operation greetMeSometimeAsync asynchronously\n");
final AsyncHandler<Order > asyncHandler = new AsyncHandler<Order >() {

@Override
public void handleResponse(Response<Order > res) {
// TODO Auto-generated method stub
LOG.info("handling");
}
};
 final ServerAsyncResponse<Device> r = new ServerAsyncResponse<Order >();
new Thread() {
public void run() {
Order resp = new Order ();
resp.setId(1);
resp.setName("Im async");
  r.set(resp);
 try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
 LOG.info("Responding on background thread\n");
asyncHandler.handleResponse(r);
}
}.start();

return r;
}


Thanks in advance for you help.

Regards

Marco Zapata

Re: Help with REST Async Service

Posted by Marco Zapata <mr...@gmail.com>.
Thanks a lot for your Sergey, I managed to implement the service with your
Help!

Regards.


2013/5/14 Sergey Beryozkin <sb...@gmail.com>

> Hi
>
> On 14/05/13 17:21, Marco Zapata wrote:
>
>> Hello,
>>
>> I'm currently trying to implement an async REST  service,  but I'm having
>> trouble getting it to work.
>>
>> I was using the "@UseAsyncMethod", but the when I call the service from
>> the
>> AsyncClient, the service only does the synchronous logic.
>>
>
> @UseAsyncMethod is a CXF specific annotation which is not recognized yet
> by the JAX-RS runtime, please watch:
>
> https://issues.apache.org/**jira/browse/CXF-4995<https://issues.apache.org/jira/browse/CXF-4995>
>
> We will get it supported for JAX-RS server/client runtimes as well.
>
> That said, please consider using JAX-RS 2.0 AsyncResponse,
>
> https://jax-rs-spec.java.net/**nonav/2.0-SNAPSHOT/apidocs/**
> javax/ws/rs/container/**AsyncResponse.html<https://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/javax/ws/rs/container/AsyncResponse.html>
>
> see the test server code here:
> http://svn.apache.org/repos/**asf/cxf/trunk/systests/jaxrs/**
> src/test/java/org/apache/cxf/**systest/jaxrs/**BookContinuationStore.java<http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java>
>
> (supported from CXF 2.7.x & on the trunk).
>
> I also recommend using CXF WebClient to JAX-RS 2.0 AsyncInvoker bridge,
> do WebClient.create("http://**someaddress <http://someaddress>").async()
> and start using AsyncInvoker:
>
> https://jax-rs-spec.java.net/**nonav/2.0-SNAPSHOT/apidocs/**
> javax/ws/rs/client/**AsyncInvoker.html<https://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/javax/ws/rs/client/AsyncInvoker.html>
>
> 2.0 Client API will be completely supported for CXF 3.0
>
> HTH, Sergey
>
>
>
>> I was wondering if you have and example on How to the the async service on
>> REST
>>
>>
>> My service look like
>>
>> @UseAsyncMethod
>> @Override
>> public Device getDevice(int orderID) {
>>   LOG.info("Executing operation getOrder synchronously");
>> Order o = new Order();
>>    //ORder Logic
>> return o;
>> }
>>
>>
>> public Future<?>  getOrderAsync( final int  orderID) {
>>   LOG.info("Executing operation greetMeSometimeAsync asynchronously\n");
>> final AsyncHandler<Order>  asyncHandler = new AsyncHandler<Order>() {
>>
>> @Override
>> public void handleResponse(Response<Order>  res) {
>> // TODO Auto-generated method stub
>> LOG.info("handling");
>> }
>> };
>>   final ServerAsyncResponse<Device>  r = new ServerAsyncResponse<Order>();
>> new Thread() {
>> public void run() {
>> Order resp = new Order ();
>> resp.setId(1);
>> resp.setName("Im async");
>>    r.set(resp);
>>   try {
>> Thread.sleep(300);
>> } catch (InterruptedException e) {
>> e.printStackTrace();
>> }
>>   LOG.info("Responding on background thread\n");
>> asyncHandler.handleResponse(r)**;
>> }
>> }.start();
>>
>> return r;
>> }
>>
>>
>> Thanks in advance for you help.
>>
>> Regards
>>
>> Marco Zapata
>>
>>
>
>

Re: Help with REST Async Service

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 14/05/13 17:21, Marco Zapata wrote:
> Hello,
>
> I'm currently trying to implement an async REST  service,  but I'm having
> trouble getting it to work.
>
> I was using the "@UseAsyncMethod", but the when I call the service from the
> AsyncClient, the service only does the synchronous logic.

@UseAsyncMethod is a CXF specific annotation which is not recognized yet 
by the JAX-RS runtime, please watch:

https://issues.apache.org/jira/browse/CXF-4995

We will get it supported for JAX-RS server/client runtimes as well.

That said, please consider using JAX-RS 2.0 AsyncResponse,

https://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/javax/ws/rs/container/AsyncResponse.html

see the test server code here:
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java

(supported from CXF 2.7.x & on the trunk).

I also recommend using CXF WebClient to JAX-RS 2.0 AsyncInvoker bridge,
do WebClient.create("http://someaddress").async() and start using 
AsyncInvoker:

https://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/javax/ws/rs/client/AsyncInvoker.html

2.0 Client API will be completely supported for CXF 3.0

HTH, Sergey

>
> I was wondering if you have and example on How to the the async service on
> REST
>
>
> My service look like
>
> @UseAsyncMethod
> @Override
> public Device getDevice(int orderID) {
>   LOG.info("Executing operation getOrder synchronously");
> Order o = new Order();
>    //ORder Logic
> return o;
> }
>
>
> public Future<?>  getOrderAsync( final int  orderID) {
>   LOG.info("Executing operation greetMeSometimeAsync asynchronously\n");
> final AsyncHandler<Order>  asyncHandler = new AsyncHandler<Order>() {
>
> @Override
> public void handleResponse(Response<Order>  res) {
> // TODO Auto-generated method stub
> LOG.info("handling");
> }
> };
>   final ServerAsyncResponse<Device>  r = new ServerAsyncResponse<Order>();
> new Thread() {
> public void run() {
> Order resp = new Order ();
> resp.setId(1);
> resp.setName("Im async");
>    r.set(resp);
>   try {
> Thread.sleep(300);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
>   LOG.info("Responding on background thread\n");
> asyncHandler.handleResponse(r);
> }
> }.start();
>
> return r;
> }
>
>
> Thanks in advance for you help.
>
> Regards
>
> Marco Zapata
>