Search in sources :

Example 6 with FutureCancelledError

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

the class AbstractPageWithTable method loadChildrenImpl.

/**
 * load tree children<br>
 * this method delegates to the table reload<br>
 * when the table is loaded and this node is not a leaf node then the table rows are mirrored in child nodes
 */
@Override
protected final void loadChildrenImpl() {
    ITree tree = getTree();
    try {
        if (tree != null) {
            tree.setTreeChanging(true);
        }
        // 
        // backup currently selected tree node and its path to root
        boolean oldSelectionOwned = false;
        int oldSelectionDirectChildIndex = -1;
        ITreeNode oldSelectedNode = null;
        if (tree != null) {
            oldSelectedNode = tree.getSelectedNode();
        }
        List<Object> oldSelectedRowKeys = null;
        if (oldSelectedNode != null) {
            ITreeNode t = oldSelectedNode;
            while (t != null && t.getParentNode() != null) {
                if (t.getParentNode() == this) {
                    oldSelectionOwned = true;
                    oldSelectedRowKeys = getTableRowFor(t).getKeyValues();
                    oldSelectionDirectChildIndex = t.getChildNodeIndex();
                    break;
                }
                t = t.getParentNode();
            }
        }
        // 
        setChildrenLoaded(false);
        fireBeforeDataLoaded();
        try {
            loadTableDataImpl();
        } catch (ThreadInterruptedError | FutureCancelledError e) {
        // NOSONAR
        // NOOP
        } finally {
            fireAfterDataLoaded();
        }
        setChildrenLoaded(true);
        setChildrenDirty(false);
        // table events will handle automatic tree changes in case table is mirrored in tree.
        // restore currently selected tree node when it was owned by our table rows.
        // in case selection was lost, try to select similar index as before
        T table = getTable();
        if (tree != null && table != null && oldSelectionOwned && tree.getSelectedNode() == null) {
            ITreeNode newSelectedNode = null;
            ITableRow row = table.getSelectedRow();
            if (row != null) {
                newSelectedNode = getTreeNodeFor(row);
            } else {
                row = table.findRowByKey(oldSelectedRowKeys);
                if (row != null) {
                    newSelectedNode = getTreeNodeFor(row);
                } else if (oldSelectedNode != null && oldSelectedNode.getTree() == tree) {
                    // NOSONAR
                    newSelectedNode = oldSelectedNode;
                } else {
                    int index = Math.max(-1, Math.min(oldSelectionDirectChildIndex, getChildNodeCount() - 1));
                    if (index >= 0 && index < getChildNodeCount()) {
                        newSelectedNode = getChildNode(index);
                    } else {
                        newSelectedNode = this;
                    }
                }
            }
            if (newSelectedNode != null) {
                tree.selectNode(newSelectedNode);
            }
        }
    } finally {
        if (tree != null) {
            tree.setTreeChanging(false);
        }
    }
    IDesktop desktop = ClientSessionProvider.currentSession().getDesktop();
    if (desktop != null) {
        desktop.afterTablePageLoaded(this);
    }
}
Also used : ITreeNode(org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) ITree(org.eclipse.scout.rt.client.ui.basic.tree.ITree) ITableRow(org.eclipse.scout.rt.client.ui.basic.table.ITableRow) IDesktop(org.eclipse.scout.rt.client.ui.desktop.IDesktop)

Example 7 with FutureCancelledError

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

the class BasicTransaction method cancel.

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
    synchronized (m_memberMapLock) {
        if (m_commitPhase) {
            return false;
        }
        if (m_cancelled) {
            return true;
        }
        m_cancelled = true;
        addFailure(new FutureCancelledError("Transaction cancelled"));
    }
    for (ITransactionMember mem : getMembers()) {
        try {
            mem.cancel();
        } catch (Throwable t) {
            LOG.error("cancel member {}", mem, t);
        }
    }
    return true;
}
Also used : FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)

Example 8 with FutureCancelledError

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

the class HttpServiceTunnel method tunnel.

@Override
protected ServiceTunnelResponse tunnel(final ServiceTunnelRequest serviceRequest) {
    final long requestSequence = serviceRequest.getRequestSequence();
    // Create the Callable to be given to the job manager for execution.
    final RemoteServiceInvocationCallable remoteInvocationCallable = createRemoteServiceInvocationCallable(serviceRequest);
    // Register the execution monitor as child monitor of the current monitor so that the service request is cancelled once the current monitor gets cancelled.
    // Invoke the service operation asynchronously (to enable cancellation) and wait until completed or cancelled.
    final IFuture<ServiceTunnelResponse> future = Jobs.schedule(remoteInvocationCallable, Jobs.newInput().withRunContext(RunContext.CURRENT.get().copy()).withName(createServiceRequestName(requestSequence)).withExceptionHandling(null, // do not handle uncaught exceptions because typically invoked from within a model job (might cause a deadlock, because ClientExceptionHandler schedules and waits for a model job to visualize the exception).
    false)).whenDone(new IDoneHandler<ServiceTunnelResponse>() {

        @Override
        public void onDone(DoneEvent<ServiceTunnelResponse> event) {
            if (event.isCancelled()) {
                remoteInvocationCallable.cancel();
            }
        }
    }, RunContext.CURRENT.get().copy().withRunMonitor(// separate monitor to not cancel this cancellation action.
    BEANS.get(RunMonitor.class)));
    try {
        return future.awaitDoneAndGet();
    } catch (ThreadInterruptedError e) {
        // NOSONAR
        // Ensure the monitor to be cancelled once this thread is interrupted to cancel the remote call.
        future.cancel(true);
        // Interruption has precedence over computation result or computation error.
        return new ServiceTunnelResponse(new ThreadInterruptedError(TEXTS.get("UserInterrupted")));
    } catch (FutureCancelledError e) {
        // Cancellation has precedence over computation result or computation error.
        return new ServiceTunnelResponse(new FutureCancelledError(TEXTS.get("UserInterrupted")));
    }
}
Also used : FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)

Example 9 with FutureCancelledError

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

the class RemoteServiceInvocationCallable method call.

/**
 * Invokes the remote service operation.
 *
 * @return {@link IServiceTunnelResponse}; is never <code>null</code>.
 */
@Override
public ServiceTunnelResponse call() throws Exception {
    long nBytes = 0;
    final long tStart = LOG.isDebugEnabled() ? System.nanoTime() : 0L;
    try {
        // Create the request.
        final ByteArrayOutputStream requestMessage = new ByteArrayOutputStream();
        m_tunnel.getContentHandler().writeRequest(requestMessage, m_serviceRequest);
        requestMessage.close();
        final byte[] requestData = requestMessage.toByteArray();
        nBytes = requestData.length;
        // Send the request to the server.
        HttpResponse resp = m_tunnel.executeRequest(m_serviceRequest, requestData);
        // Receive the response.
        m_tunnel.interceptHttpResponse(resp, m_serviceRequest);
        if (resp.getStatusCode() != 0 && (resp.getStatusCode() < 200 || resp.getStatusCode() > 299)) {
            // request failed
            return new ServiceTunnelResponse(new HttpException(resp.getStatusCode()));
        }
        try (InputStream in = resp.getContent()) {
            return m_tunnel.getContentHandler().readResponse(in);
        }
    } catch (IOException e) {
        if (Thread.currentThread().isInterrupted()) {
            LOG.debug("Ignoring IOException for interrupted thread.", e);
            return new ServiceTunnelResponse(new ThreadInterruptedError("Thread is interrupted.", e));
        } else if (RunMonitor.CURRENT.get().isCancelled()) {
            LOG.debug("Ignoring IOException for cancelled thread.", e);
            return new ServiceTunnelResponse(new FutureCancelledError("RunMonitor is cancelled.", e));
        }
        throw e;
    } finally {
        if (LOG.isDebugEnabled()) {
            final long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - tStart);
            LOG.debug("TIME {}.{} {}ms {} bytes", m_serviceRequest.getServiceInterfaceClassName(), m_serviceRequest.getOperation(), elapsedMillis, nBytes);
        }
    }
}
Also used : InputStream(java.io.InputStream) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) HttpResponse(com.google.api.client.http.HttpResponse) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) HttpException(org.eclipse.scout.rt.shared.servicetunnel.HttpException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)

Example 10 with FutureCancelledError

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

the class JobExceptionTranslationTest method testWithNullExceptionTranslator.

@Test
public void testWithNullExceptionTranslator() throws Throwable {
    final FutureCancelledError cancellationException = new FutureCancelledError("expected JUnit test exception");
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            throw cancellationException;
        }
    }, Jobs.newInput());
    try {
        future.awaitDoneAndGet(NullExceptionTranslator.class);
        fail("ExecutionException expected");
    } catch (FutureCancelledError e) {
        fail("ExecutionException expected");
    } catch (ExecutionException e) {
        assertFalse(future.isCancelled());
        assertSame(cancellationException, e.getCause());
    }
}
Also used : FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) Test(org.junit.Test)

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