当前位置: 首页 > 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.hkea.cn/news/523900/

相关文章:

  • 政府网站有哪些网站seo最新优化方法
  • 做广告牌子seo外链工具
  • 微信页面设计网站兰州网络推广技术
  • 上门做网站搜狗站长工具
  • wordpress用户邮箱验证码百度seo搜索引擎优化培训
  • 360极速怎么屏蔽网站新闻热点大事件
  • 购物app开发价格表站长工具seo排名
  • 微餐饮网站建设营销型网站建设方案
  • 高端网站建设公司好不好2020国内搜索引擎排行榜
  • 网站建设服务公司选哪家比较好?苏州优化收费
  • 中国建设银行河南省分行网站推广信息哪个平台好
  • 网站建设官网免费模板杭州seo优化
  • 绍兴网站建设谷歌搜索引擎在线
  • 网站的会员认证怎么做黑龙江新闻头条最新消息
  • 做网站如何分工百度推广登录平台客服
  • 网站建设如何提案万网域名注册信息查询
  • 创意二维码制作网站企业网络营销推广案例
  • 论坛型网站怎么做百度高级检索入口
  • 做百度移动网站排搜素引擎优化
  • 公司创建一个网站需要多少钱想做百度推广找谁
  • 做文献ppt模板下载网站有哪些常德政府网站
  • 青岛网站建设公司排行外链工具在线
  • 网站怎么做显得简洁美观seo数据是什么意思
  • 阿里巴巴开通诚信通后网站怎么做网络优化网站
  • 东莞手机网站价格便宜个人免费建站软件
  • 电子商务网站建设的步骤一般为百度100%秒收录
  • 做企业网站怎么样免费的推广软件下载
  • 拓普网站建设美国搜索引擎
  • 网站开发者工资冯耀宗seo视频教程
  • 软件开发各阶段工作量比例搜索引擎优化的基础是什么