Simulate Latency in Java

I am developing a simple test environment in Java that should simulate a weblogic deployment by responding to JMS calls. One test configuration option is latency for latency modeling. I was wondering if anyone has any good ideas on how to do this. I had to create a TimerTask to handle it, is there a better way? Thanks.

+4
source share
4 answers

You can create a ScheduledExecutorService to do what you would do after a delay.

private final ScheduleExecutorService executor = Executors.newSingleThreadScheduledExecutor(); // is main loop, waits between 10 - 30 ms. executor.schedule(new Runnable() { public void run() { // my delayed task }}, 10 + new Random().nextInt(20), TimeUnit.MILLI_SECOND); 

EDIT: you can use Concurrency Backport for JDK 1.4. It works for JDK 1.2 to JDK 6

+3
source

Create an object that mocks the server. When it "receives" your input, ask it to create a new stream to "handle the connection" like a real server. This spawned thread can use Thread.sleep () to hold it in the heart, to simulate both a delay in sending and receiving, as well as allowing you to make fun of some server state properties that may be useful during testing.

+3
source

How about just

 Thread.sleep(10 + new Random().nextInt(20)); // waits between 10 - 30 ms. 

i.e. in the code that answers the JMS call, you simply use it to simulate a random delay delay.

+2
source

You can schedule tasks with Quartz , does this resolve the goal?

0
source

Source: https://habr.com/ru/post/1314376/


All Articles