springmvc异步处理

 

好久没有写过博客了,都是看大牛的文章,略过~~

突然感觉成长在于总结!废话不多说,开干

由于是公司项目,所以不方便给出代码,看图操作

在项目util目录下创建工具类TaskExecutorConfig 并且实现 org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

该工具类用@EnableAsync修饰,表示可以用于异步;并且要实现

getAsyncExecutor()方法和
getAsyncUncaughtExceptionHandler()方法;在
getAsyncExecutor()内创建线程池并返回调用;

在需要异步调用的地方创建异步类TaskExecutorConfig并调用它的getAsyncExecutor()方法,得到线程池java.util.concurrent.

Executor对象;再通过Executor.execute(new MyRunnable(mapParam))实现异步;MyRunnable是我创建的内部类

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;
}
}

 

转载于:https://www.cnblogs.com/sjyBlog/p/11348758.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值