final Properties environment = new Properties();
environment.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
environment.put(Context.PROVIDER_URL, "ejbd://localhost:" + servers.values().iterator().next().getServerService("ejbd").getPort() + "/provider");
final InitialContext context = new InitialContext(environment);
final Calculator bean = (Calculator) context.lookup("CalculatorBeanRemote");
String previous = null;
for (StandaloneServer ignored : servers.values()) {
logger.info("Looping");
// What server are we talking to now?
final String name = bean.name();
logger.info("Sticky request to " + name);
// The root should not be serving apps
assertFalse("root".equals(name));
// Should not be the same server we were talking with previously (we killed that server)
if (previous != null) assertFalse(name.equals(previous));
previous = name;
final int i = 1000;
logger.info(String.format("Performing %s invocations, expecting %s to be used for each invocation.", i, name));
// Should be the same server for the next N calls
invoke(bean, i, name);
logger.info("Shutting down " + name);
// Now let's kill that server
servers.get(name).kill();
}
logger.info("All Servers Shutdown");
try {
logger.info("Making one last request, expecting complete failover");
final String name = bean.name();
Assert.fail("Server should be destroyed: " + name);
} catch (EJBException e) {
logger.info(String.format("Pass. Request resulted in %s: %s", e.getCause().getClass().getSimpleName(), e.getMessage()));
// good
}
// Let's start a server again and invocations should now succeed
final Iterator<StandaloneServer> iterator = servers.values().iterator();
iterator.next();
final StandaloneServer server = iterator.next();
logger.info(String.format("Starting %s server", server.getProperties().get("name")));
server.start(1, TimeUnit.MINUTES);
logger.info("Performing one more invocation");
assertEquals(5, bean.sum(2, 3));
}