当前位置: 移动技术网 > 科技>操作系统>windows > springmvc异步处理

springmvc异步处理

2019年08月14日  | 移动技术网科技  | 我要评论

尚于博女友,致命武器第二季,张平选

 

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

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

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

在项目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;
}
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网