String locatorURI = "socket://" + host + ":" + port;
InvokerLocator locator = new InvokerLocator(locatorURI);
HashMap serverConfig = new HashMap();
serverConfig.put(ServerInvoker.MAX_NUM_ONEWAY_THREADS_KEY, "2");
serverConfig.put(ServerInvoker.MAX_ONEWAY_THREAD_POOL_QUEUE_SIZE, "1");
Connector connector = new Connector(locator, serverConfig);
connector.create();
TestHandler handler = new TestHandler();
connector.addInvocationHandler("test", handler);
connector.start();
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
Client client = new Client(locator, clientConfig);
client.connect();
Object response = client.invoke(FAST);
assertEquals(FAST, response);
long start = System.currentTimeMillis();
// This invocation should run in pooled thread 1.
log.info("making 1st oneway invocation");
client.invokeOneway(SLOW + "1", null, false);
poolCounter++;
// Wait for the connection to return to the pool.
Thread.sleep(500);
// This invocation should run in pooled thread 2.
log.info("making 2nd oneway invocation");
client.invokeOneway(SLOW + "2", null, false);
// Wait for the connection to return to the pool.
Thread.sleep(500);
// This invocation should use the pooled connection and go into the queue.
log.info("making 3rd oneway invocation");
client.invokeOneway(SLOW + "3", null, false);
// Wait for the connection to return to the pool.
Thread.sleep(500);
// This invocation should use the pooled connection and get run by the
// ServerThread. The connection should go back into the pool but the
// ServerThread will be busy for the next 5 seconds.
log.info("making 4th oneway invocation");
client.invokeOneway(SLOW + "4", null, false);
// Wait for the connection to return to the pool.
Thread.sleep(500);
// This invocation should use the pooled connection and have to wait
// for 5 seconds.
log.info("making 5th oneway invocation");
client.invokeOneway(SLOW + "5", null, false);
assertTrue((System.currentTimeMillis() - start < 3000));
assertEquals(4, handler.startedCount);
// It's necessary to wait for more than 5000 ms here because one or two
// of the invocations might go out over preexisting pooled connections
// and have to wait for the handler to finish the previous invocation.
Thread.sleep(6000);
assertEquals(6, handler.startedCount);
client.disconnect();
connector.stop();
log.info(getName() + " PASSES");
}