Search in sources :

Example 1 with AsyncInvoker

use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.

the class GenericResponseTest method testAsyncPost.

@Test
public void testAsyncPost() throws ExecutionException, InterruptedException {
    GenericType<Response> generic = new GenericType<Response>(Response.class);
    Entity entity = Entity.entity("entity", MediaType.WILDCARD_TYPE);
    WebTarget target = target("resource");
    final AsyncInvoker async = target.request().async();
    Response response = async.post(entity, generic).get();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("entity", response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) Entity(javax.ws.rs.client.Entity) GenericType(javax.ws.rs.core.GenericType) AsyncInvoker(javax.ws.rs.client.AsyncInvoker) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 2 with AsyncInvoker

use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.

the class GenericResponseTest method testAsyncGet.

@Test
public void testAsyncGet() throws ExecutionException, InterruptedException {
    GenericType<Response> generic = new GenericType<Response>(Response.class);
    WebTarget target = target("resource");
    final AsyncInvoker async = target.request().async();
    Response response = async.get(generic).get();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("get", response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) AsyncInvoker(javax.ws.rs.client.AsyncInvoker) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 3 with AsyncInvoker

use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.

the class BasicClientTest method runCustomExecutorTestAsync.

private void runCustomExecutorTestAsync(Client client) throws InterruptedException, ExecutionException {
    AsyncInvoker async = client.target(getBaseUri()).path("resource").request().async();
    Future<Response> f1 = async.post(text("post"));
    final Response response = f1.get();
    final String entity = response.readEntity(String.class);
    assertEquals("custom-async-request-post", entity);
}
Also used : Response(javax.ws.rs.core.Response) AsyncInvoker(javax.ws.rs.client.AsyncInvoker)

Example 4 with AsyncInvoker

use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.

the class AsyncTest method testClientThreadPool.

@Test
@Ignore("Unstable test.")
public void testClientThreadPool() throws Exception {
    final AsyncInvoker invoker = ClientBuilder.newClient(new ClientConfig().property(ClientProperties.ASYNC_THREADPOOL_SIZE, 9)).target(getBaseUri()).path("threadpool").request().async();
    final CountDownLatch latch = new CountDownLatch(100);
    final int threadCount = Thread.activeCount();
    final List<Thread> threads = new ArrayList<Thread>(20);
    for (int i = 0; i < 20; i++) {
        threads.add(new Thread(new Runnable() {

            @Override
            public void run() throws RuntimeException {
                for (int i = 0; i < 5; i++) {
                    try {
                        assertThat(invoker.get().get().readEntity(String.class), equalTo("GET"));
                        assertThat(Thread.activeCount() - threadCount - 20, lessThanOrEqualTo(10));
                        latch.countDown();
                    } catch (final InterruptedException e) {
                        fail();
                    } catch (final ExecutionException e) {
                        fail();
                    }
                }
            }
        }));
    }
    for (final Thread thread : threads) {
        thread.start();
    }
    assertTrue(latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
}
Also used : AsyncInvoker(javax.ws.rs.client.AsyncInvoker) ArrayList(java.util.ArrayList) ClientConfig(org.glassfish.jersey.client.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Ignore(org.junit.Ignore) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 5 with AsyncInvoker

use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.

the class JerseyInvocationTest method runtimeExceptionInAsyncInvocation.

@Test
public void runtimeExceptionInAsyncInvocation() throws ExecutionException, InterruptedException {
    final AsyncInvoker ai = ClientBuilder.newClient().register(new ExceptionInvokerFilter()).target("http://localhost:888/").request().async();
    try {
        ai.get().get();
        fail("ExecutionException should be thrown");
    } catch (ExecutionException ee) {
        assertEquals(ProcessingException.class, ee.getCause().getClass());
        assertEquals(RuntimeException.class, ee.getCause().getCause().getClass());
    }
}
Also used : AsyncInvoker(javax.ws.rs.client.AsyncInvoker) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Aggregations

AsyncInvoker (javax.ws.rs.client.AsyncInvoker)9 Test (org.junit.Test)8 JerseyTest (org.glassfish.jersey.test.JerseyTest)7 Response (javax.ws.rs.core.Response)4 ExecutionException (java.util.concurrent.ExecutionException)2 WebTarget (javax.ws.rs.client.WebTarget)2 GenericType (javax.ws.rs.core.GenericType)2 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ProcessingException (javax.ws.rs.ProcessingException)1 Entity (javax.ws.rs.client.Entity)1 ClientConfig (org.glassfish.jersey.client.ClientConfig)1 Ignore (org.junit.Ignore)1