In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you the problem of how to analyze the integration of springboot responsive programming and webFlux. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Before the servlet3.0 standard, there was one thread for each request. If a thread has a high latency at this time, it will cause blocking problems, resulting in a serious performance situation for the entire service, because such an operation is possible once a third-party interface is called. In the early days, the only way to handle it was to control the thread manually.
After the servlet3.0 standard, in order to solve such problems, support for asynchronous response is provided. In the asynchronous response processing structure, the part of the time-consuming operation can be handed over to a dedicated asynchronous thread for response processing, while the requested thread resources will be released and the thread will be returned to the thread pool for use by other users. this operation mechanism will greatly improve the concurrency performance of the program.
The responsive programming support given above is only a native support model, but now that it is based on springboot program development, some simpler integration needs to be considered.
In order to implement responsive programming in spring, we need to use spring webFlux, which is a reconstructed asynchronous non-blocking Web development framework based on Reactive Streams standard. Based on the Reactor development framework, it is easier to implement the request processing model under high concurrent access. The webFlux dependency module is provided in the springboot2.x version, which has two model implementations: one is based on functional endpoints, and the other is based on SpringMVC annotations.
Maven introduces org.springframework.boot spring-boot-starter-webflux integration processor: package com.example.oldguy.myWebFlux.handler; import com.example.oldguy.myVo.Message;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Component;import reactor.core.publisher.Mono;@Component@Slf4jpublic class MessageHandler {public Mono echoHandler (Message message) {log.info ("[{}]] Business layer receives and processes data: {}", Thread.currentThread () .getName ()) Message.setTitle ("[]" + Thread.currentThread (). GetName () + "+ message.getTitle ()); message.setContent (" [] "+ Thread.currentThread (). GetName () +"] "+ message.getContent ()); return Mono.create (item- > item.success (message)); / / data response}} Integration Controller: package com.example.oldguy.myController; import com.example.oldguy.myVo.Message Import com.example.oldguy.myWebFlux.handler.MessageHandler;import com.example.oldguy.mytask.MyThreadTask;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.InitBinder Import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import org.springframework.web.context.request.async.DeferredResult;import javax.servlet.http.HttpServletRequest;import java.beans.PropertyEditorSupport;import java.time.Instant;import java.time.LocalDate;import java.time.ZoneId;import java.time.format.DateTimeFormatter;import java.util.Date;import java.util.concurrent.TimeUnit / * RestController@RequestMapping ("/ message/*") @ Slf4j@Api (tags = "asynchronous processing") public class AsyncController {@ Autowired private ThreadPoolTaskExecutor threadPoolTaskExecutor; private MyThreadTask task; private MessageHandler messageHandler; / * date conversion * @ param * @ return * / private static final DateTimeFormatter LOCAL_DATE_FORMAT = DateTimeFormatter.ofPattern ("yyyy-MM-dd") @ InitBinder public void initBinder (WebDataBinder binder) {binder.registerCustomEditor (Date.class,new PropertyEditorSupport () {@ Override public void setAsText (String text) throws IllegalArgumentException {LocalDate localDate = LocalDate.parse (text,LOCAL_DATE_FORMAT); Instant instant = localDate.atStartOfDay (). AtZone (ZoneId.systemDefault ()). ToInstant (); super.setValue (Date.from (instant)) }}); @ GetMapping ("runnable") @ ApiOperation ("exception handling Runnable") public Object message (String message) {log.info ("external thread: {}", Thread.currentThread (). GetName ()); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes ()). GetRequest (); DeferredResult result = new DeferredResult (6000L) / / set asynchronous response this.threadPoolTaskExecutor.execute (new Runnable () {/ / thread core task @ SneakyThrows public void run () {log.info ("internal thread: {}", Thread.currentThread () .getName ()); TimeUnit.SECONDS.sleep (7); result.setResult ("[echo]" + message) / / execute the final response result.onCompletion (new Runnable () {/ / completion processing thread log.info ("completion thread: {}", Thread.currentThread () .getName ()); / / log output result.onTimeout (new Runnable () {log.info ("timeout thread: {}", Thread.currentThread () .getName () Result.setResult ("[request timeout]" + request.getRequestURI ()); / / timeout path return result; @ GetMapping ("task") @ ApiOperation ("task Asynchronous Task enabled") public Object messageTask (String message) {log.info ("external thread {}", Thread.currentThread (). GetName ()); this.task.startTaskHander (); return "[echo]" + message @ GetMapping ("webflux") @ ApiOperation ("Integration webflux") public Object echo (Message message) {log.info ("receive user information, the parameter sent by the user is message= {}", message); return this.messageHandler.echoHandler (message);} page response:
Console response:
2021-11-30 15 INFO 04V 06.946 INFO 22884-[nio-1999-exec-1] c.e.oldguy.myController.AsyncController: receives user information, and the parameter sent by the user is message=Message (title=pansd, pubdate=Tue Nov 30 00:00:00 CST 2021, content=come on baby)
2021-11-30 15 c.e.o.myWebFlux.handler.MessageHandler 04V 06.947 INFO 22884-[nio-1999-exec-1] c.e.o.myWebFlux.handler.MessageHandler: [http-nio-1999-exec-1] Business layer receives and processes data: Message (title=pansd, pubdate=Tue Nov 30 00:00:00 CST 2021, content=come on baby)
WebFlux response map and List//webFlux response set public Flux list (Message message) {List messageList = new ArrayList (); for (int item0witi
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.