Springmvc 异步处理
package com.lookcoder.haircutmember.controller.login.page.async; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.concurrent.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncTask; import java.util.concurrent.*; @RequestMapping("/async/")
@Controller
public class AsyncController { private static Logger log = LoggerFactory.getLogger(AsyncController.class);
private static ExecutorService executor = Executors.newSingleThreadExecutor(); @ResponseBody
@RequestMapping("listenableFuture")
public ListenableFuture<String> testdelistenableFuture() {
log.info("in main thread.");
// Runnable r = () -> {
// for (int i = 0; i < 10; i++) {
// log.info("sub thread run.... " + (i + 1));
// }
// try {
// Thread.sleep(5 * 1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// };
Callable<String> r = () -> {
int j = 0;
for (int i = 0; i < 10; i++) {
j += i;
log.info("sub thread run.... " + (i + 1));
}
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return String.valueOf(j);
};
ListenableFutureTask lf = new ListenableFutureTask(r); lf.addCallback(new ListenableFutureCallback() {
@Override
public void onFailure(Throwable ex) {
log.info("调用失败!");
} @Override
public void onSuccess(Object result) {
log.info("调用成功!" + result);
}
}); executor.submit(lf); return lf;
} @ResponseBody
@RequestMapping("deferred")
public DeferredResult<String> testdeferred() {
DeferredResult<String> dr = new DeferredResult<>();
executor.submit(() -> {
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int j = 0;
for (int i = 0; i < 10; i++) {
log.info("children thread is run ...");
j+= i;
}
log.info("get value");
boolean b = dr.setResult(String.valueOf(j));
log.info("set value is ok!" + b);
});
return dr;
} @ResponseBody
@RequestMapping("runnable")
public String testRunnable() {
log.info("in main thread!");
log.info("create sub thread!");
Runnable c = () -> {
try {
Thread.sleep(15 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int i;
for (i = 0; i < 10; i++) {
log.info("sub thread task run...." + (i + 1));
}
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(c);
log.info("again in main thread!");
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
log.info("main thread task run...." + (i + 1));
}
log.info("main thread is complete!");
return "main thread return!";
} @ResponseBody
@RequestMapping("callable")
public Callable<Integer> testCallable() {
log.info("in main thread!");
log.info("create sub thread!");
Callable<Integer> c = () -> {
try {
Thread.sleep(15 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int i;
for (i = 0; i < 10; i++) {
log.info("sub thread task run...." + (i + 1));
}
return i;
};
log.info("again in main thread!");
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
log.info("main thread task run...." + (i + 1));
}
log.info("main thread is complete!");
return c;
} @ResponseBody
@RequestMapping("webAsyncTask")
public WebAsyncTask<String> testWebAsyncTask() {
log.info("in main thread!");
log.info("create sub thread!");
WebAsyncTask wat = new WebAsyncTask(() -> {
try {
Thread.sleep(15 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
log.info("sub thread task run...." + (i + 1));
}
return "ok";
});
log.info("again in main thread!");
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
log.info("main thread task run...." + (i + 1));
}
log.info("main thread is complete!");
return wat;
}
}
Springmvc 异步处理的更多相关文章
- SpringMVC异步调用,Callable和DeferredResult的使用
Callable和DeferredResult都是springMVC里面的异步调用,Callable主要用来处理一些简单的逻辑,DeferredResult主要用于处理一些复杂逻辑 1.Callabl ...
- SpringBoot+springmvc异步处理请求
有两种情况,第一种是业务逻辑复杂,但不需要业务逻辑的结果,第二种是需要返回业务逻辑的处理结果 第一种比较简单,利用多线程处理业务逻辑,或者利用spring中@Asyn注解更简单, 使用@Asyn注解, ...
- 天天写同步,5种SpringMvc异步请求了解下!
引言 说到异步大家肯定首先会先想到同步.我们先来看看什么是同步? 所谓同步,就是发出一个功能调用时,在没有得到结果之前,该调用就不返回或继续执行后续操作. 简单来说,同步就是必须一件一件事做,等前一件 ...
- Springmvc异步上传文件
<script src="js/jquery.js" type="text/javascript"></script><scrip ...
- SpringMVC异步文件上传下载
首先了解一下File的构造方法: File(String pathname):根据一个路径得到File对象 File(String parent,String child):根据一个目录和一个子文件/ ...
- SpringMVC 异步与定时使用示例
1.Spring 的xml配置: <aop:aspectj-autoproxy/> <task:annotation-driven executor="annotation ...
- springmvc异步处理
好久没有写过博客了,都是看大牛的文章,略过~~ 突然感觉成长在于总结!废话不多说,开干 由于是公司项目,所以不方便给出代码,看图操作 在项目util目录下创建工具类TaskExecutorConfig ...
- SpringMVC异步处理的 5 种方式
作者:丁仪 来源:https://chengxuzhixin.com/blog/post/SpringMVC-yi-bu-chu-li-de-5-zhong-fang-shi.html 前段时间研究了 ...
- springmvc异步上传图片并回调页面函数插入图片url代码示例
<tr> <td class="search_td">属性值图片值:</td> <td> <input type=" ...
随机推荐
- 浅谈Python设计模式 - 原型模式
声明,本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 在<精通Python设计模式>中把设计模式分为三种类型: 创建型模式 ...
- spring data jpa 表关联设置用户表关联角色表配置
User 表: @ManyToMany(cascade = { CascadeType.MERGE }) @JsonIgnore @JoinTable(name = "UserRole&qu ...
- PostgreSQL分区表实现——pg_pathman分区表管理
该博文用于自己学习记录,内容节选自: https://github.com/digoal/blog/blob/master/201610/20161024_01.md pg_pathman 创建分区表 ...
- linux设备驱动程序--hello-world
linux字符设备驱动程序--hello_world 基于4.14内核, beagleBone green平台 PC端的设备驱动程序 有过电脑使用经验的人都知道,当我们将外部硬件设备比如鼠标键盘插入到 ...
- mysql数据库SQL执行分析,优化前必备分析
概述 一般我们在对mysql数据库做优化,肯定需要对慢sql去做分析才能开始优化,那么有什么分析的方法呢?下面通过对sql执行时间和执行情况来做分析. 一.SQL 执行时间分析 通过找到执行时间长的 ...
- 【PHP】系统部署
1.MySql数据库,单独创建用户和数据库 使用MySql-Front导入,避免使用Navicat导入 2.httpd-vhosts.conf配置 文件位于:D:\Xampp\apache\conf\ ...
- Hbase扩展
1 HBase在商业项目中的能力 每天: 1) 消息量:发送和接收的消息数超过60亿 2) 将近1000亿条数据的读写 3) 高峰期每秒150万左右操作 4) 整体读取数据占有约55%,写入占有45% ...
- 腾讯云 Tencent Hub工作流通过钉钉通知
增加一个Job即可. 使用工作流组件为:hub.tencentyun.com/tencenthub/notice_dingding 其他的看填写说明就可以了. PS也可以通过TFTT来实现,也很好用.
- (尚034)Vue_案例_数据存储优化(代码优化!!!)
最好能将上述代码抽取成一个模块(读json数据+写json数据) 1.在src下新建文件夹util(util文件夹用于放入工具的模块) 2.*使用localStorage存储数据的工具模块* 一个模块 ...
- Java中lambda表达式学习
一.Lambda表达式的基础语法: Java8中引入了一个新的操作符"->"该操作符称为箭头操作符或Lambda操作符,箭头操作符将Lambda表达式拆分为两部分: 左侧:L ...