[英]How to send delay Response in efficient way to client
I have requirement to mock some of the service calls, but i need to send response after waiting a period of time. 我需要模拟一些服务调用,但是我需要等待一段时间后发送响应。
I know i can do this by using Thread.sleep(n)
, but i'm not comfortable to make complete thread to sleep for certain time. 我知道我可以通过使用Thread.sleep(n)
来做到这一点,但是我不愿意让完整的线程休眠一段时间。
So i'm look if RestTemplate has any method like setResponseTime()
to set Response delay time. 所以我看一下RestTemplate是否具有像setResponseTime()
这样的任何方法来设置响应延迟时间。 or can i achieve this by using any another external dependency ? 或者我可以通过使用任何其他外部依赖关系来实现这一目标?
You can use TimeUnit such as 您可以使用TimeUnit如
- TimeUnit.NANOSECONDS.sleep(timeout);
- TimeUnit.SECONDS.sleep()
- TimeUnit.MINUTES.sleep()
- ....
Or you can explore the interface ScheduledExecutorService, they have a method name scheduleWithFixedDelay. 或者,您可以浏览ScheduledExecutorService接口,它们具有方法名称scheduleWithFixedDelay。
Thanks, 谢谢,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.