I am developing a simple test framework in Java that needs to simulate weblogic deployments by responding to JMS calls. One of the the test configuration options is a delay to simulate latency. I was wondering if anyone has any good ideas on how to do this. I was jsut going to create a TimerTask to handle it, is there a better way? Thanks.
+2
A:
How about just
Thread.sleep(10 + new Random().nextInt(20)); // waits between 10 - 30 ms.
i.e. in the code that responds to the JMS call you just use this to simulate a random latency delay.
mikera
2010-06-30 21:01:21
This was my original thought, the problem is that this ties up the main thread, which is not an accurate simulation of our system. I would like the application to be able to respond to the next message if necessary before returning the delayed response.
Igman
2010-06-30 21:23:57
@Igman hmmm in that case I understand your issue but that seems odd architecturally. Aren't you spawning a new thread or using a thread pool to deal with incoming JMS requests?
mikera
2010-06-30 22:20:55
Quartz looks like it may be a bit to large for my purposes, mainly because I'm looking a having a few seconds of delay. Quartz seems to be more geared towards regularly scheduled tasks.
Igman
2010-06-30 21:16:36
+2
A:
You can create a ScheduledExecutorService to perform 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 the Concurrency Backport for JDK 1.4. It works for JDK 1.2 to JDK 6
Peter Lawrey
2010-06-30 21:29:22
This would probably be my preferred choice, but unfortunately I am using java 1.4.
Igman
2010-07-01 00:34:30
+3
A:
Create an Object that mocks the server. When it "receives" your input, have it spawn a new thread to "handle the connection" like a real server would. That spawned thread can Thread.sleep() to its heart's content to simulate both send and receive lag, as well as allow you to mock some server-state properties that might be useful during testing.
glowcoder
2010-06-30 21:40:23