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=" ...
随机推荐
- efcore 关联插入
出现这个错误: at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) Mi ...
- SQL-连接查询:left join,right join,inner join,full join之间的区别
参考: https://www.cnblogs.com/lijingran/p/9001302.html https://www.cnblogs.com/assasion/p/7768931.html ...
- zabbix--远程执行命令
zabbix 远程执行命令 重启应用 服务器 使用远程执行命令可以在某些时候帮我做一些事情,达到轻量级的自动化,比如当 nginx.mysql.php.redis.tomcat.等等应用挂掉时帮我们自 ...
- php构建型模式(Builder pattern)
练代码,增加了调用时的输出. <?php /* The builder pattern separates the construction of a complex object from i ...
- KVM-安装windows
硬盘.网卡选择vitio 虚拟机配置2个cd-rom,分别挂载系统iso与virtio-win 开始安装无法识别硬盘,加载光驱驱动 安装完成进入系统之后,设备管理器添加驱动,识别设备
- Redis 入门知识点
1.Redis简介 1.1.简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redi ...
- Go语言 - 结构体 | 方法
自定义类型和类型别名 自定义类型 在Go语言中有一些基本的数据类型,如string.整型.浮点型.布尔等数据类型, Go语言中可以使用type关键字来定义自定义类型. 自定义类型是定义了一个全新的类型 ...
- PHP函数file_get_contents()使用 https 协议时报错:SSL operation failed
场景: file_get_contents() 函数是用于将文件的内容读入到一个字符串中,是读取文件内容常用的函数之一. 但是有时在服务器上使用file_get_contents() 函数请求http ...
- 不要轻易在java ext 目录放任何三方jar包
今天在编写一个简单spi 应用demo的时候,在编译时总有一个其他的错误,如下: ERROR Failed to execute goal org.apache.maven.plugins:maven ...
- hive基础知识三
1. 基本查询 注意 SQL 语言大小写不敏感 SQL 可以写在一行或者多行 关键字不能被缩写,也不能分行 各子句一般要分行写 使用缩进提高语句的可读性 1.1 全表和特定列查询 全表查询 selec ...