Example usage for java.util.concurrent Callable Callable

List of usage examples for java.util.concurrent Callable Callable

Introduction

In this page you can find the example usage for java.util.concurrent Callable Callable.

Prototype

Callable

Source Link

Usage

From source file:com.msopentech.odatajclient.engine.communication.request.ODataBasicRequestImpl.java

/**
 * {@inheritDoc}/*  w  w w .  ja va2 s . c  om*/
 */
@Override
public final Future<V> asyncExecute() {
    return Configuration.getExecutor().submit(new Callable<V>() {

        @Override
        public V call() throws Exception {
            return execute();
        }
    });
}

From source file:com.adobe.acs.commons.util.ThreadContextClassLoaderTaskExecutorTest.java

@Test
public void test_with_exception_in_task() throws Exception {
    final ClassLoader parent = getClass().getClassLoader();

    final ClassLoader innerLoader = new DummyClassLoader(parent);

    //set the TCCL to something different
    ClassLoader outerLoader = new DummyClassLoader(parent);
    Thread.currentThread().setContextClassLoader(outerLoader);

    boolean thrown = false;

    try {/* w  w  w  . j  a  v  a2s . c  o m*/
        ThreadContextClassLoaderTaskExecutor.doWithTccl(innerLoader, new Callable<String>() {
            @Override
            public String call() throws Exception {
                assertEquals(innerLoader, Thread.currentThread().getContextClassLoader());
                throw new Exception();
            }
        });
    } catch (Exception e) {
        thrown = true;
    }
    assertTrue(thrown);
    assertEquals(outerLoader, Thread.currentThread().getContextClassLoader());
}

From source file:net.ion.framework.db.manager.script.FileAlterationMonitor.java

public synchronized void start() throws Exception {

    Callable<Void> sjob = new Callable<Void>() {
        @Override/*from  w w w  .ja va2 s .  c  om*/
        public Void call() throws Exception {
            for (FileAlterationObserver o : observers) {
                o.checkAndNotify();
            }
            ses.schedule(this, interval, TimeUnit.MILLISECONDS);
            return null;
        }
    };
    ses.schedule(sjob, interval, TimeUnit.MILLISECONDS);
}

From source file:net.ion.radon.cload.monitor.FileAlterationMonitor.java

public synchronized void start() throws Exception {

    Callable<Void> sjob = new Callable<Void>() {
        @Override// www  . j  a  v  a2  s.  c  o m
        public Void call() throws Exception {
            try {
                for (FileAlterationObserver o : observers) {
                    o.checkAndNotify();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                ses.schedule(this, interval, TimeUnit.MILLISECONDS);
            }
            return null;
        }
    };
    ses.schedule(sjob, interval, TimeUnit.MILLISECONDS);
}

From source file:de.unentscheidbar.validation.internal.Beans.java

public static <T> T propertyValue(Object bean, String propertyName) {

    try {//from w w  w . ja v  a2s .  c o  m
        ConcurrentMap<String, MethodHandle> methodHandles = CACHE.get(bean.getClass(),
                new Callable<ConcurrentMap<String, MethodHandle>>() {

                    @Override
                    public ConcurrentMap<String, MethodHandle> call() throws Exception {

                        return new MapMaker().concurrencyLevel(2).makeMap();
                    }
                });
        /*
         * We may assume this map only grows and never shrinks. It does not matter if the same
         * getter is added twice by concurrent invocations of this method.
         */
        MethodHandle getter = methodHandles.get(propertyName);
        if (getter == null) {
            getter = getterMethod(bean.getClass(), propertyName);
            methodHandles.put(propertyName, getter);
        }
        assert getter != null;
        return (T) getter.invoke(bean);
    } catch (Throwable t) {
        throw Throwables.propagate(t);
    }
}

From source file:com.netflix.curator.framework.recipes.barriers.TestDistributedDoubleBarrier.java

@Test
public void testMultiClient() throws Exception {
    final Timing timing = new Timing();
    final CountDownLatch postEnterLatch = new CountDownLatch(QTY);
    final CountDownLatch postLeaveLatch = new CountDownLatch(QTY);
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicInteger max = new AtomicInteger(0);
    List<Future<Void>> futures = Lists.newArrayList();
    ExecutorService service = Executors.newCachedThreadPool();
    for (int i = 0; i < QTY; ++i) {
        Future<Void> future = service.submit(new Callable<Void>() {
            @Override/*  w  ww .  j  av  a  2 s.com*/
            public Void call() throws Exception {
                CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
                        timing.session(), timing.connection(), new RetryOneTime(1));
                try {
                    client.start();
                    DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier", QTY);

                    Assert.assertTrue(barrier.enter(timing.seconds(), TimeUnit.SECONDS));

                    synchronized (TestDistributedDoubleBarrier.this) {
                        int thisCount = count.incrementAndGet();
                        if (thisCount > max.get()) {
                            max.set(thisCount);
                        }
                    }

                    postEnterLatch.countDown();
                    Assert.assertTrue(timing.awaitLatch(postEnterLatch));

                    Assert.assertEquals(count.get(), QTY);

                    Assert.assertTrue(barrier.leave(timing.seconds(), TimeUnit.SECONDS));
                    count.decrementAndGet();

                    postLeaveLatch.countDown();
                    Assert.assertTrue(timing.awaitLatch(postEnterLatch));
                } finally {
                    IOUtils.closeQuietly(client);
                }

                return null;
            }
        });
        futures.add(future);
    }

    for (Future<Void> f : futures) {
        f.get();
    }
    Assert.assertEquals(count.get(), 0);
    Assert.assertEquals(max.get(), QTY);
}

From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessReadWriteLock.java

@Test
public void testGetParticipantNodes() throws Exception {
    final int READERS = 20;
    final int WRITERS = 8;

    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try {/* w  w w. ja v  a2s . c  om*/
        client.start();

        final CountDownLatch latch = new CountDownLatch(READERS + WRITERS);
        final CountDownLatch readLatch = new CountDownLatch(READERS);
        final InterProcessReadWriteLock lock = new InterProcessReadWriteLock(client, "/lock");

        ExecutorService service = Executors.newCachedThreadPool();
        for (int i = 0; i < READERS; ++i) {
            service.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    lock.readLock().acquire();
                    latch.countDown();
                    readLatch.countDown();
                    return null;
                }
            });
        }
        for (int i = 0; i < WRITERS; ++i) {
            service.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    Assert.assertTrue(readLatch.await(10, TimeUnit.SECONDS));
                    latch.countDown(); // must be before as there can only be one writer
                    lock.writeLock().acquire();
                    return null;
                }
            });
        }

        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));

        Collection<String> readers = lock.readLock().getParticipantNodes();
        Collection<String> writers = lock.writeLock().getParticipantNodes();

        Assert.assertEquals(readers.size(), READERS);
        Assert.assertEquals(writers.size(), WRITERS);
    } finally {
        IOUtils.closeQuietly(client);
    }
}

From source file:fr.pilato.spring.elasticsearch.ElasticsearchNodeFactoryBean.java

@Override
public void afterPropertiesSet() throws Exception {

    if (async) {//from  w w w  .  jav  a 2 s  .  c om
        Assert.notNull(taskExecutor);

        Future<Node> nodeFuture = taskExecutor.submit(new Callable<Node>() {
            @Override
            public Node call() throws Exception {
                return initialize();
            }
        });

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setProxyTargetClass(true);
        proxyFactory.setTargetClass(Node.class);
        proxyFactory.addAdvice(new GenericInvocationHandler(nodeFuture));
        proxyfiedNode = (Node) proxyFactory.getProxy();

    } else {
        node = initialize();
    }
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.ServerQuotaDaoImplTest.java

@Test
public void testCreateUpdate() throws Exception {
    this.execute(new Callable<Object>() {
        @Override/*from  w w w .j a  va 2s  .  c o  m*/
        public Object call() {
            final BlackboardServerQuotasResponse quotasResponse = new BlackboardServerQuotasResponse();
            quotasResponse.setDiskQuota(10737418240l);
            quotasResponse.setDiskQuotaAvailable(1073741824);
            quotasResponse.setSessionQuota(1024);
            quotasResponse.setSessionQuotaAvailable(1012);

            final ServerQuota serverQuota = serverQuotaDao.createOrUpdateQuota(quotasResponse);
            assertNotNull(serverQuota);

            assertEquals(10737418240l, serverQuota.getDiskQuota());

            return null;
        }
    });

    this.execute(new Callable<Object>() {
        @Override
        public Object call() {
            final BlackboardServerQuotasResponse quotasResponse = new BlackboardServerQuotasResponse();
            quotasResponse.setDiskQuota(1073741824);
            quotasResponse.setDiskQuotaAvailable(1073741824);
            quotasResponse.setSessionQuota(1024);
            quotasResponse.setSessionQuotaAvailable(1012);

            final ServerQuota serverQuota = serverQuotaDao.createOrUpdateQuota(quotasResponse);
            assertNotNull(serverQuota);

            assertEquals(1073741824, serverQuota.getDiskQuota());

            return null;
        }
    });
}

From source file:com.imaginary.home.device.hue.HueBulb.java

@Override
public @Nonnull Future<Boolean> changeColor(final @Nonnull Color newColor,
        final @Nullable TimePeriod<?> transitionTime) {
    return HomeController.executorService.submit(new Callable<Boolean>() {
        @Override//from   w  ww.ja va2  s . c om
        public Boolean call() throws HueException {
            long millis = (transitionTime == null ? 0L
                    : transitionTime.convertTo(TimePeriod.MILLISECOND).longValue());

            return changeColor(newColor, millis);
        }
    });
}