Search in sources :

Example 1 with ThreadInterruptedException

use of com.revolsys.parallel.ThreadInterruptedException in project com.revolsys.open by revolsys.

the class NamedChannelBundle method read.

public T read(final long timeout, final Collection<String> names) {
    synchronized (this.readMonitor) {
        synchronized (this.monitor) {
            final int readerNotifyCount = this.readerNotifyCount;
            try {
                long maxTime = 0;
                if (timeout > 0) {
                    maxTime = System.currentTimeMillis() + timeout;
                }
                if (isClosed()) {
                    throw new ClosedException();
                }
                Queue<T> queue = getNextValueQueue(names);
                if (timeout == 0) {
                    while (queue == null && readerNotifyCount == this.readerNotifyCount) {
                        try {
                            this.monitor.wait();
                        } catch (final InterruptedException e) {
                            throw new ThreadInterruptedException(e);
                        }
                        if (isClosed()) {
                            throw new ClosedException();
                        }
                        queue = getNextValueQueue(names);
                    }
                } else if (timeout > 0) {
                    long waitTime = maxTime - System.currentTimeMillis();
                    while (queue == null && waitTime > 0 && readerNotifyCount == this.readerNotifyCount) {
                        final long milliSeconds = waitTime;
                        try {
                            this.monitor.wait(milliSeconds);
                        } catch (final InterruptedException e) {
                            throw new ThreadInterruptedException(e);
                        }
                        if (isClosed()) {
                            throw new ClosedException();
                        }
                        queue = getNextValueQueue(names);
                        waitTime = maxTime - System.currentTimeMillis();
                    }
                } else {
                    queue = getNextValueQueue(names);
                }
                if (queue == null) {
                    return null;
                } else {
                    final T value = queue.remove();
                    this.monitor.notifyAll();
                    return value;
                }
            } catch (final ThreadInterruptedException e) {
                close();
                this.monitor.notifyAll();
                throw new ClosedException();
            }
        }
    }
}
Also used : ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException) ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException)

Example 2 with ThreadInterruptedException

use of com.revolsys.parallel.ThreadInterruptedException in project com.revolsys.open by revolsys.

the class RunnableChannelExecutor method run.

@Override
public void run() {
    preRun();
    try {
        final MultiInputSelector selector = new MultiInputSelector();
        while (!isShutdown()) {
            final List<Channel<Runnable>> channels = this.channels;
            try {
                if (!isShutdown()) {
                    final Channel<Runnable> channel = selector.selectChannelInput(channels);
                    if (channel != null) {
                        final Runnable runnable = channel.read();
                        execute(runnable);
                    }
                }
            } catch (final ClosedException e) {
                final Throwable cause = e.getCause();
                if (cause instanceof ThreadInterruptedException) {
                    throw (ThreadInterruptedException) cause;
                }
                synchronized (this.monitor) {
                    for (final Iterator<Channel<Runnable>> iterator = channels.iterator(); iterator.hasNext(); ) {
                        final Channel<Runnable> channel = iterator.next();
                        if (channel.isClosed()) {
                            iterator.remove();
                        }
                    }
                    if (channels.isEmpty()) {
                        return;
                    }
                }
            }
        }
    } catch (final ThreadInterruptedException e) {
        throw e;
    } catch (final Throwable t) {
        if (!isShutdown()) {
            Logs.error(this, "Unexexpected error ", t);
        }
    } finally {
        postRun();
    }
}
Also used : MultiInputSelector(com.revolsys.parallel.channel.MultiInputSelector) ClosedException(com.revolsys.parallel.channel.ClosedException) Channel(com.revolsys.parallel.channel.Channel) Iterator(java.util.Iterator) ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException)

Example 3 with ThreadInterruptedException

use of com.revolsys.parallel.ThreadInterruptedException in project com.revolsys.open by revolsys.

the class ScriptExecutorProcess method executeScript.

private void executeScript(final Record record) {
    try {
        final JexlContext context = new HashMapContext();
        final Map<String, Object> vars = new HashMap<>(this.attributes);
        vars.putAll(record);
        context.setVars(vars);
        final Map<String, Object> scriptParams = new HashMap<>();
        scriptParams.putAll(this.attributes);
        for (final Entry<String, Expression> param : this.expressions.entrySet()) {
            final String key = param.getKey();
            final Expression expression = param.getValue();
            final Object value = JexlUtil.evaluateExpression(context, expression);
            scriptParams.put(key, value);
        }
        final ScriptExecutorRunnable scriptRunner = new ScriptExecutorRunnable(this.script, scriptParams);
        if (this.executor == null) {
            scriptRunner.run();
        } else {
            while (this.tasks.size() >= this.maxConcurrentScripts) {
                try {
                    synchronized (this) {
                        ThreadUtil.pause(1000);
                        for (final Iterator<Future<?>> taskIter = this.tasks.iterator(); taskIter.hasNext(); ) {
                            final Future<?> task = taskIter.next();
                            if (task.isDone()) {
                                taskIter.remove();
                            }
                        }
                    }
                } catch (final ThreadInterruptedException e) {
                    throw new ClosedException(e);
                }
            }
            final Future<?> future = this.executor.submit(scriptRunner);
            this.tasks.add(future);
        }
    } catch (final ThreadDeath e) {
        throw e;
    } catch (final Throwable t) {
        Logs.error(this, t);
    }
}
Also used : ClosedException(com.revolsys.parallel.channel.ClosedException) HashMap(java.util.HashMap) ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException) HashMapContext(org.apache.commons.jexl.context.HashMapContext) Expression(org.apache.commons.jexl.Expression) JexlContext(org.apache.commons.jexl.JexlContext) Future(java.util.concurrent.Future) ScriptExecutorRunnable(com.revolsys.parallel.tools.ScriptExecutorRunnable)

Example 4 with ThreadInterruptedException

use of com.revolsys.parallel.ThreadInterruptedException in project com.revolsys.open by revolsys.

the class Channel method read.

/**
 * Reads an Object from the Channel. This method also ensures only one of the
 * readers can actually be reading at any time. All other readers are blocked
 * until it completes the read. If no data is available to be read after the
 * timeout the method will return null.
 *
 * @param timeout The maximum time to wait in milliseconds.
 * @return The object returned from the Channel.
 */
@Override
public T read(final long timeout) {
    synchronized (this.readMonitor) {
        synchronized (this.monitor) {
            if (isClosed()) {
                throw new ClosedException();
            }
            if (this.data.getState() == ChannelValueStore.EMPTY) {
                try {
                    try {
                        this.monitor.wait(timeout);
                    } catch (final InterruptedException e) {
                        throw new ThreadInterruptedException(e);
                    }
                    if (isClosed()) {
                        throw new ClosedException();
                    }
                } catch (final ThreadInterruptedException e) {
                    close();
                    this.monitor.notifyAll();
                    throw new ClosedException();
                }
            }
            if (this.data.getState() == ChannelValueStore.EMPTY) {
                return null;
            } else {
                final T value = this.data.get();
                this.monitor.notifyAll();
                return value;
            }
        }
    }
}
Also used : ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException) ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException)

Example 5 with ThreadInterruptedException

use of com.revolsys.parallel.ThreadInterruptedException in project com.revolsys.open by revolsys.

the class Channel method write.

/**
 * Writes an Object to the Channel. This method also ensures only one of the
 * writers can actually be writing at any time. All other writers are blocked
 * until it completes the write.
 *
 * @param value The object to write to the Channel.
 */
@Override
public void write(final T value) {
    synchronized (this.writeMonitor) {
        synchronized (this.monitor) {
            if (this.closed) {
                throw new ClosedException();
            }
            final MultiInputSelector tempAlt = this.alt;
            this.data.put(value);
            if (tempAlt != null) {
                tempAlt.schedule();
            } else {
                this.monitor.notifyAll();
            }
            if (this.data.getState() == ChannelValueStore.FULL) {
                try {
                    try {
                        this.monitor.wait();
                    } catch (final InterruptedException e) {
                        throw new ThreadInterruptedException(e);
                    }
                    if (this.closed) {
                        throw new ClosedException();
                    }
                } catch (final ThreadInterruptedException e) {
                    close();
                    this.monitor.notifyAll();
                    throw new ClosedException(e);
                }
            }
        }
    }
}
Also used : ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException) ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException)

Aggregations

ThreadInterruptedException (com.revolsys.parallel.ThreadInterruptedException)5 ClosedException (com.revolsys.parallel.channel.ClosedException)2 Channel (com.revolsys.parallel.channel.Channel)1 MultiInputSelector (com.revolsys.parallel.channel.MultiInputSelector)1 ScriptExecutorRunnable (com.revolsys.parallel.tools.ScriptExecutorRunnable)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Future (java.util.concurrent.Future)1 Expression (org.apache.commons.jexl.Expression)1 JexlContext (org.apache.commons.jexl.JexlContext)1 HashMapContext (org.apache.commons.jexl.context.HashMapContext)1