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.AbstractODataBasicRequestImpl.java

/**
 * {@inheritDoc}/*w  ww. j  a  v  a  2s.  co m*/
 */
@Override
public final Future<V> asyncExecute() {
    return odataClient.getConfiguration().getExecutor().submit(new Callable<V>() {

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

From source file:com.microsoft.azure.subscriptions.TenantOperationsImpl.java

/**
* Gets a list of the tenantIds.//from   w w w . ja va 2  s .c o m
*
* @return Tenant Ids information.
*/
@Override
public Future<TenantListResult> listAsync() {
    return this.getClient().getExecutorService().submit(new Callable<TenantListResult>() {
        @Override
        public TenantListResult call() throws Exception {
            return list();
        }
    });
}

From source file:org.javafunk.funk.jackson.monad.OptionDeserializer.java

/**
 * Method called to finalize setup of this deserializer, after deserializer itself has been registered. This is
 * needed to handle recursive and transitive dependencies.
 *//*from w w w.j a  v  a  2  s .  c o m*/
@Override
public JsonDeserializer<?> createContextual(final DeserializationContext context, final BeanProperty property)
        throws JsonMappingException {
    Option<JsonDeserializer<?>> contextualValueHandler = valueHandler
            .or(new Callable<Option<? extends JsonDeserializer<?>>>() {
                @Override
                public Option<? extends JsonDeserializer<?>> call() throws Exception {
                    return option(context.findContextualValueDeserializer(referenceType, property));
                }
            });

    Option<TypeDeserializer> contextualTypeHandler = typeHandler
            .flatMap(new UnaryFunction<TypeDeserializer, Option<? extends TypeDeserializer>>() {
                @Override
                public Option<? extends TypeDeserializer> call(TypeDeserializer typeDeserializer) {
                    return option(typeDeserializer.forProperty(property));
                }
            });

    if (contextualValueHandler.equals(valueHandler) && contextualTypeHandler.equals(typeHandler)) {
        return this;
    }

    return new OptionDeserializer(referenceType, contextualTypeHandler, contextualValueHandler);
}

From source file:com.microsoft.windowsazure.management.compute.ComputeManagementIntegrationTestBase.java

protected static void createComputeManagementClient() throws Exception {
    Configuration config = createConfiguration();
    computeManagementClient = ComputeManagementService.create(config);
    addClient((ServiceClient<?>) computeManagementClient, new Callable<Void>() {
        @Override// w w  w  .  j  av a  2s.c om
        public Void call() throws Exception {
            createComputeManagementClient();
            return null;
        }
    });
    addRegexRule("hostedservices/azhst[a-z]{10}");
}

From source file:org.zenoss.zep.dao.impl.DaoUtilsTest.java

@Test
public void testDeadlockRetryOtherException() throws Exception {
    final AtomicInteger i = new AtomicInteger();
    try {//from w  ww . ja v  a2s. c  o m
        DaoUtils.deadlockRetry(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                i.incrementAndGet();
                throw new RuntimeException("Bad exception - no retry");
            }
        });
        fail("Should have thrown an exception after first retry");
    } catch (RuntimeException e) {
        assertEquals(1, i.get());
        assertEquals("Bad exception - no retry", e.getMessage());
    }
}

From source file:ch.cyberduck.core.local.FileWatcher.java

public CountDownLatch register(final Local file, final FileWatcherListener listener) throws IOException {
    // Make sure to canonicalize the watched folder
    final Path folder = new File(file.getParent().getAbsolute()).getCanonicalFile().toPath();
    if (log.isDebugEnabled()) {
        log.debug(String.format("Register folder %s watching for file %s", folder, file));
    }/*from   w  ww.ja va 2  s .  co  m*/
    final WatchKey key = monitor.register(folder,
            new WatchEvent.Kind[] { ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY });
    if (!key.isValid()) {
        throw new IOException(String.format("Failure registering for events in %s", file));
    }
    final CountDownLatch lock = new CountDownLatch(1);
    pool.execute(new Callable<Boolean>() {
        @Override
        public Boolean call() throws IOException {
            while (true) {
                // wait for key to be signaled
                final WatchKey key;
                try {
                    lock.countDown();
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Wait for key from watch service %s", monitor));
                    }
                    key = monitor.take();
                } catch (ClosedWatchServiceException e) {
                    // If this watch service is closed
                    return true;
                } catch (InterruptedException e) {
                    return false;
                }
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Retrieved key %s from watch service %s", key, monitor));
                }
                for (WatchEvent<?> event : key.pollEvents()) {
                    final WatchEvent.Kind<?> kind = event.kind();
                    if (log.isInfoEnabled()) {
                        log.info(String.format("Detected file system event %s", kind.name()));
                    }
                    if (kind == OVERFLOW) {
                        log.error(String.format("Overflow event for %s", folder));
                        break;
                    }
                    // The filename is the context of the event. May be absolute or relative path name.
                    if (matches(normalize(LocalFactory.get(folder.toString()), event.context().toString()),
                            LocalFactory.get(folder.toString(), file.getName()))) {
                        callback(LocalFactory.get(folder.toString()), event, listener);
                    } else {
                        log.warn(String.format("Ignored file system event for unknown file %s",
                                event.context()));
                    }
                }
                // Reset the key -- this step is critical to receive further watch events.
                boolean valid = key.reset();
                if (!valid) {
                    // The key is no longer valid and the loop can exit.
                    return true;
                }
            }
        }
    });
    return lock;
}

From source file:org.zenoss.zep.impl.AbstractQueueListener.java

@Override
protected void receive(final Message<com.google.protobuf.Message> message,
        final Consumer<com.google.protobuf.Message> consumer) throws Exception {
    metricRegistry.timer(receiveMessageTimerName).time(new Callable<Object>() {
        @Override/*  ww  w  .j av a  2 s  . c o  m*/
        public Object call() throws Exception {
            AbstractQueueListener.this.executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        DaoUtils.deadlockRetry(new Callable<Object>() {
                            @Override
                            public Object call() throws Exception {
                                metricRegistry.timer(handleMessageTimerName).time(new Callable<Object>() {
                                    @Override
                                    public Object call() throws Exception {
                                        handle(message.getBody());
                                        return null;
                                    }
                                });
                                return null;
                            }
                        });
                        metricRegistry.timer(ackMessageTimerName).time(new Callable<Object>() {
                            @Override
                            public Object call() throws Exception {
                                consumer.ackMessage(message);
                                return null;
                            }
                        });
                    } catch (Exception e) {
                        if (ZepUtils.isExceptionOfType(e, TransientDataAccessException.class)) {
                            /* Re-queue the message if we get a temporary database failure */
                            logger.debug("Transient database exception", e);
                            logger.debug("Re-queueing message due to transient failure: {}", message);
                            rejectMessage(consumer, message, true);
                        } else if (!message.getEnvelope().isRedeliver()) {
                            /* Attempt one redelivery of the message */
                            logger.debug("First failure processing message: " + message, e);
                            rejectMessage(consumer, message, true);
                        } else {
                            /* TODO: Dead letter queue or other safety net? */
                            logger.warn("Failed processing message: " + message, e);
                            rejectMessage(consumer, message, false);
                        }
                    }
                }
            });
            return null;
        }
    });
}

From source file:com.microsoft.windowsazure.management.scheduler.SchedulerIntegrationTestBase.java

protected static void createSchedulerService(final String cloudServiceName, final String jobCollectionName)
        throws Exception {
    Configuration config = createConfiguration(cloudServiceName, jobCollectionName);
    config.setProperty(ApacheConfigurationProperties.PROPERTY_RETRY_HANDLER,
            new DefaultHttpRequestRetryHandler());
    schedulerClient = SchedulerService.create(config);
    addClient((ServiceClient<?>) schedulerClient, new Callable<Void>() {
        @Override/*from  w  w w.jav a 2s. c  o  m*/
        public Void call() throws Exception {
            createSchedulerService(cloudServiceName, jobCollectionName);
            return null;
        }
    });
}

From source file:com.link_intersystems.lang.reflect.SerializableMethodTest.java

@Test
public void noSuchMethod() throws Throwable {
    Method method = SerializableMethodTest.class.getDeclaredMethod("someTestMethod", int.class, int[].class,
            String.class);
    final SerializableMethod serializableMethod = new NoSuchMethodSerializableMethod(method);
    Assertion.assertCause(NoSuchMethodException.class, new Callable<Object>() {

        public Object call() throws Exception {
            SerializableMethod cloneSerializable = cloneSerializable(serializableMethod);
            return cloneSerializable;
        }/*  www .  ja va2 s  .  co m*/
    });
}

From source file:com.duy.pascal.interperter.libraries.android.connection.bluetooth.AndroidBluetoothLib.java

public AndroidBluetoothLib(AndroidLibraryManager manager) {
    mAndroidFacade = manager.getReceiver(AndroidUtilsLib.class);
    mBluetoothAdapter = MainThread.run(manager.getContext(), new Callable<BluetoothAdapter>() {
        @Override//from w ww . ja  va  2 s.  c o m
        public BluetoothAdapter call() throws Exception {
            return BluetoothAdapter.getDefaultAdapter();
        }
    });
}