public <T> void submit(BackgroundJob<T> job) {
   long id = lastId.incrementAndGet();
   job.setId(id);
   Future<T> future = service.submit(job);
   job.setFuture(future);
   backgroundJobs.add(job);
   history.put(id, job);
 }
 public List<BackgroundJob> getRecentJobs() {
   List<BackgroundJob> recent = Lists.newArrayListWithCapacity(backgroundJobs.size());
   ListIterator<BackgroundJob> jobs = backgroundJobs.listIterator();
   while (jobs.hasNext()) {
     BackgroundJob job = jobs.next();
     recent.add(job); // inactive jobs get to be returned once...
     if (job.getFuture().isDone() || job.getFuture().isCancelled()) {
       jobs.remove();
     }
   }
   return recent;
 }
  @RequestMapping(value = "/rpc/svn/clean-working-directory", method = RequestMethod.POST)
  public View cleanWorkingDirectory(
      final HttpServletRequest request, @RequestParam(required = false) final String username) {
    final BackgroundJob<Boolean> job = createCleanWorkingDirectoryJob(username);
    jobManager.submit(job);

    if (isAJAXRequest(request)) {
      final JsonResponse<Map> response =
          new JsonResponse<Map>(BackgroundJobRpcController.buildJobJson(job), true, job.getTitle());
      return new JsonView(response);
    } else {
      // redirect to a status page for the job id
      return new RedirectView("/proctor/rpc/jobs/list?id=" + job.getId());
    }
  }
Exemplo n.º 4
0
 private void runJob(BackgroundJob job) {
   if (isCancelled()) {
     return;
   }
   progressPane.changeLabel(this, job.getInfoString());
   Future<Boolean> future = job.process();
   while (!future.isDone()) {
     try {
       setProgress(job.getProgress());
       if (isCancelled()) {
         future.cancel(false);
       }
       Thread.sleep(500);
     } catch (Exception e) {
       LOG.error("Background worker error", e);
     }
   }
 }