public void testAsyncCallWithHandlerAndMultipleClients() throws Exception {
URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
assertNotNull(wsdl);
SOAPService service = new SOAPService(wsdl, serviceName);
ExecutorService executor = Executors.newFixedThreadPool(5);
service.setExecutor(executor);
assertNotNull(service);
final MyHandler h = new MyHandler();
MyHandler.invocationCount = 0;
final String expectedString = new String("How are you Joe");
class Poller extends Thread {
Future<?> future;
int tid;
Poller(Future<?> f, int t) {
future = f;
tid = t;
}
public void run() {
if (tid % 2 > 0) {
while (!future.isDone()) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
// ignore
}
}
}
try {
future.get();
} catch (Exception ex) {
fail("Poller " + tid + " failed with " + ex);
}
assertEquals("callback was not executed or did not return the expected result",
expectedString, h.getReplyBuffer());
}
}
Greeter greeter = (Greeter)service.getPort(portName, Greeter.class);
Future<?> f = greeter.greetMeSometimeAsync("Joe", h);
Poller[] pollers = new Poller[4];
for (int i = 0; i < pollers.length; i++) {
pollers[i] = new Poller(f, i);
}
for (Poller p : pollers) {
p.start();
}
for (Poller p : pollers) {
p.join();
}
assertEquals(1, MyHandler.invocationCount);
executor.shutdown();
}