for (PreInitializationListener listener : preInitializationListeners) {
listener.beforeInitialization();
}
remoteSubscribe(BuiltInServices.ServerEchoService.name());
directSubscribe(BuiltInServices.ClientBus.name(), new MessageCallback() {
@Override
@SuppressWarnings({"unchecked"})
public void callback(final Message message) {
switch (BusCommands.valueOf(message.getCommandType())) {
case RemoteSubscribe:
if (message.hasPart(MessageParts.SubjectsList)) {
for (String subject : (List<String>) message.get(List.class, MessageParts.SubjectsList)) {
remoteSubscribe(subject);
}
}
else {
String subject = message.get(String.class, Subject);
remoteSubscribe(subject);
}
break;
case RemoteUnsubscribe:
unsubscribeAll(message.get(String.class, Subject));
break;
case CapabilitiesNotice:
LogUtil.log("received capabilities notice from server. supported capabilities of remote: "
+ message.get(String.class, MessageParts.CapabilitiesFlags));
String[] capabilites = message.get(String.class, MessageParts.CapabilitiesFlags).split(",");
for (String capability : capabilites) {
switch (Capabilities.valueOf(capability)) {
case WebSockets:
webSocketUrl = message.get(String.class, MessageParts.WebSocketURL);
webSocketToken = message.get(String.class, MessageParts.WebSocketToken);
LogUtil.log("attempting web sockets connection at URL: " + webSocketUrl);
Object o = ClientWebSocketChannel.attemptWebSocketConnect(ClientMessageBusImpl.this, webSocketUrl);
if (o instanceof String) {
LogUtil.log("could not use web sockets. reason: " + o);
}
break;
case LongPollAvailable:
LogUtil.log("initializing long poll subsystem");
receiveCommCallback = new LongPollRequestCallback();
break;
case NoLongPollAvailable:
receiveCommCallback = new ShortPollRequestCallback();
if (message.hasPart(MessageParts.PollFrequency)) {
POLL_FREQUENCY = message.get(Integer.class, MessageParts.PollFrequency);
}
else {
POLL_FREQUENCY = 500;
}
break;
}
}
break;
case RemoteMonitorAttach:
break;
case FinishStateSync:
if (isInitialized()) {
return;
}
LogUtil.log("received FinishStateSync message. preparing to bring up the federation");
List<String> subjects = new ArrayList<String>();
for (String s : subscriptions.keySet()) {
if (s.startsWith("local:")) continue;
if (!remotes.containsKey(s)) subjects.add(s);
}
sessionId = message.get(String.class, MessageParts.ConnectionSessionKey);
remoteSubscribe(BuiltInServices.ServerBus.name());
MessageBuilder.createMessage()
.toSubject(BuiltInServices.ServerBus.name())
.command(RemoteSubscribe)
.with(MessageParts.SubjectsList, subjects)
.with(PriorityProcessing, "1")
.noErrorHandling()
.sendNowWith(ClientMessageBusImpl.this);
MessageBuilder.createMessage()
.toSubject(BuiltInServices.ServerBus.name())
.command(BusCommands.FinishStateSync)
.with(PriorityProcessing, "1")
.noErrorHandling().sendNowWith(ClientMessageBusImpl.this);
/**
* ... also send RemoteUnsubscribe signals.
*/
addSubscribeListener(new SubscribeListener() {
@Override
public void onSubscribe(SubscriptionEvent event) {
if (event.isLocalOnly() || event.getSubject().startsWith("local:")
|| remotes.containsKey(event.getSubject())) {
return;
}
if (event.isNew()) {
MessageBuilder.getMessageProvider().get().command(RemoteSubscribe)
.toSubject(BuiltInServices.ServerBus.name())
.set(Subject, event.getSubject())
.set(PriorityProcessing, "1")
.sendNowWith(ClientMessageBusImpl.this);
}
}
});
addUnsubscribeListener(new UnsubscribeListener() {
@Override
public void onUnsubscribe(SubscriptionEvent event) {
MessageBuilder.getMessageProvider().get().command(BusCommands.RemoteUnsubscribe)
.toSubject(BuiltInServices.ServerBus.name())
.set(Subject, event.getSubject())
.set(PriorityProcessing, "1")
.sendNowWith(ClientMessageBusImpl.this);
}
});
subscribe(DefaultErrorCallback.CLIENT_ERROR_SUBJECT, new MessageCallback() {
@Override
public void callback(Message message) {
String errorTo = message.get(String.class, MessageParts.ErrorTo);
if (errorTo == null) {
logError(message.get(String.class, MessageParts.ErrorMessage),