/** * Start concurrent request processing and execute the given task with an * {@link #setTaskExecutor(AsyncTaskExecutor) AsyncTaskExecutor}. The result * from the task execution is saved and the request dispatched in order to * resume processing of that result. If the task raises an Exception then * the saved result will be the raised Exception. * @param callable a unit of work to be executed asynchronously * @param processingContext additional context to save that can be accessed * via {@link #getConcurrentResultContext()} * @throws Exception if concurrent processing failed to start * @see #getConcurrentResult() * @see #getConcurrentResultContext() */ @SuppressWarnings({"unchecked", "rawtypes"}) public void startCallableProcessing(Callable<?> callable, Object... processingContext) throws Exception { Assert.notNull(callable, "Callable must not be null"); startCallableProcessing(new WebAsyncTask(callable), processingContext); }
/** * Start concurrent request processing and execute the given task with an * {@link #setTaskExecutor(AsyncTaskExecutor) AsyncTaskExecutor}. The result * from the task execution is saved and the request dispatched in order to * resume processing of that result. If the task raises an Exception then * the saved result will be the raised Exception. * @param callable a unit of work to be executed asynchronously * @param processingContext additional context to save that can be accessed * via {@link #getConcurrentResultContext()} * @throws Exception if concurrent processing failed to start * @see #getConcurrentResult() * @see #getConcurrentResultContext() */ @SuppressWarnings({"unchecked", "rawtypes"}) public void startCallableProcessing(Callable<?> callable, Object... processingContext) throws Exception { Assert.notNull(callable, "Callable must not be null"); startCallableProcessing(new WebAsyncTask(callable), processingContext); }
@Test public void startCallableProcessingErrorAndResumeThroughCallback() throws Exception { StubCallable callable = new StubCallable(); WebAsyncTask<Object> webAsyncTask = new WebAsyncTask<>(callable); webAsyncTask.onError(new Callable<Object>() { @Override public Object call() throws Exception { return 7; } }); this.asyncManager.startCallableProcessing(webAsyncTask); Exception e = new Exception(); AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e); this.asyncWebRequest.onError(event); assertTrue(this.asyncManager.hasConcurrentResult()); assertEquals(7, this.asyncManager.getConcurrentResult()); assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()); }
@Test public void startCallableProcessingTimeoutAndResumeThroughCallback() throws Exception { StubCallable callable = new StubCallable(); WebAsyncTask<Object> webAsyncTask = new WebAsyncTask<>(callable); webAsyncTask.onTimeout(new Callable<Object>() { @Override public Object call() throws Exception { return 7; } }); this.asyncManager.startCallableProcessing(webAsyncTask); this.asyncWebRequest.onTimeout(ASYNC_EVENT); assertTrue(this.asyncManager.hasConcurrentResult()); assertEquals(7, this.asyncManager.getConcurrentResult()); assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()); }
@SuppressWarnings("unchecked") @Test public void startCallableProcessingWithAsyncTask() throws Exception { AsyncTaskExecutor executor = mock(AsyncTaskExecutor.class); given(this.asyncWebRequest.getNativeRequest(HttpServletRequest.class)).willReturn(this.servletRequest); WebAsyncTask<Object> asyncTask = new WebAsyncTask<>(1000L, executor, mock(Callable.class)); this.asyncManager.startCallableProcessing(asyncTask); verify(executor).submit((Runnable) notNull()); verify(this.asyncWebRequest).setTimeout(1000L); verify(this.asyncWebRequest).addTimeoutHandler(any(Runnable.class)); verify(this.asyncWebRequest).addErrorHandler(any(Consumer.class)); verify(this.asyncWebRequest).addCompletionHandler(any(Runnable.class)); verify(this.asyncWebRequest).startAsync(); }
/** * 取消执行脚本 * * @param id * @return */ @RequestMapping(value = "/cancelJob", method = RequestMethod.GET) @ResponseBody public WebAsyncTask<String> cancelJob(String id) { return new WebAsyncTask<>(3000, () -> workClient.cancelJobFromWeb(JobExecuteKind.ExecuteKind.DebugKind, id)); }
/** * Start concurrent request processing and execute the given task with an * {@link #setTaskExecutor(AsyncTaskExecutor) AsyncTaskExecutor}. The result * from the task execution is saved and the request dispatched in order to * resume processing of that result. If the task raises an Exception then * the saved result will be the raised Exception. * @param callable a unit of work to be executed asynchronously * @param processingContext additional context to save that can be accessed * via {@link #getConcurrentResultContext()} * @throws Exception if concurrent processing failed to start * @see #getConcurrentResult() * @see #getConcurrentResultContext() */ @SuppressWarnings({"unchecked", "rawtypes"}) public void startCallableProcessing(Callable<?> callable, Object... processingContext) throws Exception { Assert.notNull(callable, "Callable must not be null"); startCallableProcessing(new WebAsyncTask(callable), processingContext); }
return new WebAsyncTask<>(this.timeout, asyncTask);
/** * Start concurrent request processing and execute the given task with an * {@link #setTaskExecutor(AsyncTaskExecutor) AsyncTaskExecutor}. The result * from the task execution is saved and the request dispatched in order to * resume processing of that result. If the task raises an Exception then * the saved result will be the raised Exception. * @param callable a unit of work to be executed asynchronously * @param processingContext additional context to save that can be accessed * via {@link #getConcurrentResultContext()} * @throws Exception if concurrent processing failed to start * @see #getConcurrentResult() * @see #getConcurrentResultContext() */ @SuppressWarnings({"unchecked", "rawtypes"}) public void startCallableProcessing(Callable<?> callable, Object... processingContext) throws Exception { Assert.notNull(callable, "Callable must not be null"); startCallableProcessing(new WebAsyncTask(callable), processingContext); }
@RequestMapping(value = "/generateVersion", method = RequestMethod.POST) @ResponseBody public WebAsyncTask<String> generateVersion(String jobId) { if (!hasPermission(Integer.parseInt(jobId), JOB)) { return new WebAsyncTask<>(() -> ERROR_MSG); } WebAsyncTask<String> asyncTask = new WebAsyncTask<>(HeraGlobalEnvironment.getRequestTimeout(), () -> workClient.generateActionFromWeb(JobExecuteKind.ExecuteKind.ManualKind, jobId)); asyncTask.onTimeout(() -> "版本生成时间较长,请耐心等待下"); return asyncTask; }
/** * 今日所有任务状态明细,线形图初始化 * * @return */ @RequestMapping(value = "/homePage/getJobQueueInfo", method = RequestMethod.GET) @ResponseBody public WebAsyncTask getJobQueueInfo() { return new WebAsyncTask<>(HeraGlobalEnvironment.getRequestTimeout(), () -> { try { return workClient.getJobQueueInfoFromWeb(); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } return null; }); }
@RequestMapping(value = "/generateAllVersion", method = RequestMethod.GET) @ResponseBody public WebAsyncTask<String> generateAllVersion() { if (!isAdmin(getOwner())) { return new WebAsyncTask<>(() -> ERROR_MSG); } WebAsyncTask<String> asyncTask = new WebAsyncTask<>(HeraGlobalEnvironment.getRequestTimeout(), () -> workClient.generateActionFromWeb(JobExecuteKind.ExecuteKind.ManualKind, Constants.ALL_JOB_ID)); asyncTask.onTimeout(() -> "全量版本生成时间较长,请耐心等待下"); return asyncTask; }
@RequestMapping("/webAsyncTask") public WebAsyncTask<String> webAsyncTask() { log.info("外部线程:" + Thread.currentThread().getName()); WebAsyncTask<String> result = new WebAsyncTask<String>(60*1000L, new Callable<String>() { @Override public String call() throws Exception { log.info("内部线程:" + Thread.currentThread().getName()); return "WebAsyncTask!!!"; } }); result.onTimeout(new Callable<String>() { @Override public String call() throws Exception { // TODO Auto-generated method stub return "WebAsyncTask超时!!!"; } }); result.onCompletion(new Runnable() { @Override public void run() { //超时后 也会执行此方法 log.info("WebAsyncTask执行结束"); } }); return result; } }
@RequestMapping(value = "/execute", method = RequestMethod.GET) @ResponseBody @UnCheckLogin public WebAsyncTask<JsonResponse> zeusExecute(Integer id, String owner) { List<HeraAction> actions = heraJobActionService.findByJobId(String.valueOf(id)); if (actions == null) { return new WebAsyncTask<>(() -> new JsonResponse(false, "action为空")); } return execute(actions.get(actions.size() - 1).getId().toString(), 2, owner); }
@RequestMapping(value = "/homePage/getAllWorkInfo", method = RequestMethod.GET) @ResponseBody public WebAsyncTask getAllWorkInfo() { WebAsyncTask webAsyncTask = new WebAsyncTask<>(HeraGlobalEnvironment.getRequestTimeout(), () -> workClient.getAllWorkInfo()); webAsyncTask.onTimeout(() -> { ErrorLog.error("获取work信息超时"); return null; }); return webAsyncTask; }
}; return new WebAsyncTask<>(this.asyncTimeout, asyncTask);
/** * 取消正在执行的任务 * * @param jobId * @param historyId * @return */ @RequestMapping(value = "/cancelJob", method = RequestMethod.GET) @ResponseBody public WebAsyncTask<String> cancelJob(String historyId, String jobId) { if (!hasPermission(Integer.parseInt(jobId), JOB)) { return new WebAsyncTask<>(() -> ERROR_MSG); } HeraJobHistory history = heraJobHistoryService.findById(historyId); JobExecuteKind.ExecuteKind kind; if (TriggerTypeEnum.parser(history.getTriggerType()) == TriggerTypeEnum.MANUAL) { kind = JobExecuteKind.ExecuteKind.ManualKind; } else { kind = JobExecuteKind.ExecuteKind.ScheduleKind; } WebAsyncTask<String> webAsyncTask = new WebAsyncTask<>(HeraGlobalEnvironment.getRequestTimeout(), () -> workClient.cancelJobFromWeb(kind, historyId)); webAsyncTask.onTimeout(() -> "任务取消执行中,请耐心等待"); return webAsyncTask; }
return new WebAsyncTask<JsonResponse>(HeraGlobalEnvironment.getRequestTimeout(), () -> { Map<String, Object> res = new HashMap<>(2); HeraFile file = heraFileService.findById(heraFile.getId());
return new WebAsyncTask<>(10000, () -> { Map<String, Object> res = new HashMap<>(2); HeraFile file = heraFileService.findById(heraFile.getId());
return new WebAsyncTask<>(() -> new JsonResponse(false, ERROR_MSG)); heraJobActionService.update(heraAction); WebAsyncTask<JsonResponse> webAsyncTask = new WebAsyncTask<>(HeraGlobalEnvironment.getRequestTimeout(), () -> { try { workClient.executeJobFromWeb(JobExecuteKind.ExecuteKind.ManualKind, actionHistory.getId());