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();
}
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());
}
}
}
Aggregations