经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Windows » 查看文章
springmvc异步处理
来源:cnblogs  作者:sjysu  时间:2019/8/15 11:54:47  对本文有异议

 

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

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

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

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

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

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

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

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

  1. 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);
    }
    }
    需要注意的是Executorjava.util.concurrent中;

    如果这样不行可以再试试用@ComponentScan("com.videoadmin.async")修饰异步类TaskExecutorConfig;再通过以下方式实现异步:
  1. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
    TaskService taskService = context.getBean(TaskService.class);
    taskService.doAsync(destFile,objectId);
    // 这里调用异步方法...
    context.close();
  1. @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;
    }
    }

 

原文链接:http://www.cnblogs.com/sjyBlog/p/11348758.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号