1

I need to do several actions at the same time in Jexl. In official guide I found annotation @parallel: https://commons.apache.org/proper/commons-jexl/reference/syntax.html But I didn't find any examples how to use it.

Can anyone provide some examples?

I thought it will work like this:

@parallel { a.someMethod() }
@parallel { b.someMethod() }

But seems like it's still working sequentially. Second example I've tried, still not working:

var loopFunction = function(title){
    var i = 0;
    logger:info("Starting "+title);
    while(i<100) {
        logger:info(title+"="+i);
        utils:sleep(25);
        i += 1;
    }
    logger:info("Ending "+title);
}

@parallel loopFunction('i');
@parallel loopFunction('j');
  • When you say "It's still working sequentially", what does it means ? How did you checked ? – NatNgs Feb 13 '18 at 15:23
  • I've tried to add big loops (5 secs) to this method with logging. And I got `b.someMethod()` log after `a.someMethod()` log. – Mikalai Ushanau Feb 13 '18 at 15:38
  • Maybe try with a random time in your `utils.sleep` (so that i and j has a different waiting time). If it not fix the problem, I will let jexl professionals help you :P – NatNgs Feb 13 '18 at 17:06
  • Tried different timings, different length of loop. Still it prints `Starting i i=1 ... i=100 Ending i Starting j j=1 ... j=250 Ending j` – Mikalai Ushanau Feb 13 '18 at 18:13

1 Answers1

1

It seems that it's an example of annotation, in jexl code it seems that only synchronized is implemented.

@Override
public Object processAnnotation(String name, Object[] args,Callable<Object> statement) throws Exception {

    if ("synchronized".equals(name)) {
        final Object arg = args[0];
        synchronized(arg) {
            return statement.call();
        }
    }
    throw new IllegalArgumentException("unknown annotation " + name);
}

There's a parallel test but it uses ExecutorService:

/**
     * Run same test function in NTHREADS in parallel.
     * @param ctask the task / test
     * @param loops number of loops to perform
     * @param cache whether jexl cache is used or not
     * @throws Exception if anything goes wrong
     */
    @SuppressWarnings("boxing")
    void runThreaded(Class<? extends Task> ctask, int loops, boolean cache) throws Exception {
        if (loops == 0) {
            loops = MIX.length;
        }
        if (!cache) {
            jexl = jexlNoCache;
        } else {
            jexl = jexlCache;
        }
        java.util.concurrent.ExecutorService execs = java.util.concurrent.Executors.newFixedThreadPool(NTHREADS);
        List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(NTHREADS);
        for (int t = 0; t < NTHREADS; ++t) {
            tasks.add(jexl.newInstance(ctask, loops));
        }
        // let's not wait for more than a minute
        List<Future<Integer>> futures = execs.invokeAll(tasks, 60, TimeUnit.SECONDS);
        // check that all returned loops
        for (Future<Integer> future : futures) {
            Assert.assertEquals(Integer.valueOf(loops), future.get());
        }
    }
Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233