当前位置: 首页 > news >正文

营销型网站 平台中国建设银行信用卡电话

营销型网站 平台,中国建设银行信用卡电话,盐城网站关键词优化,大学生网页设计作业代码首先要明确一点,同步请求和异步请求对于客户端用户来讲是一样的,都是需客户端等待返回结果。不同之处在于请求到达服务器之后的处理方式,下面用两张图解释一下同步请求和异步请求在服务端处理方式的不同:同步请求异步请求两个流程…

首先要明确一点,同步请求和异步请求对于客户端用户来讲是一样的,都是需客户端等待返回结果。不同之处在于请求到达服务器之后的处理方式,下面用两张图解释一下同步请求和异步请求在服务端处理方式的不同:

同步请求

异步请求

两个流程中客户端对Web容器的请求,都是同步的。因为它们在请求客户端时都处于阻塞等待状态,并没有进行异步处理。在Web容器部分,第一个流程采用同步请求,第二个流程采用异步回调的形式。通过异步处理,可以先释放容器分配给请求的线程与相关资源,减轻系统负担,从而增加了服务器对客户端请求的吞吐量。但并发请求量较大时,通常会通过负载均衡的方案来解决,而不是异步。

  1. 使用AsyncContext执行异步请求

package com.example.async;import java.io.IOException;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class AsyncContextController {@GetMapping("/asyncContext")@ResponseBodypublic String asyncTask(HttpServletRequest request) {AsyncContext asyncContext = request.startAsync();asyncContext.addListener(new AsyncListener() {@Overridepublic void onTimeout(AsyncEvent event) throws IOException {System.out.println("处理超时了...");}@Overridepublic void onStartAsync(AsyncEvent event) throws IOException {System.out.println("线程开始执行");}@Overridepublic void onError(AsyncEvent event) throws IOException {System.out.println("执行过程中发生错误:" + event.getThrowable().getMessage());}@Overridepublic void onComplete(AsyncEvent event) throws IOException {System.out.println("执行完成,释放资源");}});asyncContext.setTimeout(6000);asyncContext.start(new Runnable() {@Overridepublic void run() {try {Thread.sleep(5000);System.out.println("内部线程:" + Thread.currentThread().getName());asyncContext.getResponse().getWriter().println("async processing");} catch (Exception e) {System.out.println("异步处理发生异常:" + e.getMessage());}asyncContext.complete(); // 异步请求完成通知,整个请求完成}});System.out.println("主线程:" + Thread.currentThread().getName()); return "OK";}
}
  1. 使用Callable执行异步请求

package com.example.async;import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class CallableController {@GetMapping(path = "/callable")@ResponseBodypublic Callable<String> asyncRequest() {return () -> {TimeUnit.SECONDS.sleep(10);return "OK";};}
}
  1. 使用WebAsyncTask执行异步请求

package com.example.async;import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.WebAsyncTask;@RestController
public class WebAsyncTaskController {@GetMapping("/webAsyncTask")@ResponseBodypublic WebAsyncTask<String> asyncTask() {WebAsyncTask<String> webAsyncTask = new WebAsyncTask<String>(1000l * 10, new Callable<String>() {@Overridepublic String call() throws Exception {TimeUnit.SECONDS.sleep(5);return "OK";}});webAsyncTask.onCompletion(new Runnable() {@Overridepublic void run() {System.out.println("调用完成");}});webAsyncTask.onTimeout(new Callable<String>() {@Overridepublic String call() throws Exception {return "Time Out";}});return webAsyncTask;}
}
  1. 使用DeferredResult执行异步请求

package com.example.async;import java.util.concurrent.TimeUnit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;@RestController
public class DeferredResultController {@GetMapping(path = "/deferredResult")@ResponseBodypublic DeferredResult<String> asyncRequest() {DeferredResult<String> deferredResult = new DeferredResult<>(1000L * 5, "失败");deferredResult.onTimeout(() -> {System.out.println("调用超时");deferredResult.setResult("调用超时");});deferredResult.onCompletion(() -> {System.out.println("调用完成");});new Thread(() -> {try {TimeUnit.SECONDS.sleep(10);deferredResult.setResult("OK");} catch (Exception e) {e.printStackTrace();}}).start();return deferredResult;}
}
  1. 另外:Spring Boot中使用注解@Async处理异步任务

@Async注解的异步操作和上文所诉的四种异步请求不同之处在于,使用@Async处理异步任务时没有异步回调响应客户端的流程:

使用@EnableAsync开启@Async

package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;@EnableAsync
@SpringBootApplication
public class ExampleApplication {public static void main(String[] args) {SpringApplication.run(ExampleApplication.class, args);}}

如果将@Async加在Controller上或是 Controller 的方法上

package com.example.async;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class AsyncController {@Autowiredprivate TestService testService;@GetMapping("/async")@ResponseBody@Asyncpublic String asyncTask() {testService.doSomeThing();System.out.println("处理完成");return "OK";}
}

控制器立即会给客户端空响应,但是控制器方法依旧执行

如果将@Async加在Service上或是 Service 的方法上

package com.example.async;import java.util.concurrent.TimeUnit;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class TestService {@Asyncpublic void doSomeThing() {try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}}
}

控制器不再等待Service方法执行完毕就响应客户端

http://www.yayakq.cn/news/939144/

相关文章:

  • 济南网站建设方案书炫丽的网站
  • 一站式采购平台官网搜索引擎查询
  • 做网站的上海公司徐州建站公司
  • 哪些网站的简历做的比较好wordpress 关闭工具栏
  • asp网站报错信息网站推广公司推荐
  • 建网站商城平台株洲seo网络优化招聘网
  • 泉州网站建设网络推广大连seo外包公司
  • 网站建设产业pest分析自媒体怎么入门
  • 乡村建设的网站学做点心的网站
  • 锡林浩特网站建设宁波依众网络科技有限公司
  • 重庆夹夹虫网络公司网站建设建设房产网站
  • 邢台中高风险地区查询搜索引擎优化到底是优化什么
  • 赣州网站维护wordpress 数据库查询插件
  • 中文企业展示网站模板网站以前在百度能搜索不到了
  • 开发网站类型百度一下你就知道手机版官网
  • 如何做竞价网站做暧暧视频网站w
  • WordPress首页不收录昆明百度搜索排名优化
  • 郑州网站推广 汉狮网络微信小程序 连接网站
  • 公司做网站卖东西要什么证深圳保障房申请条件2022
  • 网站开发还是做数据库开发自定义网站模板
  • 商城网站后台模板社交网站开发难度
  • 培训学校如何做网站宣传品牌建设工作的意义
  • 建设网站的账务处理电商网站开发研究内容和预期成果
  • 网站开发技术thinkphp网站建设的书籍
  • 酒店网站建设策划方案wordpress 使用七牛
  • 大航母网站建设怎么样做农资的网站
  • wordpress工作室重庆seo排名收费
  • 国外做问卷网站天津建设工程信息网 塘沽一中
  • html网站建设流程图网页编辑哪个键
  • jsp网站开发视频德州极速网站建设小程序