-1

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.

So i'm look if RestTemplate has any method like setResponseTime() to set Response delay time. or can i achieve this by using any another external dependency ?

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98

1 Answers1

2

You can use TimeUnit such as

- TimeUnit.NANOSECONDS.sleep(timeout);
- TimeUnit.SECONDS.sleep()
- TimeUnit.MINUTES.sleep()
- ....

Or you can explore the interface ScheduledExecutorService, they have a method name scheduleWithFixedDelay.

Thanks,

Nghia Do
  • 2,588
  • 2
  • 17
  • 31
  • i referred this https://stackoverflow.com/questions/9587673/thread-sleep-vs-timeunit-seconds-sleep – Ryuzaki L Jul 11 '18 at 19:33
  • So anything is not clear not you? or you still have questions? – Nghia Do Jul 11 '18 at 20:47
  • I mean i'm looking for method like this `setResponseWaitTime(long ms)`, so that i can configure, but this TimeUnit will internally call Thread.sleep() right? then what is the difference in using Thread.sleep() and TimeUnit class sleep()? – Ryuzaki L Jul 12 '18 at 15:08
  • If you want, you can create your wrap method and hide the logic inside it. And yes for your question, TimeUnit is just an utility with more features and functions – Nghia Do Jul 12 '18 at 16:06