欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

springmvc异步处理

程序员文章站 2022-08-28 22:35:43
好久没有写过博客了,都是看大牛的文章,略过~~ 突然感觉成长在于总结!废话不多说,开干 由于是公司项目,所以不方便给出代码,看图操作 在项目util目录下创建工具类TaskExecutorConfig 并且实现 org.springframework.aop.interceptor.AsyncUnc ......

 

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

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

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

springmvc异步处理

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

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

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

springmvc异步处理

在需要异步调用的地方创建异步类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;
}
}