Search in sources :

Example 1 with ExecutionService

use of org.jgroups.blocks.executor.ExecutionService in project JGroups by belaban.

the class ExecutingServiceTest2 method testDisconnect.

@Test
public void testDisconnect() throws Exception {
    JChannel channel1 = new JChannel(Util.getTestStack(new CENTRAL_EXECUTOR()));
    JChannel channel2 = new JChannel(Util.getTestStack(new CENTRAL_EXECUTOR()));
    channels.add(channel1);
    channels.add(channel2);
    channel1.connect("test-cluster");
    channel2.connect("test-cluster");
    Util.waitUntilAllChannelsHaveSameView(20000, 1000, channel1, channel2);
    final ExecutionService executionService = new ExecutionService(channel1);
    ExecutionRunner executionRunner1 = new ExecutionRunner(channel1);
    ExecutionRunner executionRunner2 = new ExecutionRunner(channel2);
    Thread runner1 = new Thread(executionRunner1);
    threads.add(runner1);
    runner1.start();
    Thread runner2 = new Thread(executionRunner2);
    threads.add(runner2);
    runner2.start();
    final AtomicInteger submittedTasks = new AtomicInteger();
    final AtomicInteger finishedTasks = new AtomicInteger();
    final FutureListener<Void> listener = future -> {
        finishedTasks.incrementAndGet();
        synchronized (ExecutingServiceTest2.this) {
            ExecutingServiceTest2.this.notify();
        }
    };
    Thread submitter = new Thread(new Runnable() {

        @Override
        public void run() {
            // Two long running tasks that should be sent to each runner
            submit(true);
            submit(true);
            while (!Thread.interrupted()) {
                submit(false);
                // Throttle
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }

        private void submit(boolean wait) {
            Callable<Void> task = new Wait(wait);
            NotifyingFuture<Void> future = executionService.submit(task);
            submittedTasks.incrementAndGet();
            future.setListener(listener);
        }
    });
    threads.add(submitter);
    submitter.start();
    // Run for 2 seconds
    Thread.sleep(500);
    // Close channel
    channel2.close();
    // Stop submitting
    submitter.interrupt();
    submitter.join();
    // Wait for running tasks to finish
    synchronized (this) {
        int lastFinished = finishedTasks.get();
        while (submittedTasks.get() > finishedTasks.get()) {
            wait(10000);
            if (lastFinished == finishedTasks.get()) {
                assert false : "Tasks still outstanding, none finished in the last 10s";
            }
            lastFinished = finishedTasks.get();
        }
    }
    Assert.assertEquals(submittedTasks.get(), finishedTasks.get(), "Tasks not finished");
}
Also used : NotifyingFuture(org.jgroups.util.NotifyingFuture) Util(org.jgroups.util.Util) ExecutionService(org.jgroups.blocks.executor.ExecutionService) Set(java.util.Set) Test(org.testng.annotations.Test) Callable(java.util.concurrent.Callable) FutureListener(org.jgroups.util.FutureListener) AfterMethod(org.testng.annotations.AfterMethod) Serializable(java.io.Serializable) HashSet(java.util.HashSet) CENTRAL_EXECUTOR(org.jgroups.protocols.CENTRAL_EXECUTOR) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Global(org.jgroups.Global) ExecutionRunner(org.jgroups.blocks.executor.ExecutionRunner) JChannel(org.jgroups.JChannel) JChannel(org.jgroups.JChannel) CENTRAL_EXECUTOR(org.jgroups.protocols.CENTRAL_EXECUTOR) ExecutionService(org.jgroups.blocks.executor.ExecutionService) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutionRunner(org.jgroups.blocks.executor.ExecutionRunner) NotifyingFuture(org.jgroups.util.NotifyingFuture) Test(org.testng.annotations.Test)

Example 2 with ExecutionService

use of org.jgroups.blocks.executor.ExecutionService in project JGroups by belaban.

the class ExecutionServiceDemo method start.

public void start() throws Exception {
    ch = new JChannel(props);
    if (name != null)
        ch.setName(name);
    execution_service = new ExecutionService(ch);
    runner = new ExecutionRunner(ch);
    ch.connect("executing-cluster");
    JmxConfigurator.registerChannel(ch, Util.getMBeanServer(), "execution-service", ch.getClusterName(), true);
    // Start a consumer
    queue.add(executor.submit(runner));
    random = new Random();
    printValues = false;
    try {
        loop();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        Util.close(ch);
    }
}
Also used : JChannel(org.jgroups.JChannel) Random(java.util.Random) ExecutionRunner(org.jgroups.blocks.executor.ExecutionRunner) ExecutionService(org.jgroups.blocks.executor.ExecutionService)

Aggregations

JChannel (org.jgroups.JChannel)2 ExecutionRunner (org.jgroups.blocks.executor.ExecutionRunner)2 ExecutionService (org.jgroups.blocks.executor.ExecutionService)2 Serializable (java.io.Serializable)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Global (org.jgroups.Global)1 CENTRAL_EXECUTOR (org.jgroups.protocols.CENTRAL_EXECUTOR)1 FutureListener (org.jgroups.util.FutureListener)1 NotifyingFuture (org.jgroups.util.NotifyingFuture)1 Util (org.jgroups.util.Util)1 Assert (org.testng.Assert)1 AfterMethod (org.testng.annotations.AfterMethod)1 Test (org.testng.annotations.Test)1