Search in sources :

Example 11 with JerseyProcessingUncaughtExceptionHandler

use of org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler in project jersey by jersey.

the class RxFlowableTest method setUp.

@Before
public void setUp() throws Exception {
    client = ClientBuilder.newClient().register(TerminalClientRequestFilter.class);
    client.register(RxFlowableInvokerProvider.class);
    executor = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder().setNameFormat("jersey-rx-client-test-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build());
}
Also used : JerseyProcessingUncaughtExceptionHandler(org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadFactoryBuilder(org.glassfish.jersey.internal.guava.ThreadFactoryBuilder) Before(org.junit.Before)

Example 12 with JerseyProcessingUncaughtExceptionHandler

use of org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler in project jersey by jersey.

the class AsyncServletResourceTest method testAsyncServlet.

/**
     * Test asynchronous servlet-deployed resource.
     *
     * @throws InterruptedException in case the waiting for all requests to complete was interrupted.
     */
@Test
public void testAsyncServlet() throws InterruptedException {
    final WebTarget resourceTarget = target("async");
    resourceTarget.register(LoggingFeature.class);
    final String expectedResponse = AsyncServletResource.HELLO_ASYNC_WORLD;
    final int MAX_MESSAGES = 50;
    final int LATCH_WAIT_TIMEOUT = 10;
    final boolean debugMode = false;
    final boolean sequentialGet = false;
    final Object sequentialGetLock = new Object();
    final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("async-resource-test-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build());
    final Map<Integer, ResponseRecord> getResponses = new ConcurrentHashMap<>();
    final CountDownLatch getRequestLatch = new CountDownLatch(MAX_MESSAGES);
    try {
        for (int i = 0; i < MAX_MESSAGES; i++) {
            final int requestId = i;
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    //noinspection PointlessBooleanExpression,ConstantConditions
                    if (debugMode || sequentialGet) {
                        synchronized (sequentialGetLock) {
                            get();
                        }
                    } else {
                        get();
                    }
                }

                private void get() {
                    try {
                        final Response response = resourceTarget.request().get();
                        getResponses.put(requestId, new ResponseRecord(response.getStatus(), response.readEntity(String.class)));
                    } catch (final Throwable t) {
                        t.printStackTrace();
                    } finally {
                        getRequestLatch.countDown();
                    }
                }
            });
        }
        //noinspection ConstantConditions
        if (debugMode) {
            getRequestLatch.await();
        } else {
            assertTrue("Waiting for all GET requests to complete has timed out.", getRequestLatch.await(LATCH_WAIT_TIMEOUT * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
        }
    } finally {
        executor.shutdownNow();
    }
    final StringBuilder messageBuilder = new StringBuilder();
    for (final Map.Entry<Integer, ResponseRecord> getResponseEntry : getResponses.entrySet()) {
        messageBuilder.append("GET response for message ").append(getResponseEntry.getKey()).append(": ").append(getResponseEntry.getValue().toString()).append('\n');
    }
    LOGGER.info(messageBuilder.toString());
    assertEquals(MAX_MESSAGES, getResponses.size());
    for (final Map.Entry<Integer, ResponseRecord> entry : getResponses.entrySet()) {
        assertEquals("Unexpected GET response status for request " + entry.getKey(), 200, entry.getValue().status);
        assertEquals("Unexpected GET response message for request " + entry.getKey(), expectedResponse, entry.getValue().message);
    }
}
Also used : JerseyProcessingUncaughtExceptionHandler(org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Response(javax.ws.rs.core.Response) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(org.glassfish.jersey.internal.guava.ThreadFactoryBuilder) WebTarget(javax.ws.rs.client.WebTarget) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 13 with JerseyProcessingUncaughtExceptionHandler

use of org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler in project jersey by jersey.

the class AsyncServletResourceITCase method testAsyncServlet.

/**
     * Test asynchronous servlet-deployed resource.
     *
     * @throws InterruptedException in case the waiting for all requests to complete was interrupted.
     */
@Test
public void testAsyncServlet() throws InterruptedException {
    final WebTarget resourceTarget = target("async");
    resourceTarget.register(LoggingFeature.class);
    final String expectedResponse = AsyncServletResource.HELLO_ASYNC_WORLD;
    final int MAX_MESSAGES = 50;
    final int LATCH_WAIT_TIMEOUT = 10 * getAsyncTimeoutMultiplier();
    final boolean debugMode = false;
    final boolean sequentialGet = false;
    final Object sequentialGetLock = new Object();
    final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("async-resource-test-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build());
    final Map<Integer, ResponseRecord> getResponses = new ConcurrentHashMap<Integer, ResponseRecord>();
    final CountDownLatch getRequestLatch = new CountDownLatch(MAX_MESSAGES);
    try {
        for (int i = 0; i < MAX_MESSAGES; i++) {
            final int requestId = i;
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    //noinspection PointlessBooleanExpression,ConstantConditions
                    if (debugMode || sequentialGet) {
                        synchronized (sequentialGetLock) {
                            get();
                        }
                    } else {
                        get();
                    }
                }

                private void get() {
                    try {
                        final Response response = resourceTarget.request().get();
                        getResponses.put(requestId, new ResponseRecord(response.getStatus(), response.readEntity(String.class)));
                    } catch (Throwable t) {
                        t.printStackTrace();
                    } finally {
                        getRequestLatch.countDown();
                    }
                }
            });
        }
        //noinspection ConstantConditions
        if (debugMode) {
            getRequestLatch.await();
        } else {
            assertTrue("Waiting for all GET requests to complete has timed out.", getRequestLatch.await(LATCH_WAIT_TIMEOUT, TimeUnit.SECONDS));
        }
    } finally {
        executor.shutdownNow();
    }
    StringBuilder messageBuilder = new StringBuilder();
    for (Map.Entry<Integer, ResponseRecord> getResponseEntry : getResponses.entrySet()) {
        messageBuilder.append("GET response for message ").append(getResponseEntry.getKey()).append(": ").append(getResponseEntry.getValue().toString()).append('\n');
    }
    LOGGER.info(messageBuilder.toString());
    assertEquals(MAX_MESSAGES, getResponses.size());
    for (Map.Entry<Integer, ResponseRecord> entry : getResponses.entrySet()) {
        assertEquals("Unexpected GET response status for request " + entry.getKey(), 200, entry.getValue().status);
        assertEquals("Unexpected GET response message for request " + entry.getKey(), expectedResponse, entry.getValue().message);
    }
}
Also used : JerseyProcessingUncaughtExceptionHandler(org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Response(javax.ws.rs.core.Response) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(org.glassfish.jersey.internal.guava.ThreadFactoryBuilder) WebTarget(javax.ws.rs.client.WebTarget) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 14 with JerseyProcessingUncaughtExceptionHandler

use of org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler in project jersey by jersey.

the class AsyncServletResourceITCase method testAsyncRequestCanceling.

/**
     * Test canceling of an async request to a servlet-deployed resource.
     *
     * @throws InterruptedException in case the waiting for all requests to complete was interrupted.
     */
@Test
public void testAsyncRequestCanceling() throws InterruptedException {
    final WebTarget resourceTarget = target("async/canceled");
    resourceTarget.register(LoggingFeature.class);
    final int MAX_MESSAGES = 10;
    final int LATCH_WAIT_TIMEOUT = 10 * getAsyncTimeoutMultiplier();
    final boolean debugMode = false;
    final boolean sequentialGet = false;
    final boolean sequentialPost = false;
    final Object sequentialGetLock = new Object();
    final Object sequentialPostLock = new Object();
    final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("async-canceled-resource-test-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build());
    final Map<Integer, String> postResponses = new ConcurrentHashMap<Integer, String>();
    final Map<Integer, String> getResponses = new ConcurrentHashMap<Integer, String>();
    final CountDownLatch postRequestLatch = new CountDownLatch(MAX_MESSAGES);
    final CountDownLatch getRequestLatch = new CountDownLatch(MAX_MESSAGES);
    try {
        for (int i = 0; i < MAX_MESSAGES; i++) {
            final int requestId = i;
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    //noinspection PointlessBooleanExpression,ConstantConditions
                    if (debugMode || sequentialGet) {
                        synchronized (sequentialGetLock) {
                            get();
                        }
                    } else {
                        get();
                    }
                }

                private void get() {
                    try {
                        final String response = resourceTarget.queryParam("id", requestId).request().get(String.class);
                        getResponses.put(requestId, response);
                    } catch (WebApplicationException ex) {
                        final Response response = ex.getResponse();
                        getResponses.put(requestId, response.getStatus() + ": " + response.readEntity(String.class));
                    } finally {
                        getRequestLatch.countDown();
                    }
                }
            });
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    //noinspection PointlessBooleanExpression,ConstantConditions
                    if (debugMode || sequentialPost) {
                        synchronized (sequentialPostLock) {
                            post();
                        }
                    } else {
                        post();
                    }
                }

                private void post() throws ProcessingException {
                    try {
                        final String response = resourceTarget.request().post(Entity.text("" + requestId), String.class);
                        postResponses.put(requestId, response);
                    } finally {
                        postRequestLatch.countDown();
                    }
                }
            });
        }
        //noinspection ConstantConditions
        if (debugMode) {
            postRequestLatch.await();
            getRequestLatch.await();
        } else {
            assertTrue("Waiting for all POST requests to complete has timed out.", postRequestLatch.await(LATCH_WAIT_TIMEOUT, TimeUnit.SECONDS));
            assertTrue("Waiting for all GET requests to complete has timed out.", getRequestLatch.await(LATCH_WAIT_TIMEOUT, TimeUnit.SECONDS));
        }
    } finally {
        executor.shutdownNow();
    }
    StringBuilder messageBuilder = new StringBuilder();
    for (Map.Entry<Integer, String> postResponseEntry : postResponses.entrySet()) {
        messageBuilder.append("POST response for message ").append(postResponseEntry.getKey()).append(": ").append(postResponseEntry.getValue()).append('\n');
    }
    messageBuilder.append('\n');
    for (Map.Entry<Integer, String> getResponseEntry : getResponses.entrySet()) {
        messageBuilder.append("GET response for message ").append(getResponseEntry.getKey()).append(": ").append(getResponseEntry.getValue()).append('\n');
    }
    LOGGER.info(messageBuilder.toString());
    assertEquals(MAX_MESSAGES, postResponses.size());
    for (Map.Entry<Integer, String> postResponseEntry : postResponses.entrySet()) {
        assertTrue("Unexpected POST notification response for message " + postResponseEntry.getKey(), postResponseEntry.getValue().startsWith(AsyncServletResource.CANCELED));
    }
    assertEquals(MAX_MESSAGES, getResponses.size());
    final Collection<Integer> getResponseKeys = getResponses.keySet();
    for (int i = 0; i < MAX_MESSAGES; i++) {
        assertTrue("Detected a GET message response loss: " + i, getResponseKeys.contains(i));
        final String getResponseEntry = getResponses.get(i);
        assertTrue("Unexpected canceled GET response status for request " + i, getResponseEntry.startsWith("503: "));
    }
}
Also used : JerseyProcessingUncaughtExceptionHandler(org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler) WebApplicationException(javax.ws.rs.WebApplicationException) CountDownLatch(java.util.concurrent.CountDownLatch) Response(javax.ws.rs.core.Response) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(org.glassfish.jersey.internal.guava.ThreadFactoryBuilder) WebTarget(javax.ws.rs.client.WebTarget) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 15 with JerseyProcessingUncaughtExceptionHandler

use of org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler in project jersey by jersey.

the class AsyncServletResourceTest method testAsyncRequestCanceling.

/**
     * Test canceling of an async request to a servlet-deployed resource.
     *
     * @throws InterruptedException in case the waiting for all requests to complete was interrupted.
     */
@Test
public void testAsyncRequestCanceling() throws InterruptedException {
    final WebTarget resourceTarget = target("async/canceled");
    resourceTarget.register(LoggingFeature.class);
    final int MAX_MESSAGES = 10;
    final int LATCH_WAIT_TIMEOUT = 10;
    final boolean debugMode = false;
    final boolean sequentialGet = false;
    final boolean sequentialPost = false;
    final Object sequentialGetLock = new Object();
    final Object sequentialPostLock = new Object();
    final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("async-canceled-resource-test-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build());
    final Map<Integer, String> postResponses = new ConcurrentHashMap<>();
    final Map<Integer, String> getResponses = new ConcurrentHashMap<>();
    final CountDownLatch postRequestLatch = new CountDownLatch(MAX_MESSAGES);
    final CountDownLatch getRequestLatch = new CountDownLatch(MAX_MESSAGES);
    try {
        for (int i = 0; i < MAX_MESSAGES; i++) {
            final int requestId = i;
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    //noinspection PointlessBooleanExpression,ConstantConditions
                    if (debugMode || sequentialGet) {
                        synchronized (sequentialGetLock) {
                            get();
                        }
                    } else {
                        get();
                    }
                }

                private void get() {
                    try {
                        final String response = resourceTarget.queryParam("id", requestId).request().get(String.class);
                        getResponses.put(requestId, response);
                    } catch (final WebApplicationException ex) {
                        final Response response = ex.getResponse();
                        getResponses.put(requestId, response.getStatus() + ": " + response.readEntity(String.class));
                    } finally {
                        getRequestLatch.countDown();
                    }
                }
            });
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    //noinspection PointlessBooleanExpression,ConstantConditions
                    if (debugMode || sequentialPost) {
                        synchronized (sequentialPostLock) {
                            post();
                        }
                    } else {
                        post();
                    }
                }

                private void post() throws ProcessingException {
                    try {
                        final String response = resourceTarget.request().post(Entity.text("" + requestId), String.class);
                        postResponses.put(requestId, response);
                    } finally {
                        postRequestLatch.countDown();
                    }
                }
            });
        }
        //noinspection ConstantConditions
        if (debugMode) {
            postRequestLatch.await();
            getRequestLatch.await();
        } else {
            assertTrue("Waiting for all POST requests to complete has timed out.", postRequestLatch.await(LATCH_WAIT_TIMEOUT * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
            assertTrue("Waiting for all GET requests to complete has timed out.", getRequestLatch.await(LATCH_WAIT_TIMEOUT * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
        }
    } finally {
        executor.shutdownNow();
    }
    final StringBuilder messageBuilder = new StringBuilder();
    for (final Map.Entry<Integer, String> postResponseEntry : postResponses.entrySet()) {
        messageBuilder.append("POST response for message ").append(postResponseEntry.getKey()).append(": ").append(postResponseEntry.getValue()).append('\n');
    }
    messageBuilder.append('\n');
    for (final Map.Entry<Integer, String> getResponseEntry : getResponses.entrySet()) {
        messageBuilder.append("GET response for message ").append(getResponseEntry.getKey()).append(": ").append(getResponseEntry.getValue()).append('\n');
    }
    LOGGER.info(messageBuilder.toString());
    assertEquals(MAX_MESSAGES, postResponses.size());
    for (final Map.Entry<Integer, String> postResponseEntry : postResponses.entrySet()) {
        assertTrue("Unexpected POST notification response for message " + postResponseEntry.getKey(), postResponseEntry.getValue().startsWith(AsyncServletResource.CANCELED));
    }
    assertEquals(MAX_MESSAGES, getResponses.size());
    final Collection<Integer> getResponseKeys = getResponses.keySet();
    for (int i = 0; i < MAX_MESSAGES; i++) {
        assertTrue("Detected a GET message response loss: " + i, getResponseKeys.contains(i));
        final String getResponseEntry = getResponses.get(i);
        assertTrue("Unexpected canceled GET response status for request " + i, getResponseEntry.startsWith("503: "));
    }
}
Also used : JerseyProcessingUncaughtExceptionHandler(org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler) WebApplicationException(javax.ws.rs.WebApplicationException) CountDownLatch(java.util.concurrent.CountDownLatch) Response(javax.ws.rs.core.Response) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(org.glassfish.jersey.internal.guava.ThreadFactoryBuilder) WebTarget(javax.ws.rs.client.WebTarget) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ProcessingException(javax.ws.rs.ProcessingException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

ThreadFactoryBuilder (org.glassfish.jersey.internal.guava.ThreadFactoryBuilder)15 JerseyProcessingUncaughtExceptionHandler (org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler)15 Map (java.util.Map)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ExecutorService (java.util.concurrent.ExecutorService)8 WebTarget (javax.ws.rs.client.WebTarget)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7 Test (org.junit.Test)7 Response (javax.ws.rs.core.Response)5 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)4 ProcessingException (javax.ws.rs.ProcessingException)4 Before (org.junit.Before)4 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 HttpServer (com.sun.net.httpserver.HttpServer)1 HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)1 HttpsServer (com.sun.net.httpserver.HttpsServer)1 InetSocketAddress (java.net.InetSocketAddress)1