コード例 #1
0
ファイル: ChildOnBus.java プロジェクト: maffan/MinBusskompis
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_child_on_bus);
    viewHolder = new ViewHolder();

    // Set on bus status
    ParseCloudManager.getInstance().setStatusForSelfAndNotifyParents(TravelingData.ON_BUS);

    // Initiate views
    initiateViews();

    // Get data from intent
    travelingData = getIntent().getParcelableExtra("data");

    // Set trip information from travelingData
    viewHolder.busStopName.setText(travelingData.busStopName);
    viewHolder.timeToStop.setText(travelingData.busArrivingAt);

    // Check if next bus stop is yours, and if the bus have left the last one each 20s
    poolExecutor = new ScheduledThreadPoolExecutor(1);
    poolExecutor.scheduleWithFixedDelay(this, 0, 20, TimeUnit.SECONDS);

    // Dummy buttons, pass forward
    addButtonListener();
    // Init service
    Intent serviceIntent = new Intent(context, UpdateLocToParseService.class);
    bindService(serviceIntent, this, 0);
  }
コード例 #2
0
ファイル: Timer.java プロジェクト: clemensv/azure-event-hubs
  /** @param runFrequency implemented only for TimeUnit granularity - Seconds */
  public static void schedule(Runnable runnable, Duration runFrequency, TimerType timerType) {
    switch (timerType) {
      case OneTimeRun:
        long seconds = runFrequency.getSeconds();
        if (seconds > 0) executor.schedule(runnable, seconds, TimeUnit.SECONDS);
        else executor.schedule(runnable, runFrequency.toMillis(), TimeUnit.MILLISECONDS);
        break;

      case RepeatRun:
        executor.scheduleWithFixedDelay(
            runnable, runFrequency.getSeconds(), runFrequency.getSeconds(), TimeUnit.SECONDS);
        break;

      default:
        throw new UnsupportedOperationException("Unsupported timer pattern.");
    }
  }
コード例 #3
0
ファイル: JobsPlugin.java プロジェクト: hopcroft/databus
  @Override
  public void afterApplicationStart() {
    List<Class<?>> jobs = new ArrayList<Class<?>>();
    for (Class clazz : Play.classloader.getAllClasses()) {
      if (Job.class.isAssignableFrom(clazz)) {
        jobs.add(clazz);
      }
    }
    scheduledJobs = new ArrayList<Job>();
    for (final Class<?> clazz : jobs) {
      // @OnApplicationStart
      if (clazz.isAnnotationPresent(OnApplicationStart.class)) {
        // check if we're going to run the job sync or async
        OnApplicationStart appStartAnnotation = clazz.getAnnotation(OnApplicationStart.class);
        if (!appStartAnnotation.async()) {
          // run job sync
          try {
            Job<?> job = ((Job<?>) clazz.newInstance());
            scheduledJobs.add(job);
            job.run();
            if (job.wasError) {
              if (job.lastException != null) {
                throw job.lastException;
              }
              throw new RuntimeException("@OnApplicationStart Job has failed");
            }
          } catch (InstantiationException e) {
            throw new UnexpectedException("Job could not be instantiated", e);
          } catch (IllegalAccessException e) {
            throw new UnexpectedException("Job could not be instantiated", e);
          } catch (Throwable ex) {
            if (ex instanceof PlayException) {
              throw (PlayException) ex;
            }
            throw new UnexpectedException(ex);
          }
        } else {
          // run job async
          try {
            Job<?> job = ((Job<?>) clazz.newInstance());
            scheduledJobs.add(job);
            // start running job now in the background
            @SuppressWarnings("unchecked")
            Callable<Job> callable = (Callable<Job>) job;
            executor.submit(callable);
          } catch (InstantiationException ex) {
            throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
          } catch (IllegalAccessException ex) {
            throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
          }
        }
      }

      // @On
      if (clazz.isAnnotationPresent(On.class)) {
        try {
          Job<?> job = ((Job<?>) clazz.newInstance());
          scheduledJobs.add(job);
          scheduleForCRON(job);
        } catch (InstantiationException ex) {
          throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
        } catch (IllegalAccessException ex) {
          throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
        }
      }
      // @Every
      if (clazz.isAnnotationPresent(Every.class)) {
        try {
          Job job = (Job) clazz.newInstance();
          scheduledJobs.add(job);
          String value = job.getClass().getAnnotation(Every.class).value();
          if (value.startsWith("cron.")) {
            value = Play.configuration.getProperty(value);
          }
          value = Expression.evaluate(value, value).toString();
          if (!"never".equalsIgnoreCase(value)) {
            executor.scheduleWithFixedDelay(
                job, Time.parseDuration(value), Time.parseDuration(value), TimeUnit.SECONDS);
          }
        } catch (InstantiationException ex) {
          throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
        } catch (IllegalAccessException ex) {
          throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
        }
      }
    }
  }
コード例 #4
0
 private void createSipRouter() {
   DynamicKeepAliveTask keepAliveTask = new DynamicKeepAliveTask(this);
   scheduledThreadPoolExecutor.scheduleWithFixedDelay(
       keepAliveTask, keepAliveDelay, keepAliveDelay, TimeUnit.SECONDS);
 }
コード例 #5
0
ファイル: ThreadPool.java プロジェクト: mehiel/elasticsearch
 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, TimeValue interval) {
   return scheduler.scheduleWithFixedDelay(
       new LoggingRunnable(command), interval.millis(), interval.millis(), TimeUnit.MILLISECONDS);
 }
コード例 #6
0
 public void schedule(long period) {
   ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(THREADS_COUNT);
   executor.scheduleWithFixedDelay(this::sendEvents, period, period, TimeUnit.MILLISECONDS);
 }
コード例 #7
0
 public void startRunning() {
   findAndConnectToTerminal();
   executorService.scheduleWithFixedDelay(this, 1000, 100, TimeUnit.MILLISECONDS);
   executorService.scheduleWithFixedDelay(reconnector, 1, 1, TimeUnit.SECONDS);
 }
コード例 #8
0
ファイル: Scheduler.java プロジェクト: jruyi/jruyi
 @Override
 public ScheduledFuture<?> scheduleWithFixedDelay(
     Runnable command, long initialDelay, long delay, TimeUnit unit) {
   return m_executor.scheduleWithFixedDelay(command, initialDelay, delay, unit);
 }
コード例 #9
0
 @Override
 public void afterApplicationStart() {
   List<Class> jobs = new ArrayList();
   for (Class clazz : Play.classloader.getAllClasses()) {
     if (Job.class.isAssignableFrom(clazz)) {
       jobs.add(clazz);
     }
   }
   scheduledJobs = new ArrayList();
   for (final Class clazz : jobs) {
     // @OnApplicationStart
     if (clazz.isAnnotationPresent(OnApplicationStart.class)) {
       try {
         Job job = ((Job) clazz.newInstance());
         scheduledJobs.add(job);
         job.run();
         if (job.wasError) {
           if (job.lastException != null) {
             throw job.lastException;
           }
           throw new RuntimeException("@OnApplicationStart Job has failed");
         }
       } catch (InstantiationException e) {
         throw new UnexpectedException("Job could not be instantiated", e);
       } catch (IllegalAccessException e) {
         throw new UnexpectedException("Job could not be instantiated", e);
       } catch (Throwable ex) {
         if (ex instanceof PlayException) {
           throw (PlayException) ex;
         }
         throw new UnexpectedException(ex);
       }
     }
     // @On
     if (clazz.isAnnotationPresent(On.class)) {
       try {
         Job job = ((Job) clazz.newInstance());
         scheduledJobs.add(job);
         scheduleForCRON(job);
       } catch (InstantiationException ex) {
         throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
       } catch (IllegalAccessException ex) {
         throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
       }
     }
     // @Every
     if (clazz.isAnnotationPresent(Every.class)) {
       try {
         Job job = (Job) clazz.newInstance();
         scheduledJobs.add(job);
         String value = ((Every) (job.getClass().getAnnotation(Every.class))).value();
         executor.scheduleWithFixedDelay(
             job, Time.parseDuration(value), Time.parseDuration(value), TimeUnit.SECONDS);
       } catch (InstantiationException ex) {
         throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
       } catch (IllegalAccessException ex) {
         throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
       }
     }
   }
 }