use of java.util.concurrent.ExecutorCompletionService in project drools by kiegroup.
the class AbstractConcurrentInsertionsTest method testConcurrentInsertions.
protected void testConcurrentInsertions(final String drl, final int objectCount, final int threadCount, final boolean newSessionForEachThread, final boolean updateFacts) throws InterruptedException {
final KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build();
final ExecutorService executor = Executors.newCachedThreadPool(r -> {
final Thread t = new Thread(r);
t.setDaemon(true);
return t;
});
KieSession ksession = null;
try {
final Callable[] tasks = new Callable[threadCount];
if (newSessionForEachThread) {
for (int i = 0; i < threadCount; i++) {
tasks[i] = getTask(objectCount, kieBase, updateFacts);
}
} else {
ksession = kieBase.newKieSession();
for (int i = 0; i < threadCount; i++) {
tasks[i] = getTask(objectCount, ksession, false, updateFacts);
}
}
final CompletionService<Boolean> ecs = new ExecutorCompletionService<>(executor);
for (final Callable task : tasks) {
ecs.submit(task);
}
int successCounter = 0;
for (int i = 0; i < threadCount; i++) {
try {
if (ecs.take().get()) {
successCounter++;
}
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Assertions.assertThat(successCounter).isEqualTo(threadCount);
if (ksession != null) {
ksession.dispose();
}
} finally {
executor.shutdown();
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
}
}
use of java.util.concurrent.ExecutorCompletionService in project drools by kiegroup.
the class MultithreadTest method testSlidingTimeWindows.
@Test(timeout = 1000000)
public void testSlidingTimeWindows() {
final String str = "package org.drools\n" + "global java.util.List list; \n" + "import " + StockTick.class.getCanonicalName() + "; \n" + "" + "declare StockTick @role(event) end\n" + "" + "rule R\n" + "when\n" + " accumulate( $st : StockTick() over window:time(400ms)\n" + " from entry-point X,\n" + " $c : count(1) )" + "then\n" + " list.add( $c ); \n" + "end \n";
final List<Exception> errors = new ArrayList<Exception>();
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(kbconf, str);
final KieSession ksession = kbase.newKieSession();
final EntryPoint ep = ksession.getEntryPoint("X");
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Executor executor = Executors.newCachedThreadPool(new ThreadFactory() {
public Thread newThread(final Runnable r) {
final Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
});
// runs for 10 seconds
final int RUN_TIME = 5000;
final int THREAD_NR = 2;
final CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
ecs.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
try {
ksession.fireUntilHalt();
return true;
} catch (final Exception e) {
errors.add(e);
e.printStackTrace();
return false;
}
}
});
for (int i = 0; i < THREAD_NR; i++) {
ecs.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
try {
final String s = Thread.currentThread().getName();
final long endTS = System.currentTimeMillis() + RUN_TIME;
int j = 0;
long lastTimeInserted = -1;
while (System.currentTimeMillis() < endTS) {
final long currentTimeInMillis = System.currentTimeMillis();
if (currentTimeInMillis > lastTimeInserted) {
lastTimeInserted = currentTimeInMillis;
ep.insert(new StockTick(j++, s, 0.0, 0));
}
}
return true;
} catch (final Exception e) {
errors.add(e);
e.printStackTrace();
return false;
}
}
});
}
boolean success = true;
for (int i = 0; i < THREAD_NR; i++) {
try {
success = ecs.take().get() && success;
} catch (final Exception e) {
errors.add(e);
}
}
ksession.halt();
try {
success = ecs.take().get() && success;
} catch (final Exception e) {
errors.add(e);
}
for (final Exception e : errors) {
e.printStackTrace();
}
Assertions.assertThat(errors).isEmpty();
Assertions.assertThat(success).isTrue();
ksession.dispose();
}
use of java.util.concurrent.ExecutorCompletionService in project drools by kiegroup.
the class PhreakConcurrencyTest method testMultipleConcurrentEPs2.
@Test(timeout = 10000)
public void testMultipleConcurrentEPs2() {
String str = "global java.util.List results\n" + "\n" + "rule \"R0\" when\n" + " $s : String( ) from entry-point EP0\n" + " $i : Integer( toString().equals($s) ) from entry-point EP1\n" + " $l : Long( intValue() == $i ) from entry-point EP2\n" + "then\n" + " results.add( $s );\n" + "end\n" + "\n" + "rule \"R1\" when\n" + " $s : String( ) from entry-point EP1\n" + " $i : Integer( toString().equals($s) ) from entry-point EP2\n" + " $l : Long( intValue() == $i ) from entry-point EP0\n" + "then\n" + " results.add( $s );\n" + "end\n" + "\n" + "rule \"R2\" when\n" + " $s : String( ) from entry-point EP2\n" + " $i : Integer( toString().equals($s) ) from entry-point EP0\n" + " $l : Long( intValue() == $i ) from entry-point EP1\n" + "then\n" + " results.add( $s );\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = kbase.newKieSession();
List<String> results = new ArrayList<String>();
ksession.setGlobal("results", results);
boolean success = true;
CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
for (int i = 0; i < 3; i++) {
ecs.submit(new EPManipulator2(ksession, i));
}
for (int i = 0; i < 3; i++) {
try {
success = ecs.take().get() && success;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
assertTrue(success);
ksession.fireAllRules();
System.out.println(results);
assertEquals(3, results.size());
for (String s : results) {
assertEquals("2", s);
}
}
use of java.util.concurrent.ExecutorCompletionService in project drools by kiegroup.
the class Misc2Test method testFunctionInvokingFunction.
@Test
public void testFunctionInvokingFunction() throws Exception {
// DROOLS-926
String drl = "function boolean isOdd(int i) {\n" + " return i % 2 == 1;\n" + "}\n" + "\n" + "function boolean isEven(int i) {\n" + " return !isOdd(i);\n" + "}\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule R when\n" + " $i : Integer( isEven( this ) ) \n" + "then\n" + " list.add($i);\n" + "end\n";
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
int parallelThreads = 10;
ExecutorService executor = Executors.newFixedThreadPool(parallelThreads);
Collection<Callable<Boolean>> solvers = new ArrayList<Callable<Boolean>>();
for (int i = 0; i < parallelThreads; ++i) {
solvers.add(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
KieSession ksession = kbase.newKieSession();
List<Integer> list = new ArrayList<Integer>();
ksession.setGlobal("list", list);
for (int i = 0; i < 100; i++) {
ksession.insert(i);
}
ksession.fireAllRules();
return list.size() == 50;
}
});
}
CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
for (Callable<Boolean> s : solvers) {
ecs.submit(s);
}
for (int i = 0; i < parallelThreads; ++i) {
assertTrue(ecs.take().get());
}
}
Aggregations