Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to push DeferredResult on spring mvc server

Shulou Source: shulou.com Published: 2022-06-02 02:12:09 10月14日 Update

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.

Tags: Threads push services more knowledge examples articles practical that is work meetings articles look knowledge points industry details information information channels follow this is part of Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Microsoft vpn Shulou Information Xiaomi