Example usage for java.util.concurrent FutureTask FutureTask

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

Introduction

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

Prototype

public FutureTask(Callable<V> callable) 

Source Link

Document

Creates a FutureTask that will, upon running, execute the given Callable .

Usage

From source file:com.alibaba.napoli.metamorphosis.client.consumer.RecoverStorageManager.java

public Store getOrCreateStore(final String topic, final String group) {
    final String name = this.generateKey(topic, group);
    FutureTask<Store> task = this.topicStoreMap.get(name);
    if (task != null) {
        return this.getStore(topic, task);
    } else {//from  w ww.  ja  v a  2  s.  c om
        task = new FutureTask<Store>(new Callable<Store>() {

            @Override
            public Store call() throws Exception {
                final File file = new File(META_RECOVER_STORE_PATH + File.separator + name);
                if (!file.exists()) {
                    file.mkdir();
                }
                return new MessageStore(META_RECOVER_STORE_PATH + File.separator + name, name);
            }

        });
        FutureTask<Store> existsTask = this.topicStoreMap.putIfAbsent(name, task);
        if (existsTask == null) {
            task.run();
            existsTask = task;
        }
        return this.getStore(name, existsTask);
    }

}

From source file:uk.ac.sanger.cgp.wwdocker.daemon.WorkerDaemon.java

@Override
public void run(String mode)
        throws IOException, InterruptedException, TimeoutException, ConfigurationException {
    WorkerResources hr = new WorkerResources();
    logger.debug(Utils.objectToJson(hr));

    Thread shutdownThread = null;

    String qPrefix = config.getString("qPrefix");

    File thisConfig = new File("/opt/wwdocker/" + qPrefix + ".remote.cfg");
    File thisJar = Utils.thisJarFile();

    // build a local WorkerState
    WorkerState thisState = new WorkerState(thisJar, thisConfig);
    thisState.setStatus(HostStatus.CLEAN);
    String hostName = thisState.getResource().getHostName();

    // Remove from all queues as I'll set my state again now
    messaging.removeFromStateQueue(qPrefix.concat(".").concat("BROKEN"), hostName);
    messaging.removeFromStateQueue(qPrefix.concat(".").concat("CLEAN"), hostName);
    messaging.removeFromStateQueue(qPrefix.concat(".").concat("DONE"), hostName);
    messaging.removeFromStateQueue(qPrefix.concat(".").concat("ERROR"), hostName);
    messaging.removeFromStateQueue(qPrefix.concat(".").concat("ERRORLOGS"), hostName);
    messaging.removeFromStateQueue(qPrefix.concat(".").concat("RECEIVE"), hostName);
    messaging.removeFromStateQueue(qPrefix.concat(".").concat("RUNNING"), hostName);

    // I'm running so send a message to the CLEAN queue
    messaging.sendMessage(qPrefix.concat(".CLEAN"), thisState);
    boolean firstCleanIter = true;
    String myQueue = qPrefix.concat(".").concat(hostName);

    int counter = 30;
    Workflow workflowImp = new WorkflowFactory().getWorkflow(config);
    int failedRmqGet = 0;
    while (true) {
        Thread.sleep(500); // don't eat cpu
        //Only control messages will be sent directly to the host now

        WorkerState recievedState = null;
        try {/*from  w w w.  j  a v  a 2  s . c  o  m*/
            recievedState = (WorkerState) messaging.getWorkerState(myQueue, 10);
            failedRmqGet = 0;
        } catch (IOException e) {
            failedRmqGet++;
            if (failedRmqGet == 10) {
                logger.fatal("Failed to communicate with RMQ server 10 times, aborting.", e);
                System.exit(1);
            }
            logger.warn("Failed to communicate with RMQ server, allowable for 10 iterations only.", e);
        }

        thisState.getResource().init();

        if (recievedState != null) {
            if (!recievedState.equals(thisState) && thisState.getStatus().equals(HostStatus.CLEAN)) {
                messaging.removeFromStateQueue(qPrefix.concat(".").concat(thisState.getStatus().name()),
                        hostName);
                logger.fatal("Host refresh required, shutting down...");
                System.exit(0);
            }
            if (recievedState.getChangeStatusTo() != null) {
                if (recievedState.getChangeStatusTo().equals(HostStatus.KILL)) {
                    messaging.removeFromStateQueue(qPrefix.concat(".").concat(thisState.getStatus().name()),
                            hostName);
                    messaging.removeFromStateQueue(qPrefix.concat(".").concat("RUNNING"), hostName); // this is never changed unless a host dies/killed
                    if (thisState.getStatus().equals(HostStatus.ERROR)) {
                        messaging.removeFromStateQueue(qPrefix.concat(".").concat("ERRORLOGS"), hostName);
                    }
                    if (!thisState.getStatus().equals(HostStatus.CLEAN)) {
                        if (shutdownThread == null) {
                            messaging.sendMessage(qPrefix.concat(".").concat("PEND"),
                                    Utils.objectToJson(thisState.getWorkflowIni()));
                        }
                    }
                    logger.fatal("FORCED SHUTDOWN...");
                    if (dockerThread != null) {
                        Local.execCommand("docker ps | tail -n +2 | cut -d ' ' -f 1 | xargs docker kill",
                                Config.getEnvs(config), true);
                        futureTask.cancel(true);
                        executor.shutdownNow();
                    }
                    System.exit(0);
                } else if (recievedState.getChangeStatusTo().equals(HostStatus.CHECKIN)) {
                    logger.info(recievedState.toString());
                    messaging.sendMessage(recievedState.getReplyToQueue(), thisState);
                } else if (recievedState.getChangeStatusTo().equals(HostStatus.RUNNING)) {
                    // this is only sent if we want to retry the execution of an errored workflow
                    throw new RuntimeException("Restart attempted, I don't know how yet");
                }
            }
        }

        // then we do the actual work
        if (thisState.getStatus().equals(HostStatus.CLEAN)) {

            // clean up any other queues that may have legacy entries, boolean to prevent rapid query rates
            if (firstCleanIter) {
                messaging.removeFromStateQueue(qPrefix.concat(".").concat("DONE"), hostName);
                messaging.removeFromStateQueue(qPrefix.concat(".").concat("ERROR"), hostName);
                messaging.removeFromStateQueue(qPrefix.concat(".").concat("ERRORLOGS"), hostName);
                messaging.removeFromStateQueue(qPrefix.concat(".").concat("RECEIVE"), hostName);
                messaging.removeFromStateQueue(qPrefix.concat(".").concat("RUNNING"), hostName);
                messaging.removeFromStateQueue(qPrefix.concat(".").concat("BROKEN"), hostName);
                firstCleanIter = false;
            }

            //We pull data from the wwd_PEND queue
            WorkflowIni workIni = (WorkflowIni) messaging.getMessageObject(qPrefix.concat(".").concat("PEND"),
                    WorkflowIni.class, 10);
            if (workIni == null) {
                continue;
            }
            logger.debug(thisState.toString());
            thisState.setWorkflowIni(workIni);
            shutdownThread = attachWorkIniShutdownHook(thisState.getWorkflowIni(), messaging, qPrefix,
                    hostName);
            workflowImp.cleanDockerPath(config); // clean up the workarea

            dockerThread = new Docker(workIni, config);

            futureTask = new FutureTask<>(dockerThread);
            executor = Executors.newSingleThreadExecutor();
            executor.execute(futureTask);
            // this section saves having to check you've got it right
            messaging.removeFromStateQueue(qPrefix.concat(".").concat(thisState.getStatus().name()), hostName);
            thisState.setStatus(HostStatus.RUNNING);
            messaging.sendMessage(qPrefix.concat(".").concat(thisState.getStatus().name()), thisState);
        } else if (thisState.getStatus().equals(HostStatus.RUNNING)) {
            if (futureTask.isDone()) {
                try {
                    int dockerExitCode = futureTask.get();
                    logger.info("Exit code: " + dockerExitCode);
                    if (dockerExitCode == 0) {
                        thisState.setStatus(HostStatus.DONE);
                        messaging.sendMessage(qPrefix.concat(".").concat("UPLOADED"),
                                thisState.getWorkflowIni());
                    } else {
                        if (dockerThread.getLogArchive() != null) {
                            messaging.sendFile(qPrefix.concat(".").concat("ERRORLOGS"), hostName,
                                    dockerThread.getLogArchive());
                        }
                        thisState.setStatus(HostStatus.ERROR);
                    }

                    Runtime.getRuntime().removeShutdownHook(shutdownThread);
                    messaging.removeFromStateQueue(qPrefix.concat(".").concat("RUNNING"), hostName);
                    messaging.sendMessage(qPrefix.concat(".").concat(thisState.getStatus().name()), thisState);
                    shutdownThread = null;

                    executor.shutdown();

                    dockerThread = null;
                    executor = null;
                    futureTask = null;
                } catch (InterruptedException | ExecutionException | IOException e) {
                    logger.warn(e.getMessage(), e);
                    thisState.setStatus(HostStatus.ERROR);
                }
            }
        } else if (thisState.getStatus().equals(HostStatus.DONE)) {
            /* if we need to handle working without GNOS access on images
               then we need to change the logic here to wait for a
               state change pushed from the control code */
            messaging.removeFromStateQueue(qPrefix.concat(".").concat(thisState.getStatus().name()), hostName);
            thisState.setStatus(HostStatus.CLEAN);
            firstCleanIter = true;
            thisState.setWorkflowIni(null);
            messaging.sendMessage(qPrefix.concat(".").concat(thisState.getStatus().name()), thisState);
        } else if (thisState.getStatus().equals(HostStatus.ERROR)) {
            if (counter == 60) {
                logger.debug("I'm set to error, waiting for directions...");
                counter = 0;
            }
            counter++;
            Thread.sleep(500); // sleep at top too
        } else {
            throw new RuntimeException("Don't know what to do yet");
        }
    }
}

From source file:com.google.android.dialer.provider.DialerProvider.java

private <T> T execute(Callable<T> callable, String name, long time, TimeUnit timeUnit) {
    FutureCallable<T> futureCallable = new FutureCallable<T>(callable);
    FutureTask<T> future = new FutureTask<T>(futureCallable);
    futureCallable.setFuture(future);/*ww  w .  j a  v a 2s  .com*/

    synchronized (mActiveTasks) {
        mActiveTasks.addLast(future);
        if (Log.isLoggable("DialerProvider", Log.VERBOSE)) {
            Log.v("DialerProvider", "Currently running tasks: " + mActiveTasks.size());
        }
        while (mActiveTasks.size() > 8) {
            Log.w("DialerProvider", "Too many tasks, canceling one");
            mActiveTasks.removeFirst().cancel(true);
        }
    }

    if (Log.isLoggable("DialerProvider", Log.VERBOSE)) {
        Log.v("DialerProvider", "Starting task " + name);
    }

    new Thread(future, name).start();
    try {
        if (Log.isLoggable("DialerProvider", Log.VERBOSE)) {
            Log.v("DialerProvider", "Getting future " + name);
        }
        return future.get(time, timeUnit);
    } catch (InterruptedException e) {
        Log.w("DialerProvider", "Task was interrupted: " + name);
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        Log.w("DialerProvider", "Task threw an exception: " + name, e);
    } catch (TimeoutException e) {
        Log.w("DialerProvider", "Task timed out: " + name);
        future.cancel(true);
    } catch (CancellationException e) {
        Log.w("DialerProvider", "Task was cancelled: " + name);
    }

    // TODO: Is this appropriate?
    return null;
}

From source file:org.esigate.extension.parallelesi.IncludeElement.java

@Override
public void onTagEnd(String tag, FutureParserContext ctx) throws IOException, HttpErrorPage {
    write = true;//from   w  w  w .j a  v  a 2  s . co m
    String src = includeTag.getAttribute("src");
    String alt = includeTag.getAttribute("alt");
    boolean ignoreError = "continue".equals(includeTag.getAttribute("onerror"));
    FutureElement current = ctx.getCurrent();
    // write accumulated data into parent
    Executor executor = (Executor) ctx.getData(EsiRenderer.DATA_EXECUTOR);
    Future<CharSequence> result = null;
    IncludeTask task = new IncludeTask(includeTag, src, alt, ctx, current, ignoreError, fragmentReplacements,
            regexpReplacements, executor);
    if (executor == null) {
        // No threads.
        CharSequence content = task.call();
        result = new CharSequenceFuture(content);
    } else {
        // Start processing in a new thread.
        RunnableFuture<CharSequence> r = new FutureTask<CharSequence>(task);
        executor.execute(r);
        result = r;
    }
    ctx.getCurrent().characters(result);
}

From source file:com.luke.lukef.lukeapp.tools.LukeNetUtils.java

/**
 * Creates a Bitmap from the given URL/*from   w w w.ja  va  2  s. c o m*/
 *
 * @param imageUrl The URL of the image
 * @return The image as an Bitmap object, otherwise <b>null</b>
 * @throws ExecutionException
 * @throws InterruptedException
 */
public Bitmap getBitmapFromURL(final String imageUrl) throws ExecutionException, InterruptedException {
    Callable<Bitmap> bitmapCallable = new Callable<Bitmap>() {
        @Override
        public Bitmap call() throws Exception {
            try {
                URL url = new URL(imageUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                return BitmapFactory.decodeStream(input);
            } catch (IOException e) {
                Log.e(TAG, "call: ", e);
                return null;
            }
        }
    };
    FutureTask<Bitmap> bitmapFutureTask = new FutureTask<>(bitmapCallable);
    Thread t = new Thread(bitmapFutureTask);
    t.start();
    return bitmapFutureTask.get();
}

From source file:io.teak.sdk.DeviceConfiguration.java

private void registerForGCM(@NonNull final AppConfiguration appConfiguration) {
    try {/*w w  w .j a  v a  2  s .  c om*/
        if (appConfiguration.pushSenderId != null) {
            final DeviceConfiguration _this = this;

            final FutureTask<String> gcmRegistration = new FutureTask<>(
                    new RetriableTask<>(100, 7000L, new Callable<String>() {
                        @Override
                        public String call() throws Exception {
                            GoogleCloudMessaging gcm = _this.gcm.get();

                            if (Teak.isDebug) {
                                Log.d(LOG_TAG,
                                        "Registering for GCM with sender id: " + appConfiguration.pushSenderId);
                            }
                            return gcm.register(appConfiguration.pushSenderId);
                        }
                    }));
            new Thread(gcmRegistration).start();

            new Thread(new Runnable() {
                public void run() {
                    try {
                        String registration = gcmRegistration.get();

                        if (registration == null) {
                            Log.e(LOG_TAG, "Got null token during GCM registration.");
                            return;
                        }

                        if (_this.preferences != null) {
                            SharedPreferences.Editor editor = _this.preferences.edit();
                            editor.putInt(PREFERENCE_APP_VERSION, appConfiguration.appVersion);
                            editor.putString(PREFERENCE_GCM_ID, registration);
                            editor.apply();
                        }

                        // Inform event listeners GCM is here
                        if (!registration.equals(gcmId)) {
                            _this.gcmId = registration;
                            synchronized (eventListenersMutex) {
                                for (EventListener e : eventListeners) {
                                    e.onGCMIdChanged(_this);
                                }
                            }
                        }

                        displayGCMDebugMessage();
                    } catch (Exception e) {
                        Log.e(LOG_TAG, Log.getStackTraceString(e));
                    }
                }
            }).start();
        }
    } catch (Exception ignored) {
    }
}

From source file:info.pancancer.arch3.test.TestWorker.java

@Test
public void testWorker_endless() throws Exception {

    byte[] body = setupMessage();
    Delivery testDelivery = new Delivery(mockEnvelope, mockProperties, body);
    setupMockQueue(testDelivery);/*  w  w w .  ja v a2s .  co m*/
    Mockito.when(Utilities.parseJSONStr(anyString())).thenCallRealMethod();
    Mockito.when(Utilities.parseConfig(anyString())).thenCallRealMethod();

    //Because the code that does cleanup in calls resultHandler.waitFor(); we need to actually execute something, even if it does nothing.
    Mockito.doNothing().when(mockExecutor).execute(any(CommandLine.class),
            any(DefaultExecuteResultHandler.class));

    // This is to mock the cleanup command - we don't really want to execute the command for deleting contents of /datastore, at least not when unit testing on a workstation!
    PowerMockito.whenNew(DefaultExecutor.class).withNoArguments().thenReturn(mockExecutor);

    Mockito.when(mockExecHandler.hasResult()).thenReturn(true);
    PowerMockito.whenNew(DefaultExecuteResultHandler.class).withNoArguments().thenReturn(mockExecHandler);

    final FutureTask<String> tester = new FutureTask<>(new Callable<String>() {
        @Override
        public String call() {
            LOG.debug("tester thread started");
            try {
                Worker.main(new String[] { "--config", "src/test/resources/workerConfig.ini", "--uuid",
                        "vm123456", "--endless", "--pidFile", "/var/run/arch3_worker.pid" });
            } catch (CancellationException | InterruptedException e) {
                LOG.error("Exception caught: " + e.getMessage());
                return e.getMessage();
            } catch (Exception e) {
                e.printStackTrace();
                fail("Unexpected exception");
                return null;
            } finally {
                Mockito.verify(mockAppender, Mockito.atLeastOnce()).doAppend(argCaptor.capture());
                String s = appendEventsIntoString(argCaptor.getAllValues());
                return s;
            }
        }

    });

    final Thread killer = new Thread(new Runnable() {

        @Override
        public void run() {
            LOG.debug("killer thread started");
            try {
                // The endless worker will not end on its own (because it's endless) so we need to wait a little bit (0.5 seconds) and
                // then kill it as if it were killed by the command-line script (kill_worker_daemon.sh).
                Thread.sleep(2500);
            } catch (InterruptedException e) {
                e.printStackTrace();
                LOG.error(e.getMessage());
            }
            tester.cancel(true);
        }
    });

    ExecutorService es = Executors.newFixedThreadPool(2);
    es.execute(tester);
    es.execute(killer);
    try {
        tester.get();
    } catch (CancellationException e) {
        Mockito.verify(mockAppender, Mockito.atLeastOnce()).doAppend(argCaptor.capture());
        List<LoggingEvent> tmpList = new LinkedList<>(argCaptor.getAllValues());
        String output = this.appendEventsIntoString(tmpList);
        assertTrue(output.contains("The \"--endless\" flag was set, this worker will run endlessly!"));

        int numJobsPulled = StringUtils.countMatches(output, " WORKER IS PREPARING TO PULL JOB FROM QUEUE ");

        LOG.info("Number of jobs attempted: " + numJobsPulled);
        assertTrue("number of jobs attempted > 1", numJobsPulled > 1);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}

From source file:voltcache.api.VoltCache.java

/**
 * Asynchronously Executes an operation//from  w w w.ja v  a  2  s .com
 * @param type Type of response expected
 * @param procedure Name of the VoltProcedure to call on the server
 * @param parameters Ordered list of procedure parameters
 * @returns Future result of the operation
 */
private Future<VoltCacheResult> asyncExecute(VoltCacheResult.Type type, final String procedure,
        final Object... parameters) {
    try {
        Callable<ClientResponse> callable = new Callable<ClientResponse>() {
            public ClientResponse call() {
                ClientResponse response = null;
                try {
                    response = client.callProcedure(procedure, parameters);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return response;
            }
        };

        FutureTask<ClientResponse> future = new FutureTask<ClientResponse>(callable);

        return VoltCacheFuture.cast(type, future);
    } catch (Exception x) {
        return VoltCacheFuture.fail();
    }
}

From source file:com.alibaba.napoli.metamorphosis.client.producer.ProducerZooKeeper.java

public void publishTopic(final String topic) {
    if (this.topicConnectionListeners.get(topic) != null) {
        return;//from w  w  w  .  j a  va  2  s. c om
    }
    final FutureTask<BrokerConnectionListener> task = new FutureTask<BrokerConnectionListener>(
            new Callable<BrokerConnectionListener>() {
                @Override
                public BrokerConnectionListener call() throws Exception {
                    final BrokerConnectionListener listener = new BrokerConnectionListener(topic);
                    if (ProducerZooKeeper.this.zkClient != null) {
                        ProducerZooKeeper.this.publishTopicInternal(topic, listener);
                    }
                    return listener;
                }

            });

    final FutureTask<BrokerConnectionListener> existsTask = this.topicConnectionListeners.putIfAbsent(topic,
            task);
    if (existsTask == null) {
        task.run();
    }
}

From source file:uk.bl.dpt.qa.ProcessIsolatedTika.java

/**
 * Parse an inputstream and populate a Metadata object
 * @param pInputStream stream to analyse 
 * @param pMetadata metadata object to populate
 * @param pOutputStream output to write data to
 * @return true if processed ok, false if execution was terminated
 *//*from www. j a v a 2 s  .c o  m*/
public boolean parse(final InputStream pInputStream, final Metadata pMetadata) {

    boolean ret = true;

    if (!gRunner.isRunning()) {
        gLogger.error("Tika-Server is not running");
        return false;
    }

    final String TIKA_PATH = "/meta";
    final String END_POINT = "http://" + TIKA_LOCAL_HOST + ":" + TIKA_SERVER_PORT;

    gLogger.trace("Server: " + END_POINT + TIKA_PATH);

    final String detectedType = pMetadata.get(Metadata.CONTENT_TYPE);

    FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
        @Override
        public Integer call() throws Exception {

            gResponse = WebClient.create(END_POINT + TIKA_PATH).accept("text/csv")
                    // give the parsers a hint
                    .type(detectedType)
                    // protect the stream from being closed
                    .put(new CloseShieldInputStream(pInputStream));

            return null;
        }
    });

    Thread thread = new Thread(task);
    thread.start();

    try {
        task.get(TIMEOUT_SECS * 1000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        gLogger.info("InterruptedException: " + e);
        ret = false;
        restart();
    } catch (ExecutionException e) {
        gLogger.info("ExecutionException: " + e);
        ret = false;
        restart();
    } catch (TimeoutException e) {
        gLogger.info("TimeoutException: " + e);
        ret = false;
        restart();
    }

    if (gResponse != null) {
        if (gResponse.getStatus() == Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()) {
            // the server may return HTTP 415 (unsupported) if it won't accept the mimetype
            // handle this issue here
            // add some text to the output
            // FIXME: maybe change mimetype for a more visible error?
            pMetadata.add("parseFailure415", "true");
            gLogger.error("Parse Failure: HTTP 415 (format unsupported for parsing)");
        } else {
            if (gResponse.getEntity() instanceof InputStream) {
                InputStream is = (InputStream) gResponse.getEntity();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                try {
                    Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(reader);
                    for (CSVRecord record : records) {
                        pMetadata.add(record.get(0), record.get(1));
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                    ret = false;
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    gLogger.info("Metadata entries: " + pMetadata.names().length);

    return ret;
}