springmvc asynchronous processing

 

Long time did not write the blog, is to see Daniel's article, skip ~~

Suddenly feeling grew that the summary! Ado, open dry

Because the project is the company, so not convenient given the code, plug-operation

Creating tools TaskExecutorConfig util directory in the project and realization org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

The tools used @EnableAsync modified, can be used to represent asynchronous; and to achieve

getAsyncExecutor () method, and
getAsyncUncaughtExceptionHandler () method; in
Create a thread pool getAsyncExecutor () and return the call within;

Create an asynchronous class TaskExecutorConfig where needed asynchronous calls and call it getAsyncExecutor () method to obtain a thread pool java.util.concurrent.

Executor objects; and then implement asynchronous by Executor.execute (new MyRunnable (mapParam)); MyRunnable is an internal class that I created

public static class MyRunnable implements Runnable {
private Map<String,Object> mapParam;
public MyRunnable(Map<String,Object> mapParam){
this.mapParam = mapParam;
}
/**
* 重写run方法
*/
@Override
public void run() {
concertVideo(mapParam);
}
}
需要注意的是Executor在java.util.concurrent中;

如果这样不行可以再试试用@ComponentScan("com.videoadmin.async")修饰异步类TaskExecutorConfig;再通过以下方式实现异步:
               AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
TaskService taskService = context.getBean(TaskService.class);
taskService.doAsync(destFile,objectId);
// 这里调用异步方法...
context.close();
@Service
public class TaskService {
private static String piantouTs = "F:\\shiping_test\\tsFile\\test\\2.ts";
@Async
public String doAsync(String filePath,String objectId) throws Exception {
if (filePath != null) {
filePath = ConvertVideo.transTOts(filePath);
if (filePath != null) {
filePath = ConvertVideo.concatVideo(piantouTs, filePath);
if (filePath != null) {
filePath = ConvertVideo.processMp4(filePath);
if (filePath != null) {
return filePath;
}
}
}
}
return null;
}
}

 

Guess you like

Origin www.cnblogs.com/sjyBlog/p/11348758.html