Search in sources :

Example 11 with FutureCancelledError

use of org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError in project scout.rt by eclipse.

the class FutureAwaitTest method testAwaitDoneAndGet_Cancelled.

@Test(timeout = 5000)
public void testAwaitDoneAndGet_Cancelled() throws java.lang.InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    // Init
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            setupLatch.countDownAndBlock();
            return "result";
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    // Wait until ready
    assertTrue(setupLatch.await());
    // Run the test and verify
    future.cancel(false);
    try {
        future.awaitDoneAndGet();
        fail("cancellation expected");
    } catch (FutureCancelledError e) {
        assertTrue(future.isCancelled());
    }
    setupLatch.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) Test(org.junit.Test)

Example 12 with FutureCancelledError

use of org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError in project scout.rt by eclipse.

the class UiSession method processFileUpload.

@Override
public JSONObject processFileUpload(HttpServletRequest req, HttpServletResponse res, final IBinaryResourceConsumer resourceConsumer, final List<BinaryResource> uploadResources, final Map<String, String> uploadProperties) {
    final ClientRunContext clientRunContext = ClientRunContexts.copyCurrent().withSession(m_clientSession, true);
    m_httpContext.set(req, res);
    try {
        m_processingJsonRequest = true;
        try {
            // 1. Process the JSON request.
            ModelJobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    resourceConsumer.consumeBinaryResource(uploadResources, uploadProperties);
                }
            }, createFileUploadModelJobInput(clientRunContext));
            // 2. Wait for all model jobs of the session.
            BEANS.get(UiJobs.class).awaitModelJobs(m_clientSession, ExceptionHandler.class);
        } finally {
            // Reset this flag _before_ the "response-to-json" job (#3), because writing to the response while transforming would be unsafe and unreliable.
            m_processingJsonRequest = false;
        }
        // 3. Transform the response to JSON.
        final IFuture<JSONObject> future = ModelJobs.schedule(newResponseToJsonTransformer(), ModelJobs.newInput(clientRunContext.copy().withRunMonitor(// separate RunMonitor to not cancel 'response-to-json' job once processing is cancelled
        BEANS.get(RunMonitor.class))).withName("Transforming response to JSON").withExecutionHint(UiJobs.EXECUTION_HINT_RESPONSE_TO_JSON).withExceptionHandling(null, // Propagate exception to caller (UIServlet)
        false));
        try {
            return BEANS.get(UiJobs.class).awaitAndGet(future);
        } catch (ThreadInterruptedError e) {
            // NOSONAR
            future.cancel(true);
            return null;
        } catch (FutureCancelledError e) {
            // NOSONAR
            return null;
        }
    } finally {
        m_httpContext.clear();
        if (m_disposing) {
            dispose();
        }
    }
}
Also used : ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) JSONObject(org.json.JSONObject) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable)

Example 13 with FutureCancelledError

use of org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError in project scout.rt by eclipse.

the class UiSession method processJsonRequest.

@Override
public JSONObject processJsonRequest(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final JsonRequest jsonRequest) {
    if (isAlreadyProcessed(jsonRequest)) {
        JSONObject response = m_responseHistory.getResponseForRequest(jsonRequest.getSequenceNo());
        LOG.debug("Request #{} was already processed. Sending back response from history.", jsonRequest.getSequenceNo());
        return response;
    }
    final ClientRunContext clientRunContext = ClientRunContexts.copyCurrent().withSession(m_clientSession, true);
    m_httpContext.set(servletRequest, servletResponse);
    m_currentJsonRequest = jsonRequest;
    try {
        m_processingJsonRequest = true;
        try {
            // 1. Process the JSON request.
            ModelJobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    processJsonRequestInternal();
                }
            }, createJsonRequestModelJobInput(jsonRequest, clientRunContext));
            // 2. Wait for all model jobs of the session.
            BEANS.get(UiJobs.class).awaitModelJobs(m_clientSession, ExceptionHandler.class);
        } finally {
            // Reset this flag _before_ the "response-to-json" job (#3), because writing to the response while transforming would be unsafe and unreliable.
            m_processingJsonRequest = false;
        }
        // 3. Transform the response to JSON.
        final IFuture<JSONObject> future = ModelJobs.schedule(newResponseToJsonTransformer(), ModelJobs.newInput(clientRunContext.copy().withRunMonitor(// separate RunMonitor to not cancel 'response-to-json' job once processing is cancelled
        BEANS.get(RunMonitor.class))).withName("Transforming response to JSON").withExecutionHint(UiJobs.EXECUTION_HINT_RESPONSE_TO_JSON).withExecutionHint(UiJobs.EXECUTION_HINT_POLL_REQUEST, jsonRequest.getRequestType() == RequestType.POLL_REQUEST).withExceptionHandling(null, // Propagate exception to caller (UIServlet)
        false));
        try {
            return BEANS.get(UiJobs.class).awaitAndGet(future);
        } catch (ThreadInterruptedError e) {
            // NOSONAR
            future.cancel(true);
            return null;
        } catch (FutureCancelledError e) {
            // NOSONAR
            return null;
        }
    } finally {
        setRequestProcessed(jsonRequest);
        m_httpContext.clear();
        m_currentJsonRequest = null;
        if (m_disposing) {
            dispose();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Adapter count after request: {}", m_jsonAdapterRegistry.size());
        }
    }
}
Also used : JSONObject(org.json.JSONObject) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable)

Aggregations

FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)13 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)7 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 Test (org.junit.Test)4 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)3 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)3 ServiceTunnelResponse (org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)2 JSONObject (org.json.JSONObject)2 HttpResponse (com.google.api.client.http.HttpResponse)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 ITableRow (org.eclipse.scout.rt.client.ui.basic.table.ITableRow)1 ITree (org.eclipse.scout.rt.client.ui.basic.tree.ITree)1 ITreeNode (org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode)1 IDesktop (org.eclipse.scout.rt.client.ui.desktop.IDesktop)1