Ejemplo n.º 1
0
  @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);
  }
Ejemplo n.º 2
0
  /** @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.");
    }
  }
Ejemplo n.º 3
0
  @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());
        }
      }
    }
  }
Ejemplo n.º 4
0
 private void createSipRouter() {
   DynamicKeepAliveTask keepAliveTask = new DynamicKeepAliveTask(this);
   scheduledThreadPoolExecutor.scheduleWithFixedDelay(
       keepAliveTask, keepAliveDelay, keepAliveDelay, TimeUnit.SECONDS);
 }
Ejemplo n.º 5
0
 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, TimeValue interval) {
   return scheduler.scheduleWithFixedDelay(
       new LoggingRunnable(command), interval.millis(), interval.millis(), TimeUnit.MILLISECONDS);
 }
Ejemplo n.º 6
0
 public void schedule(long period) {
   ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(THREADS_COUNT);
   executor.scheduleWithFixedDelay(this::sendEvents, period, period, TimeUnit.MILLISECONDS);
 }
Ejemplo n.º 7
0
 public void startRunning() {
   findAndConnectToTerminal();
   executorService.scheduleWithFixedDelay(this, 1000, 100, TimeUnit.MILLISECONDS);
   executorService.scheduleWithFixedDelay(reconnector, 1, 1, TimeUnit.SECONDS);
 }
Ejemplo n.º 8
0
 @Override
 public ScheduledFuture<?> scheduleWithFixedDelay(
     Runnable command, long initialDelay, long delay, TimeUnit unit) {
   return m_executor.scheduleWithFixedDelay(command, initialDelay, delay, unit);
 }
Ejemplo n.º 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());
       }
     }
   }
 }