Search in sources :

Example 1 with TimeoutHandler

use of javax.ws.rs.container.TimeoutHandler in project javaee7-samples by javaee-samples.

the class MyResource method getList.

//    @Resource(name = "DefaultManagedThreadFactory")
//    ManagedThreadFactory threadFactory;
@GET
public void getList(@Suspended final AsyncResponse ar) throws NamingException {
    ar.setTimeoutHandler(new TimeoutHandler() {

        @Override
        public void handleTimeout(AsyncResponse ar) {
            ar.resume("Operation timed out");
        }
    });
    ar.setTimeout(4000, TimeUnit.MILLISECONDS);
    ar.register(new MyCompletionCallback());
    ar.register(new MyConnectionCallback());
    ManagedThreadFactory threadFactory = (ManagedThreadFactory) new InitialContext().lookup("java:comp/DefaultManagedThreadFactory");
    Executors.newSingleThreadExecutor(threadFactory).submit(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(3000);
                ar.resume(response[0]);
            } catch (InterruptedException ex) {
            }
        }
    });
}
Also used : TimeoutHandler(javax.ws.rs.container.TimeoutHandler) AsyncResponse(javax.ws.rs.container.AsyncResponse) InitialContext(javax.naming.InitialContext) ManagedThreadFactory(javax.enterprise.concurrent.ManagedThreadFactory) GET(javax.ws.rs.GET)

Example 2 with TimeoutHandler

use of javax.ws.rs.container.TimeoutHandler in project jersey by jersey.

the class AsyncCancelTimeoutResource method setTimeout.

@POST
@Path("timeout")
public void setTimeout(final String stage) throws Exception {
    final AsyncResponse async = stages[Integer.parseInt(stage)].take();
    async.setTimeoutHandler(new TimeoutHandler() {

        @Override
        public void handleTimeout(final AsyncResponse response) {
            response.cancel();
        }
    });
    async.setTimeout(200L, TimeUnit.MILLISECONDS);
    stages[Integer.parseInt(stage) + 1].add(async);
}
Also used : TimeoutHandler(javax.ws.rs.container.TimeoutHandler) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 3 with TimeoutHandler

use of javax.ws.rs.container.TimeoutHandler in project jersey by jersey.

the class AsyncResumeTimeoutResource method setTimeOut.

@POST
@Path("timeout")
public void setTimeOut(final Integer millis) throws InterruptedException {
    final AsyncResponse asyncResponse = queue.take();
    final boolean timeout1 = asyncResponse.setTimeout(millis, TimeUnit.MILLISECONDS);
    asyncResponse.setTimeoutHandler(new TimeoutHandler() {

        @Override
        public void handleTimeout(final AsyncResponse asyncResponse) {
            final boolean timeout2 = asyncResponse.setTimeout(millis, TimeUnit.MILLISECONDS);
            asyncResponse.setTimeoutHandler(new TimeoutHandler() {

                @Override
                public void handleTimeout(final AsyncResponse asyncResponse) {
                    asyncResponse.resume("timeout1=" + timeout1 + "_timeout2=" + timeout2 + "_handled");
                }
            });
        }
    });
}
Also used : TimeoutHandler(javax.ws.rs.container.TimeoutHandler) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 4 with TimeoutHandler

use of javax.ws.rs.container.TimeoutHandler in project jersey by jersey.

the class AsyncTimeoutResource method setTimeOut.

@POST
@Path("timeout")
public void setTimeOut(final Integer millis) throws InterruptedException {
    final AsyncResponse asyncResponse = queue.take();
    final boolean timeout1 = asyncResponse.setTimeout(millis, TimeUnit.MILLISECONDS);
    asyncResponse.setTimeoutHandler(new TimeoutHandler() {

        @Override
        public void handleTimeout(final AsyncResponse asyncResponse) {
            final boolean timeout2 = asyncResponse.setTimeout(millis, TimeUnit.MILLISECONDS);
            asyncResponse.setTimeoutHandler(new TimeoutHandler() {

                @Override
                public void handleTimeout(final AsyncResponse asyncResponse) {
                    asyncResponse.resume("timeout1=" + timeout1 + "_timeout2=" + timeout2 + "_handled");
                    asyncResponse.cancel();
                }
            });
        }
    });
}
Also used : TimeoutHandler(javax.ws.rs.container.TimeoutHandler) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

AsyncResponse (javax.ws.rs.container.AsyncResponse)4 TimeoutHandler (javax.ws.rs.container.TimeoutHandler)4 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 ManagedThreadFactory (javax.enterprise.concurrent.ManagedThreadFactory)1 InitialContext (javax.naming.InitialContext)1 GET (javax.ws.rs.GET)1