Search in sources :

Example 6 with ClosedException

use of com.revolsys.parallel.channel.ClosedException 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)

Aggregations

ClosedException (com.revolsys.parallel.channel.ClosedException)6 Logger (org.apache.log4j.Logger)3 ThreadInterruptedException (com.revolsys.parallel.ThreadInterruptedException)2 Channel (com.revolsys.parallel.channel.Channel)2 MultiInputSelector (com.revolsys.parallel.channel.MultiInputSelector)2 ScriptExecutorRunnable (com.revolsys.parallel.tools.ScriptExecutorRunnable)1 Record (com.revolsys.record.Record)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)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