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.hortonworks.hbase.BufferedMutatorExample.java

@Override
public int run(String[] args) throws InterruptedException, ExecutionException, TimeoutException {

    /** a callback invoked when an asynchronous write fails. */
    final BufferedMutator.ExceptionListener listener = new BufferedMutator.ExceptionListener() {
        @Override//from   w  w  w  .  j  a v a  2s. com
        public void onException(RetriesExhaustedWithDetailsException e, BufferedMutator mutator) {
            for (int i = 0; i < e.getNumExceptions(); i++) {
                LOG.info("Failed to send put " + e.getRow(i) + ".");
            }
        }
    };
    BufferedMutatorParams params = new BufferedMutatorParams(TABLE).listener(listener);

    //
    // step 1: create a single Connection and a BufferedMutator, shared by all worker threads.
    //
    Configuration conf = new Configuration();
    try (final Connection conn = ConnectionFactory.createConnection(conf);
            final BufferedMutator mutator = conn.getBufferedMutator(params)) {

        conf.set("hbase.zookeeper.quorum", "sandbox.hortonworks.com");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("zookeeper.znode.parent", "/hbase-unsecure");

        //        conf.set("hbase.zookeeper.quorum", "jetmaster2.jetnetname.artem.com,jetslave5.jetnetname.artem.com,jetslave1.jetnetname.artem.com");
        //        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //        conf.set("zookeeper.znode.parent", "/hbase-unsecure");

        /** worker pool that operates on BufferedTable instances */
        final ExecutorService workerPool = Executors.newFixedThreadPool(POOL_SIZE);
        List<Future<Void>> futures = new ArrayList<>(TASK_COUNT);

        for (int i = 0; i < TASK_COUNT; i++) {
            futures.add(workerPool.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    //
                    // step 2: each worker sends edits to the shared BufferedMutator instance. They all use
                    // the same backing buffer, call-back "listener", and RPC executor pool.
                    //
                    Put p = new Put(Bytes.toBytes("someRow"));
                    p.addColumn(FAMILY, Bytes.toBytes("someQualifier"), Bytes.toBytes("some value"));
                    mutator.mutate(p);
                    // do work... maybe you want to call mutator.flush() after many edits to ensure any of
                    // this worker's edits are sent before exiting the Callable
                    return null;
                }
            }));
        }

        //
        // step 3: clean up the worker pool, shut down.
        //
        for (Future<Void> f : futures) {
            f.get(5, TimeUnit.MINUTES);
        }
        workerPool.shutdown();
    } catch (IOException e) {
        // exception while creating/destroying Connection or BufferedMutator
        LOG.info("exception while creating/destroying Connection or BufferedMutator", e);
    } // BufferedMutator.close() ensures all work is flushed. Could be the custom listener is
      // invoked from here.
    return 0;
}

From source file:com.emc.vipr.sync.target.CasTarget.java

@Override
public void filter(final SyncObject obj) {
    timeOperationStart(CasUtil.OPERATION_TOTAL);

    if (!(obj instanceof CasSource.ClipSyncObject))
        throw new UnsupportedOperationException("sync object was not a CAS clip");
    final CasSource.ClipSyncObject clipSync = (CasSource.ClipSyncObject) obj;

    FPClip clip = null;//w  w  w  .  j a va 2s.co  m
    FPTag tag = null;
    int targetTagNum = 0;
    try {
        // first clone the clip via CDF raw write
        clip = TimingUtil.time(this, CasUtil.OPERATION_WRITE_CDF, new Callable<FPClip>() {
            @Override
            public FPClip call() throws Exception {
                return new FPClip(pool, clipSync.getRawSourceIdentifier(), clipSync.getInputStream(),
                        CLIP_OPTIONS);
            }
        });
        clipSync.setTargetIdentifier(clipSync.getRawSourceIdentifier());

        // next write the blobs
        for (ClipTag sourceTag : clipSync.getTags()) {
            tag = clip.FetchNext(); // this should sync the tag indexes
            if (sourceTag.isBlobAttached()) { // only stream if the tag has a blob
                timedStreamBlob(tag, sourceTag);
            }
            tag.Close();
            tag = null;
        }

        final FPClip fClip = clip;
        String destClipId = TimingUtil.time(this, CasUtil.OPERATION_WRITE_CLIP, new Callable<String>() {
            @Override
            public String call() throws Exception {
                return fClip.Write();
            }
        });
        if (!destClipId.equals(clipSync.getRawSourceIdentifier()))
            throw new RuntimeException(String.format("clip IDs do not match\n    [%s != %s]",
                    clipSync.getRawSourceIdentifier(), destClipId));

        LogMF.debug(l4j, "Wrote source {0} to dest {1}", clipSync.getSourceIdentifier(),
                clipSync.getTargetIdentifier());

        timeOperationComplete(CasUtil.OPERATION_TOTAL);
    } catch (Throwable t) {
        timeOperationFailed(CasUtil.OPERATION_TOTAL);
        if (t instanceof RuntimeException)
            throw (RuntimeException) t;
        throw new RuntimeException("Failed to store object: " + t.getMessage(), t);
    } finally {
        // close current tag ref
        try {
            if (tag != null)
                tag.Close();
        } catch (Throwable t) {
            l4j.warn("could not close tag " + clipSync.getRawSourceIdentifier() + "." + targetTagNum, t);
        }
        // close clip
        try {
            if (clip != null)
                clip.Close();
        } catch (Throwable t) {
            l4j.warn("could not close clip " + clipSync.getRawSourceIdentifier(), t);
        }
    }
}

From source file:com.microsoft.azure.management.resources.DeploymentOperationOperationsImpl.java

/**
* Get a list of deployments operations.//from w  ww .  j a  v  a 2  s.  c  om
*
* @param resourceGroupName Required. The name of the resource group. The
* name is case insensitive.
* @param deploymentName Required. The name of the deployment.
* @param operationId Required. Operation Id.
* @return Deployment operation.
*/
@Override
public Future<DeploymentOperationsGetResult> getAsync(final String resourceGroupName,
        final String deploymentName, final String operationId) {
    return this.getClient().getExecutorService().submit(new Callable<DeploymentOperationsGetResult>() {
        @Override
        public DeploymentOperationsGetResult call() throws Exception {
            return get(resourceGroupName, deploymentName, operationId);
        }
    });
}

From source file:com.metamx.druid.merger.coordinator.ForkingTaskRunner.java

@Override
public ListenableFuture<TaskStatus> run(final Task task) {
    synchronized (tasks) {
        if (!tasks.containsKey(task.getId())) {
            tasks.put(task.getId(), new TaskInfo(exec.submit(new Callable<TaskStatus>() {
                @Override//w ww .j  av  a 2s.co m
                public TaskStatus call() {
                    final String attemptUUID = UUID.randomUUID().toString();
                    final File taskDir = new File(config.getBaseTaskDir(), task.getId());
                    final File attemptDir = new File(taskDir, attemptUUID);

                    final ProcessHolder processHolder;

                    try {
                        if (!attemptDir.mkdirs()) {
                            throw new IOException(
                                    String.format("Could not create directories: %s", attemptDir));
                        }

                        final File taskFile = new File(attemptDir, "task.json");
                        final File statusFile = new File(attemptDir, "status.json");
                        final File logFile = new File(attemptDir, "log");

                        // time to adjust process holders
                        synchronized (tasks) {
                            final TaskInfo taskInfo = tasks.get(task.getId());

                            if (taskInfo.shutdown) {
                                throw new IllegalStateException("Task has been shut down!");
                            }

                            if (taskInfo == null) {
                                throw new ISE("WTF?! TaskInfo disappeared for task: %s", task.getId());
                            }

                            if (taskInfo.processHolder != null) {
                                throw new ISE("WTF?! TaskInfo already has a process holder for task: %s",
                                        task.getId());
                            }

                            final List<String> command = Lists.newArrayList();
                            final int childPort = findUnusedPort();
                            final String childHost = String.format(config.getHostPattern(), childPort);

                            command.add(config.getJavaCommand());
                            command.add("-cp");
                            command.add(config.getJavaClasspath());

                            Iterables.addAll(command, Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings()
                                    .split(config.getJavaOptions()));

                            for (String propName : props.stringPropertyNames()) {
                                if (propName.startsWith(CHILD_PROPERTY_PREFIX)) {
                                    command.add(String.format("-D%s=%s",
                                            propName.substring(CHILD_PROPERTY_PREFIX.length()),
                                            props.getProperty(propName)));
                                }
                            }

                            String nodeType = task.getNodeType();
                            if (nodeType != null) {
                                command.add(String.format("-Ddruid.executor.nodeType=%s", nodeType));
                            }

                            command.add(String.format("-Ddruid.host=%s", childHost));
                            command.add(String.format("-Ddruid.port=%d", childPort));

                            command.add(config.getMainClass());
                            command.add(taskFile.toString());
                            command.add(statusFile.toString());

                            jsonMapper.writeValue(taskFile, task);

                            log.info("Running command: %s", Joiner.on(" ").join(command));
                            taskInfo.processHolder = new ProcessHolder(
                                    new ProcessBuilder(ImmutableList.copyOf(command)).redirectErrorStream(true)
                                            .start(),
                                    logFile, childPort);

                            processHolder = taskInfo.processHolder;
                        }

                        log.info("Logging task %s output to: %s", task.getId(), logFile);

                        final OutputStream toProc = processHolder.process.getOutputStream();
                        final InputStream fromProc = processHolder.process.getInputStream();
                        final OutputStream toLogfile = Files.newOutputStreamSupplier(logFile).getOutput();

                        boolean runFailed = false;

                        try {
                            ByteStreams.copy(fromProc, toLogfile);
                            final int statusCode = processHolder.process.waitFor();
                            log.info("Process exited with status[%d] for task: %s", statusCode, task.getId());

                            if (statusCode != 0) {
                                runFailed = true;
                            }
                        } catch (Exception e) {
                            log.warn(e, "Failed to read from process for task: %s", task.getId());
                            runFailed = true;
                        } finally {
                            Closeables.closeQuietly(fromProc);
                            Closeables.closeQuietly(toLogfile);
                            Closeables.closeQuietly(toProc);
                        }

                        // Upload task logs

                        // XXX: Consider uploading periodically for very long-lived tasks to prevent
                        // XXX: bottlenecks at the end or the possibility of losing a lot of logs all
                        // XXX: at once.

                        taskLogPusher.pushTaskLog(task.getId(), logFile);

                        if (!runFailed) {
                            // Process exited successfully
                            return jsonMapper.readValue(statusFile, TaskStatus.class);
                        } else {
                            // Process exited unsuccessfully
                            return TaskStatus.failure(task.getId());
                        }
                    } catch (Exception e) {
                        log.info(e, "Exception caught during execution");
                        throw Throwables.propagate(e);
                    } finally {
                        try {
                            synchronized (tasks) {
                                final TaskInfo taskInfo = tasks.remove(task.getId());
                                if (taskInfo != null && taskInfo.processHolder != null) {
                                    taskInfo.processHolder.process.destroy();
                                }
                            }

                            log.info("Removing temporary directory: %s", attemptDir);
                            FileUtils.deleteDirectory(attemptDir);
                        } catch (Exception e) {
                            log.error(e, "Suppressing exception caught while cleaning up task");
                        }
                    }
                }
            })));
        }

        return tasks.get(task.getId()).statusFuture;
    }
}

From source file:com.microsoft.azure.management.sql.ServerUpgradeOperationsImpl.java

/**
* Cancel a pending upgrade for the Azure SQL Database server.
*
* @param resourceGroupName Required. The name of the Resource Group to
* which the server belongs./*from  w  w w .  ja  v a2 s.  co m*/
* @param serverName Required. The name of the Azure SQL Database Server to
* cancel upgrade.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public Future<OperationResponse> cancelAsync(final String resourceGroupName, final String serverName) {
    return this.getClient().getExecutorService().submit(new Callable<OperationResponse>() {
        @Override
        public OperationResponse call() throws Exception {
            return cancel(resourceGroupName, serverName);
        }
    });
}

From source file:com.microsoft.azure.utility.compute.ComputeTestBase.java

protected static void createComputeManagementClient() throws Exception {
    computeManagementClient = ComputeManagementService.create(config);
    if (IS_MOCKED) {
        computeManagementClient.setLongRunningOperationInitialTimeout(0);
        computeManagementClient.setLongRunningOperationRetryTimeout(0);
    } else {//from  w  ww .j av  a  2s  .co  m
        computeManagementClient.setLongRunningOperationRetryTimeout(10);
    }

    addClient((ServiceClient<?>) computeManagementClient, new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            createComputeManagementClient();
            return null;
        }
    });
}

From source file:com.techcavern.pircbotz.hooks.managers.ThreadedListenerManager.java

protected void submitEvent(ExecutorService pool, final Listener<B> listener, final Event<B> event) {
    pool.execute(new ManagedFutureTask(listener, event, new Callable<Void>() {
        public Void call() {
            try {
                if (event.getBot() != null)
                    Utils.addBotToMDC(event.getBot());
                listener.onEvent(event);
            } catch (Throwable e) {
                getExceptionHandler().onException(listener, event, e);
            }/*ww w.ja va2s .c o  m*/
            return null;
        }
    }));
}

From source file:ch.cyberduck.core.cryptomator.features.CryptoChecksumCompute.java

protected Checksum compute(final InputStream in, final long offset, final ByteBuffer header,
        final NonceGenerator nonces) throws ChecksumException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Calculate checksum with header %s", header));
    }/*from w w  w.  j  a  va 2s .  com*/
    try {
        final PipedOutputStream source = new PipedOutputStream();
        final CryptoOutputStream<Void> out = new CryptoOutputStream<Void>(new VoidStatusOutputStream(source),
                cryptomator.getCryptor(), cryptomator.getCryptor().fileHeaderCryptor().decryptHeader(header),
                nonces, cryptomator.numberOfChunks(offset));
        final PipedInputStream sink = new PipedInputStream(source,
                PreferencesFactory.get().getInteger("connection.chunksize"));
        final ThreadPool pool = ThreadPoolFactory.get("checksum", 1);
        try {
            final Future execute = pool.execute(new Callable<TransferStatus>() {
                @Override
                public TransferStatus call() throws Exception {
                    if (offset == 0) {
                        source.write(header.array());
                    }
                    final TransferStatus status = new TransferStatus();
                    new StreamCopier(status, status).transfer(in, out);
                    return status;
                }
            });
            try {
                return delegate.compute(sink, new TransferStatus());
            } finally {
                try {
                    execute.get();
                } catch (InterruptedException e) {
                    throw new ChecksumException(LocaleFactory.localizedString("Checksum failure", "Error"),
                            e.getMessage(), e);
                } catch (ExecutionException e) {
                    if (e.getCause() instanceof BackgroundException) {
                        throw (BackgroundException) e.getCause();
                    }
                    throw new DefaultExceptionMappingService().map(e.getCause());
                }
            }
        } finally {
            pool.shutdown(true);
        }
    } catch (ChecksumException e) {
        throw e;
    } catch (IOException | BackgroundException e) {
        throw new ChecksumException(LocaleFactory.localizedString("Checksum failure", "Error"), e.getMessage(),
                e);
    }
}

From source file:com.microsoft.azure.management.resources.TagOperationsImpl.java

/**
* Create a subscription resource tag./*from w w  w  .j  a v a  2s  . c  o m*/
*
* @param tagName Required. The name of the tag.
* @return Tag information.
*/
@Override
public Future<TagCreateResult> createOrUpdateAsync(final String tagName) {
    return this.getClient().getExecutorService().submit(new Callable<TagCreateResult>() {
        @Override
        public TagCreateResult call() throws Exception {
            return createOrUpdate(tagName);
        }
    });
}

From source file:no.ntnu.idi.socialhitchhiking.client.RequestTask.java

/**
 * Static method which adds elements and data to an xml file and sends it as a string to the server.
 * /* w  w w .  j  a  va 2  s . c  o  m*/
 * @param req - {@link Request}
 * @return returns a subclass of {@link Response} to the input {@link Request}
 * @throws ClientProtocolException 
 * @throws MalformedURLException
 * @throws FileNotFoundException
 * @throws IOException
 * @throws ExecutionException 
 * @throws InterruptedException 
 */
public static Response sendRequest(final Request req, final Context c)
        throws ClientProtocolException, IOException, InterruptedException, ExecutionException {
    /**
     * Code for putting all all network communication on separate thread as required by higher Android APIs
     */
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Callable<Response> callable = new Callable<Response>() {
        @Override
        /**
         * This contains the actual code for initiating the communication
         */
        public Response call() throws ClientProtocolException, IOException {
            String xml = RequestSerializer.serialize(req);
            con = c;
            String url = con.getResources().getString(R.string.server_url);
            RequestTask requestTask = new RequestTask(url, xml);

            return ResponseParser.parse(requestTask.getResponse());
        }
    };
    /**
     * Execute and retrieve result from network operation
     */
    Future<Response> future = executor.submit(callable);
    Response ret = future.get();
    executor.shutdown();
    return ret;
}