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=" ...
随机推荐
- 认识Redis
认识的Redis 官方原文: Redis is an open source (BSD licensed), in-memory data structure store, used as a dat ...
- Filter和Listener
Filter: 1.概念: web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,做一些事. 过滤器的作用:一般用于完成一些通用的操作:登录验证.统一编码处理,敏感字符处理.... 2.快 ...
- H3C 802.11 WEP加密特点与注意事项
- K8S概念理解参考
- centos7 增加开放端口
添加 firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效) 重新载入 不然不生效 ...
- datetime,Timestamp和datetime64之间转换
引入工具包 import datetime import numpy as np import pandas as pd 总览 from IPython.display import Image fr ...
- python + redis +ipset实现IP黑名单的动态添加及解封禁
1.抽空用python做了一个 动态添加/删除IP黑名单 的程序(或者说实现方案),项目地址: https://gitee.com/lowmanisbusy/ip_blacklists, 2.这里的实 ...
- Hbase扩展
1 HBase在商业项目中的能力 每天: 1) 消息量:发送和接收的消息数超过60亿 2) 将近1000亿条数据的读写 3) 高峰期每秒150万左右操作 4) 整体读取数据占有约55%,写入占有45% ...
- mysql使用过程中出现的问题总结
1.mysql命令窗口输入密码后窗口闪退 密码输入错误.(其他暂不清楚) 2. 出现这个错误的原因是,数据库的编码格式不一致. https://www.cnblogs.com/lsr-flying/p ...
- python--模块学习之xml模块
xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 本文主要学习的ElementTree是python的XML处理模块,它提供了一个轻量级的对象 ...