Search in sources :

Example 11 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.

the class JobCancelTest method testCancelMultipleJobsByName.

/**
 * Cancel multiple jobs with the same job-id.
 */
@Test
public void testCancelMultipleJobsByName() throws Exception {
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final String commonJobName = "777";
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(6);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(4);
    // Job-1 (common-id) => CANCEL
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-1-interrupted");
            }
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName(commonJobName).withExecutionSemaphore(null).withExceptionHandling(null, false));
    // Job-2 (common-id) => CANCEL
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-2-interrupted");
            }
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName(commonJobName).withExecutionSemaphore(null).withExceptionHandling(null, false));
    // Job-3 (common-id) => CANCEL
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            // Job-3a (other name, same re-used runMonitor => CANCEL AS WELL)
            Jobs.getJobManager().schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        setupLatch.countDownAndBlock();
                    } catch (InterruptedException e) {
                        protocol.add("job-3a-interrupted");
                    }
                    verifyLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("otherName").withExecutionSemaphore(null).withExceptionHandling(null, false));
            // Job-3b (other name, other runMonitor => NO CANCEL)
            Jobs.getJobManager().schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        setupLatch.countDownAndBlock();
                    } catch (InterruptedException e) {
                        protocol.add("job-3b-interrupted");
                    }
                }
            }, Jobs.newInput().withRunContext(RunContexts.copyCurrent().withRunMonitor(new RunMonitor())).withName("otherName").withExecutionSemaphore(null).withExceptionHandling(null, false));
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-3-interrupted");
            }
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName(commonJobName).withExecutionSemaphore(null));
    // Job-4 (common-id, but not-null mutex)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-4-interrupted");
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName(commonJobName).withExecutionSemaphore(Jobs.newExecutionSemaphore(1)).withExceptionHandling(null, false));
    assertTrue(setupLatch.await());
    Jobs.getJobManager().cancel(Jobs.newFutureFilterBuilder().andMatchName(commonJobName).andMatchExecutionSemaphore(null).toFilter(), true);
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.hashSet("job-1-interrupted", "job-2-interrupted", "job-3-interrupted", "job-3a-interrupted"), protocol);
    // release not cancelled jobs
    setupLatch.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 12 with RunMonitor

use of org.eclipse.scout.rt.platform.context.RunMonitor 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

RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)12 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)7 Test (org.junit.Test)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)2 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)2 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)2 Callable (java.util.concurrent.Callable)1 BeanMetaData (org.eclipse.scout.rt.platform.BeanMetaData)1 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)1 JobInput (org.eclipse.scout.rt.platform.job.JobInput)1 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)1 IPingService (org.eclipse.scout.rt.shared.services.common.ping.IPingService)1 JSONObject (org.json.JSONObject)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1