public void test() throws Exception {
// To run in an IDE, uncomment and update this line
System.setProperty("version", OpenEjbVersion.get().getVersion());
final Duration reconnectDelay = new Duration("1 second");
final File zip = Repository.getArtifact("org.apache.openejb", "openejb-standalone", "zip");
final File app = Repository.getArtifact("org.apache.openejb.itests", "failover-ejb", "jar");
final File dir = Files.tmpdir();
System.setProperty("openejb.client.requestretry", "true");
final Map<String, StandaloneServer> servers = new HashMap<String, StandaloneServer>();
for (final String name : new String[]{"red", "green", "blue"}) {
final File home = new File(dir, name);
Files.mkdir(home);
Zips.unzip(zip, home, true);
final StandaloneServer server = new StandaloneServer(home, home);
server.killOnExit();
server.ignoreOut();
server.getJvmOpts().add("-Dopenejb.classloader.forced-load=org.apache.openejb");
server.getProperties().put("name", name);
server.getProperties().put("openejb.extract.configuration", "false");
IO.copy(app, Files.path(home, "apps", "itest.jar"));
IO.copy(IO.read("<openejb><Deployments dir=\"apps/\"/></openejb>"), Files.path(home, "conf", "openejb.xml"));
final StandaloneServer.ServerService ejbd = server.getServerService("ejbd");
ejbd.setDisabled(false);
ejbd.setBind("0.0.0.0");
ejbd.setPort(getAvailablePort());
ejbd.setThreads(5);
ejbd.set("discoveryHost", "localhost");
ejbd.set("discovery", "ejb:ejbd://{discoveryHost}:{port}/" + name);
final StandaloneServer.ServerService multipoint = server.getServerService(MULTIPOINT);
multipoint.setBind("0.0.0.0");
multipoint.setPort(getAvailablePort());
multipoint.setDisabled(false);
multipoint.set("discoveryHost", "localhost");
multipoint.set("discoveryName", name);
multipoint.set("reconnectDelay", reconnectDelay.toString());
servers.put(name, server);
}
final StandaloneServer red = servers.get("red");
// Set all the initialServers to point to RED
for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
final StandaloneServer server = entry.getValue();
final StandaloneServer.ServerService multipoint = server.getServerService(MULTIPOINT);
final String value = "lOcAlHosT:" + red.getServerService(MULTIPOINT).getPort() + ",locALHost:"+multipoint.getPort();
multipoint.set("initialServers", value);
}
// Start all the servers except RED
for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
if (entry.getKey().equals("red")) continue;
entry.getValue().start(1, TimeUnit.MINUTES);
}
// Verify Failover is not yet functional
{
// RED was never started so BLUE never found any peers
// Lets invoke BLUE then shut it down and verify we have
// no other peers to invoke
final StandaloneServer blue = servers.get("blue");
final Properties environment = new Properties();
environment.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
environment.put(Context.PROVIDER_URL, "ejbd://localhost:" + blue.getServerService("ejbd").getPort());
final InitialContext context = new InitialContext(environment);
final Calculator bean = (Calculator) context.lookup("CalculatorBeanRemote");
// Invoke BLUE a few times
invoke(bean, 10, "blue");
// Kill BLUE
blue.kill();
// Invocations should now fail (and not failover)
try {
bean.name();
Assert.fail("Server should be down and failover not hooked up");
} catch (final Exception e) {
// pass
}
}
// Now we start RED
red.start(1, TimeUnit.MINUTES);
// Wait for the reconnectDelay so GREEN can find RED
Thread.sleep((long) (reconnectDelay.getTime(TimeUnit.MILLISECONDS) * 1.5));
// Verify Failover is now functional
{
// RED was never started so GREEN never found any peers