Example usage for java.util Timer schedule

List of usage examples for java.util Timer schedule

Introduction

In this page you can find the example usage for java.util Timer schedule.

Prototype

public void schedule(TimerTask task, Date time) 

Source Link

Document

Schedules the specified task for execution at the specified time.

Usage

From source file:com.fennex.modules.NotificationService.java

/**
 * The IntentService calls this method from the default worker thread with
 * the intent that started the service. When this method returns, IntentService
 * stops the service, as appropriate./*from w  ww .  j  a  v a  2 s.c o m*/
 */
@Override
protected void onHandleIntent(final Intent intent) {
    timeFromNow = intent.getExtras().getFloat("timeFromNow");
    Log.i("LocalNotification", "scheduleNotification -> timeFromNow: " + timeFromNow);
    Timer t = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            createNotif(intent);
        }
    };
    t.schedule(task, (long) (timeFromNow * 1000));
}

From source file:de.uni_koeln.spinfo.maalr.webapp.controller.EditorController.java

@Override
public String export(Set<String> fields, EditorQuery query) throws NoDatabaseAvailableException, IOException {
    File dir = new File("exports");
    dir.mkdirs();/* w w  w.  j  a v  a2 s. c  o m*/
    final File tmp = new File(dir, "export_" + UUID.randomUUID() + ".tsv.zip");
    Timer timer = new Timer();
    service.export(fields, query, tmp);
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            if (tmp.exists()) {
                System.out.println("Deleting file " + tmp);
                tmp.delete();
            }
        }
    }, 60000 * 30);
    return tmp.getName();
}

From source file:de.uni_koeln.spinfo.maalr.webapp.controller.EditorController.java

@Override
public String export(Set<String> fields, MaalrQuery query) throws Exception {
    File dir = new File("exports");
    dir.mkdirs();/*from  www  .  j a v a 2 s .  com*/
    final File tmp = new File(dir, "export_" + UUID.randomUUID() + ".tsv.zip");
    Timer timer = new Timer();
    service.export(fields, query, tmp);
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            if (tmp.exists()) {
                System.out.println("Deleting file " + tmp);
                tmp.delete();
            }
        }
    }, 60000 * 30);
    return tmp.getName();
}

From source file:jahirfiquitiva.iconshowcase.dialogs.WallpaperDialog.java

@SuppressLint("InflateParams")
@NonNull/* w  w  w .j av  a 2s. c  om*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    WallpaperEvent.Step step = (WallpaperEvent.Step) getArguments().getSerializable("wall_step");
    if (step == null)
        step = WallpaperEvent.Step.START;

    final MaterialDialog.Builder[] builder = { new MaterialDialog.Builder(getActivity()) };
    final boolean[] enteredApplyTask = { false };

    switch (step) {
    case START:
        builder[0].title(R.string.apply).content(R.string.confirm_apply).positiveText(R.string.apply)
                .negativeText(android.R.string.cancel).onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog materialDialog,
                            @NonNull final DialogAction dialogAction) {
                        showBase(getActivity(), getUrl(), WallpaperEvent.Step.LOADING);
                    }
                });
        break;
    case LOADING:

        final ApplyWallpaper task = new ApplyWallpaper(getActivity(), getUrl(),
                new ApplyWallpaper.ApplyCallback() {
                    @Override
                    public void afterApplied() {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dismiss();
                                builder[0] = new MaterialDialog.Builder(getActivity());
                                builder[0].content(R.string.set_as_wall_done).positiveText(android.R.string.ok)
                                        .show();
                                if (getActivity() instanceof ShowcaseActivity) {
                                    if (((ShowcaseActivity) getActivity()).isWallsPicker()) {
                                        getActivity().finish();
                                    }
                                }
                            }
                        });
                    }
                }, new ApplyWallpaper.DownloadCallback() {
                    @Override
                    public void afterDownloaded() {
                        //TODO: Properly show the "Setting wallpaper..." dialog
                        showBase(getActivity(), getUrl(), WallpaperEvent.Step.APPLYING);
                    }
                });

        builder[0].content(R.string.downloading_wallpaper).progress(true, 0).cancelable(false)
                .onPositive(new MaterialDialog.SingleButtonCallback() { //TODO set
                    // positive text?
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        task.cancel(true);
                        dismiss();
                    }
                });

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (!enteredApplyTask[0]) {
                                String newContent = getActivity().getString(R.string.downloading_wallpaper)
                                        + "\n" + getActivity().getString(R.string.download_takes_longer);
                                builder[0].content(newContent).positiveText(android.R.string.cancel);
                            }
                        }
                    });
                }
            }
        }, 10000);

        task.execute();
        enteredApplyTask[0] = true;
        break;
    case APPLYING:
        builder[0].content(R.string.setting_wall_title).progress(true, 0).cancelable(false);
        break;
    default:
        builder[0].title(R.string.error); //TODO put to R.string
        break;
    }

    return builder[0].build();
}

From source file:org.camunda.bpm.example.spring.servlet.pa.ArquillianTest.java

public void waitForJobExecutorToProcessAllJobs(JobExecutor jobExecutor, long maxMillisToWait) {

    int checkInterval = 1000;

    jobExecutor.start();/*from   w ww  .  j  a  v  a2 s .  co  m*/

    try {
        Timer timer = new Timer();
        InteruptTask task = new InteruptTask(Thread.currentThread());
        timer.schedule(task, maxMillisToWait);
        boolean areJobsAvailable = true;
        try {
            while (areJobsAvailable && !task.isTimeLimitExceeded()) {
                Thread.sleep(checkInterval);
                areJobsAvailable = areJobsAvailable();
            }
        } catch (InterruptedException e) {
        } finally {
            timer.cancel();
        }
        if (areJobsAvailable) {
            throw new ProcessEngineException("time limit of " + maxMillisToWait + " was exceeded");
        }

    } finally {
        jobExecutor.shutdown();
    }
}

From source file:org.jbpm.db.AbstractDbTestCase.java

private void processAllJobs(final long maxWait, int maxJobs) {
    boolean jobsAvailable = true;

    // install a timer that will interrupt if it takes too long
    // if that happens, it will lead to an interrupted exception and the test
    // will fail//  w  ww . ja  va  2 s . c  o m
    TimerTask interruptTask = new TimerTask() {
        Thread testThread = Thread.currentThread();

        public void run() {
            log.debug("test " + getName() + " took too long. going to interrupt...");
            testThread.interrupt();
        }
    };
    Timer timer = new Timer();
    timer.schedule(interruptTask, maxWait);

    try {
        while (jobsAvailable) {
            log.debug("going to sleep for 200 millis, waiting for the job executor to process more jobs");
            Thread.sleep(200);
            jobsAvailable = (getNbrOfJobsAvailable() > maxJobs);
        }
        jobExecutor.stopAndJoin();

    } catch (InterruptedException e) {
        fail("test execution exceeded treshold of " + maxWait + " milliseconds");
    } finally {
        timer.cancel();
    }
}

From source file:deincraftlauncher.IO.download.FTPDownloader.java

private void update() {

    System.out.println("downloader update #" + updateNum);
    updateNum++;/* w  w  w.  ja va2s  . c  o m*/

    if (finished) {
        System.out.println("cancelling updating");
        return;
    }

    onUpdate.call(totalProgress, totalSize, this);

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            //periodic update
            timer.cancel();
            timer.purge();
            update();
        }
    }, updateDelay);

}

From source file:org.jbpm.bpel.persistence.db.AbstractDbTestCase.java

private void processAllJobs(final long maxWait) {
    boolean jobsAvailable = true;

    // install a timer that will interrupt if it takes too long
    // if that happens, it will lead to an interrupted exception and the test
    // will fail//from w  w  w .j ava2 s  .  com
    TimerTask interruptTask = new TimerTask() {

        Thread testThread = Thread.currentThread();

        public void run() {
            log.debug("test " + getName() + " took too long. going to interrupt...");
            testThread.interrupt();
        }
    };
    Timer timer = new Timer();
    timer.schedule(interruptTask, maxWait);

    try {
        while (jobsAvailable) {
            log.debug("going to sleep for 200 millis, waiting for the job executor to process more jobs");
            Thread.sleep(200);
            jobsAvailable = areJobsAvailable();
        }
        jobExecutor.stopAndJoin();
    } catch (InterruptedException e) {
        fail("test execution exceeded treshold of " + maxWait + " milliseconds");
    } finally {
        timer.cancel();
    }
}

From source file:kieker.tools.resourceMonitor.ResourceMonitor.java

@Override
protected boolean performTask() {
    LOG.info(this.toString());

    final CountDownLatch cdl = new CountDownLatch(1);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override//from w w  w.j a v  a2s .  c  o m
        public void run() {
            cdl.countDown();
        }
    });

    final Configuration controllerConfiguration;
    if (this.monitoringConfigurationFile != null) {
        controllerConfiguration = ConfigurationFactory
                .createConfigurationFromFile(this.monitoringConfigurationFile);
    } else {
        controllerConfiguration = ConfigurationFactory.createSingletonConfiguration();
    }
    this.monitoringController = MonitoringController.createInstance(controllerConfiguration);

    this.initSensors();
    LOG.info("Monitoring started");

    if (this.duration >= 0) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                cdl.countDown();
                timer.cancel();
            }
        }, TimeUnit.MILLISECONDS.convert(this.duration, this.durationUnit));
        LOG.info("Waiting for " + this.duration + " " + this.durationUnit + " timeout...");
    }

    try {
        LOG.info("Press Ctrl+c to terminate");
        cdl.await();
    } catch (final InterruptedException ex) {
        LOG.warn("The monitoring has been interrupted", ex);
        return false;
    } finally {
        LOG.info("Monitoring terminated");
    }

    return true;
}

From source file:org.apache.geronimo.shell.geronimo.ProcessLauncher.java

public void launch() throws Exception {
    assert name != null;

    Runnable runner = new Inner();

    Thread t = new Thread(runner, name + " Runner");

    out.println("Launching " + name + "...");
    //System.console().flush();

    StopWatch watch = new StopWatch();
    watch.start();//from w ww  .j  ava  2 s  .  c  om

    t.start();

    if (verifier()) {
        Timer timer = new Timer(name + " Timer", true);

        TimerTask timeoutTask = new TimingTimerTask();
        if (timeout > 0) {
            timer.schedule(timeoutTask, timeout * 1000);

        }

        boolean started = false;

        log.debug("Waiting for " + name + " ...");

        while (!started) {
            if (timedOut) {
                throw new Exception("Unable to verify if " + name + " was started in the given time (" + timeout
                        + " seconds)");
            }

            if (error != null) {
                throw new Exception("Failed to start: " + name, error);
            }

            if (verifier()) {
                started = true;
            } else {
                Thread.sleep(verifyWaitDelay);
            }
        }

        timeoutTask.cancel();
    }

    out.println(name + " started in " + watch);
    //System.console().flush();

    if (!background) {
        log.debug("Waiting for " + name + " to shutdown...");

        t.join();

        log.debug(name + " has shutdown");
    }
}