use of org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError in project scout.rt by eclipse.
the class FutureAwaitTest method testAwaitDone_Cancelled.
@Test(timeout = 5000)
public void testAwaitDone_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);
assertTrue(future.isCancelled());
future.awaitDone();
try {
future.awaitDoneAndGet();
fail("cancellation expected");
} catch (FutureCancelledError e) {
// NOOP
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError in project scout.rt by eclipse.
the class FutureAwaitTest method testAwaitDoneAndGetWithTimeout_Cancelled.
@Test(timeout = 5000)
public void testAwaitDoneAndGetWithTimeout_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(10, TimeUnit.SECONDS);
fail("cancellation expected");
} catch (FutureCancelledError e) {
assertTrue(future.isCancelled());
}
setupLatch.unblock();
}
use of org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError in project scout.rt by eclipse.
the class RemoteServiceInvocationCallable method cancel.
/**
* Cancels the remote service operation on server side.
*/
public void cancel() {
try {
final String sessionId = m_serviceRequest.getSessionId();
if (sessionId == null) {
// cannot cancel an event without session. The IRunMonitorCancelService requires a session.
return;
}
final Method serviceMethod = IRunMonitorCancelService.class.getMethod(IRunMonitorCancelService.CANCEL_METHOD, long.class);
final Object[] serviceArgs = new Object[] { m_serviceRequest.getRequestSequence() };
ServiceTunnelRequest request = m_tunnel.createRequest(IRunMonitorCancelService.class, serviceMethod, serviceArgs);
request.setClientNodeId(m_serviceRequest.getClientNodeId());
request.setSessionId(sessionId);
request.setUserAgent(m_serviceRequest.getUserAgent());
m_tunnel.invokeService(request);
} catch (final FutureCancelledError | ThreadInterruptedError e) {
// NOSONAR
// NOOP: Do not cancel 'cancel-request' to prevent loop.
} catch (RuntimeException | NoSuchMethodException e) {
LOG.warn("Failed to cancel server processing [requestSequence={}]", m_serviceRequest.getRequestSequence(), e);
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError in project scout.rt by eclipse.
the class AbstractProposalChooser method updateStatus.
protected void updateStatus(IContentAssistFieldDataFetchResult<LOOKUP_KEY> result) {
if (result != null && result.getException() instanceof FutureCancelledError) {
return;
}
List<? extends ILookupRow<LOOKUP_KEY>> rows = null;
Throwable exception = null;
String searchText = null;
if (result != null) {
rows = result.getLookupRows();
exception = result.getException();
searchText = result.getSearchParam().getSearchQuery();
}
if (rows == null) {
rows = CollectionUtility.emptyArrayList();
}
String statusText = null;
int severity = IStatus.INFO;
if (exception != null) {
if (exception instanceof ProcessingException) {
statusText = ((ProcessingException) exception).getStatus().getMessage();
} else {
statusText = exception.getMessage();
}
severity = IStatus.ERROR;
} else if (rows.isEmpty()) {
if (getContentAssistField().getWildcard().equals(searchText)) {
statusText = TEXTS.get("SmartFieldNoDataFound");
} else {
statusText = TEXTS.get("SmartFieldCannotComplete", (searchText == null) ? ("") : (searchText));
}
severity = IStatus.WARNING;
} else if (rows.size() > m_contentAssistField.getBrowseMaxRowCount()) {
statusText = TEXTS.get("SmartFieldMoreThanXRows", "" + m_contentAssistField.getBrowseMaxRowCount());
severity = IStatus.INFO;
}
if (statusText != null) {
setStatus(new Status(statusText, severity));
} else {
setStatus(null);
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError in project scout.rt by eclipse.
the class AbstractSmartField2 method fetchLookupRows.
/**
* Loads lookup rows according to the specified {@link ILookupRowProvider}, and notifies the specified callback upon
* fetching completed.
*
* @return {@link IFuture} if data is fetched asynchronously, or <code>null</code> for synchronous fetching, or if
* using {@link LocalLookupCall}.
*/
private IFuture<Void> fetchLookupRows(final ILookupRowProvider<VALUE> dataProvider, final ILookupRowFetchedCallback<VALUE> callback, final boolean asynchronousFetching, final int maxRowCount) {
cancelPotentialLookup();
if (getLookupCall() == null) {
callback.onSuccess(Collections.<ILookupRow<VALUE>>emptyList());
return null;
}
// Prepare the lookup call.
final ILookupCall<VALUE> lookupCall = cloneLookupCall();
lookupCall.setMaxRowCount(maxRowCount > 0 ? maxRowCount : getBrowseMaxRowCount());
// Prepare processing of the fetched rows.
final ILookupRowFetchedCallback<VALUE> internalCallback = new ILookupRowFetchedCallback<VALUE>() {
@Override
public void onSuccess(final List<? extends ILookupRow<VALUE>> rows) {
joinModelThreadAndUpdateField(rows, null);
}
@Override
public void onFailure(final RuntimeException e) {
joinModelThreadAndUpdateField(null, e);
}
private void joinModelThreadAndUpdateField(final List<? extends ILookupRow<VALUE>> rows, final RuntimeException exception) {
if (ModelJobs.isModelThread()) {
updateField(rows, exception);
} else {
// Note: The model job will not commence execution if the current ClientRunContext is or gets cancelled.
try {
ClientRunContext callerRunContext = ClientRunContexts.copyCurrent();
if (callerRunContext.getRunMonitor().isCancelled()) {
return;
}
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
updateField(rows, exception);
}
}, ModelJobs.newInput(callerRunContext).withName("Updating {}", AbstractSmartField2.this.getClass().getName())).awaitDone();
} catch (ThreadInterruptedError e) {
/*
This state can be reached by a race condition when the job's RunMonitor is in canceled state and later the ModelJob is run.
This yields to a Thread.interrupt in the RunContext.ThreadInterrupter...
at RunContext$ThreadInterrupter.cancel(boolean) line: 563
at RunMonitor.cancel(ICancellable, boolean) line: 160
at RunMonitor.registerCancellable(ICancellable) line: 104 <---------------------
at RunContext$ThreadInterrupter.<init>(Thread, RunMonitor) line: 545
at ClientRunContext(RunContext).call(Callable<RESULT>, Class<IExceptionTranslator<EXCEPTION>>) line: 154
at RunContextRunner<RESULT>.intercept(Chain<RESULT>) line: 38
which itself causes the running job to be interrupted with a InterruptedException
at org.eclipse.scout.rt.platform.job.internal.JobExceptionTranslator.translateInterruptedException(JobExceptionTranslator.java:49)
at org.eclipse.scout.rt.platform.job.internal.JobFutureTask.awaitDone(JobFutureTask.java:339)
at org.eclipse.scout.rt.client.ui.form.fields.smartfield2.AbstractSmartField2$7.joinModelThreadAndUpdateField(AbstractSmartField2.java:1598)
at org.eclipse.scout.rt.client.ui.form.fields.smartfield2.AbstractSmartField2$7.onSuccess(AbstractSmartField2.java:1575)
at org.eclipse.scout.rt.shared.services.lookup.LookupCall.loadData(LookupCall.java:437)
at org.eclipse.scout.rt.shared.services.lookup.LookupCall$5.run(LookupCall.java:417)
*/
return;
}
}
}
private void updateField(final List<? extends ILookupRow<VALUE>> rows, final RuntimeException exception) {
try {
if (exception != null) {
// throw to handle exception at the end.
throw exception;
}
final List<ILookupRow<VALUE>> result = new ArrayList<>(rows);
dataProvider.afterProvide(lookupCall, result);
callback.onSuccess(result);
} catch (FutureCancelledError | ThreadInterruptedError e) {
// NOSONAR
callback.onSuccess(Collections.<ILookupRow<VALUE>>emptyList());
} catch (final RuntimeException e) {
callback.onFailure(e);
}
}
};
// Start fetching lookup rows.
IFuture<Void> asyncLookupFuture = null;
try {
dataProvider.beforeProvide(lookupCall);
if (asynchronousFetching) {
asyncLookupFuture = dataProvider.provideAsync(lookupCall, internalCallback, ClientRunContexts.copyCurrent());
} else {
dataProvider.provideSync(lookupCall, internalCallback);
asyncLookupFuture = null;
}
} catch (final RuntimeException e) {
internalCallback.onFailure(e);
asyncLookupFuture = null;
}
m_lookupFuture = asyncLookupFuture;
return asyncLookupFuture;
}
Aggregations