In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how the spring mvc server pushes DeferredResult. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Example 1:
Package com.example.demo.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.context.request.async.DeferredResult;import java.time.LocalDateTime;import java.util.concurrent.TimeUnit;@Controllerpublic class PushController {private static final Logger LOGGER = LoggerFactory.getLogger (PushController.class) @ RequestMapping ("/ deferre") @ ResponseBody public DeferredResult DeferredCall () {LOGGER.info ("--request start--"); try {return handle ();} finally {LOGGER.info ("--request end -");}} private DeferredResult handle () {DeferredResult deferredResult = new DeferredResult () New Thread (()-> {try {TimeUnit.SECONDS.sleep (3);} catch (InterruptedException e) {e.printStackTrace ();} LOGGER.info ("- fill value -"); deferredResult.setResult (LocalDateTime.now (). ToString ());}. Start () Return deferredResult;}}
Example 2:
@ RestControllerpublic class PiceaServletContoller {@ Autowired private PiceaService piceaService; @ RequestMapping ("/ deferredresult") public DeferredResult deferredResult () throws Exception {System.out.println ("Control layer execution thread:" + Thread.currentThread () .getName ()); / / timeout DeferredResult deferredResult = new DeferredResult (10cm 1000L) DeferredResult.onTimeout (new Runnable () {@ Override public void run () {System.out.println ("Asynchronous thread execution timeout"); deferredResult.setResult ("thread execution timeout");}}) DeferredResult.onCompletion (new Runnable () {@ Override public void run () {System.out.println ("asynchronous execution completed");}}) FIXED_THREAD_POOL.execute (new Runnable () {@ Override public void run () {System.out.println ("Asynchronous execution Thread:" + Thread.currentThread (). GetName ()); try {String str = piceaService.task2 (); Thread.sleep (1000) DeferredResult.setResult ("this is an asynchronous request:" + str);} catch (InterruptedException e) {e.printStackTrace ();} catch (Exception e) {e.printStackTrace ();}); return deferredResult } / * * Thread Pool * / public static ExecutorService FIXED_THREAD_POOL = Executors.newFixedThreadPool (10);} above is how the spring mvc server pushes DeferredResult. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.