You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Paul Carter-Brown <pa...@jini.guru> on 2019/10/18 17:21:36 UTC

Question on @Suspended AsyncResponse

Hi,

In this code sample, why is it that it takes 3s for the "OK" response to
get back to the client.
My expectation would be that the response would go back immediately?

@Path("/abc")
@Stateless
public class BasicResource {

    @PersistenceContext()
    private EntityManager em;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public void test(@Suspended final AsyncResponse asyncResponse) {
        // Want OK to get to the client immediately
        asyncResponse.resume("OK");
        try {
            // Some long running JDBC transactional stuff to run in JTA
container managed transaction using injected em
            // That must commit when done
            Thread.sleep(3000);
        } catch (Exception e) {
        }
    }
}

Paul

Re: Question on @Suspended AsyncResponse

Posted by Paul Carter-Brown <pa...@jini.guru>.
Hi David,

Neither of these return immediately:

@Path("/abc")
@Stateless
@javax.ejb.TransactionAttribute(TransactionAttributeType.NEVER)
public class BasicResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public void test(@Suspended final AsyncResponse asyncResponse) {
        asyncResponse.resume(Response.ok("Hello world").build());
        sleep();
    }
    private void sleep() {
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
        }
    }
}

@Path("/abc")
public class BasicResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public void test(@Suspended final AsyncResponse asyncResponse) {
        asyncResponse.resume(Response.ok("Hello world").build());
        sleep();
    }
    private void sleep() {
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
        }
    }
}

Paul


On Sat, Oct 19, 2019 at 1:23 AM David Jencks <da...@gmail.com>
wrote:

> IIUC this method is running inside the container managed transaction?
> Does it send the response immediately if it’s not in a container managed
> transaction?
>
> david jencks
>
> > On Oct 18, 2019, at 10:21 AM, Paul Carter-Brown
> <pa...@jini.guru> wrote:
> >
> > Hi,
> >
> > In this code sample, why is it that it takes 3s for the "OK" response to
> > get back to the client.
> > My expectation would be that the response would go back immediately?
> >
> > @Path("/abc")
> > @Stateless
> > public class BasicResource {
> >
> >    @PersistenceContext()
> >    private EntityManager em;
> >
> >    @GET
> >    @Produces(MediaType.TEXT_PLAIN)
> >    public void test(@Suspended final AsyncResponse asyncResponse) {
> >        // Want OK to get to the client immediately
> >        asyncResponse.resume("OK");
> >        try {
> >            // Some long running JDBC transactional stuff to run in JTA
> > container managed transaction using injected em
> >            // That must commit when done
> >            Thread.sleep(3000);
> >        } catch (Exception e) {
> >        }
> >    }
> > }
> >
> > Paul
>
>

Re: Question on @Suspended AsyncResponse

Posted by David Jencks <da...@gmail.com>.
IIUC this method is running inside the container managed transaction?  Does it send the response immediately if it’s not in a container managed transaction?

david jencks

> On Oct 18, 2019, at 10:21 AM, Paul Carter-Brown <pa...@jini.guru> wrote:
> 
> Hi,
> 
> In this code sample, why is it that it takes 3s for the "OK" response to
> get back to the client.
> My expectation would be that the response would go back immediately?
> 
> @Path("/abc")
> @Stateless
> public class BasicResource {
> 
>    @PersistenceContext()
>    private EntityManager em;
> 
>    @GET
>    @Produces(MediaType.TEXT_PLAIN)
>    public void test(@Suspended final AsyncResponse asyncResponse) {
>        // Want OK to get to the client immediately
>        asyncResponse.resume("OK");
>        try {
>            // Some long running JDBC transactional stuff to run in JTA
> container managed transaction using injected em
>            // That must commit when done
>            Thread.sleep(3000);
>        } catch (Exception e) {
>        }
>    }
> }
> 
> Paul