prefs.put(ServiceContainerConfigurationConstants.CONFIG_SCHEMA_VERSION, ""
+ ServiceContainerConfigurationConstants.CURRENT_CONFIG_SCHEMA_VERSION);
prefs.put(ServiceContainerConfigurationConstants.DATA_DIRECTORY, "target");
prefs.put(ServiceContainerConfigurationConstants.CMDSERVICES, EchoCommandService.class.getName());
ServiceContainer sc = new ServiceContainer();
sc.start(prefs, new ClientCommandSenderConfiguration());
Thread.sleep(5000);
try {
// numberOfRetries tells jboss remoting effectively the number of seconds before declaring "cannot connect"
RemoteCommunicator comm = new JBossRemotingRemoteCommunicator(
"socket://127.0.0.1:" + CommTestConstants.CONNECTOR_BIND_PORT + "/?force_remote=true&numberOfRetries=2");
ClientCommandSenderConfiguration config = new ClientCommandSenderConfiguration();
config.maxRetries = 5;
config.retryInterval = 500L;
config.defaultTimeoutMillis = 4000L;
sender = new ClientCommandSender(comm, config);
sender.startSending();
// sanity check - make sure we can call it
Command cmd = createNewCommand("hello");
assert sender.sendSynch(cmd).getResults().toString().equals("hello");
// try to send a command that can never make it - we should never retry this one
cmd = createNewCommand(new NotSerializable());
TestCommandResponseCallback callback = new TestCommandResponseCallback();
synchronized (callback) {
sender.sendAsynchGuaranteed(cmd, callback);
callback.wait(3000L); // should not retry so should go fast
}
assert callback.response != null;
assert callback.response.getCommand() != null;
assert !callback.response.isSuccessful() : "Command wasn't serializable, should have failed: "
+ callback.response;
assert !callback.response.getCommand().getConfiguration().containsKey("rhq.retry") : "Should not have retried at all: "
+ callback.response;
sc.shutdown();
Thread.sleep(2000L); // give the server container time to shutdown
// shutdown listener and try to send - should retry forever due to cannot-connect exception (max-retries will be ignored)
cmd = createNewCommand("forever");
callback = new TestCommandResponseCallback();
synchronized (callback) {
sender.sendAsynchGuaranteed(cmd, callback);
callback.wait((config.maxRetries * (config.retryInterval + config.defaultTimeoutMillis)) + 1000); // we will not get a callback - this proves max-retries doesn't take effect
}
assert callback.response == null : "Server was shut down, and we should have retried forever; should not have received a response yet: "
+ callback.response;
// now restart the server and the command should immediately get triggered
synchronized (callback) {
sc.start(prefs, new ClientCommandSenderConfiguration());
callback.wait(5000); // should get notified very fast, probably within a second
}
assert callback.response != null : "Command should have been finished by now";
assert callback.response.isSuccessful() : "Command should have been successful: " + callback.response;
assert callback.response.getResults().toString().equals("forever");
assert Integer.parseInt(callback.response.getCommand().getConfiguration().getProperty("rhq.retry")) >= config.maxRetries : "Should have retried: "
+ callback.response;
} finally {
if (sender != null) {
sender.stopSending(false);
}
sc.shutdown();
}
}