@Override
public Subscription subscribe(final String subject, final MessageCallback receiver) {
if (reservedNames.contains(subject))
throw new IllegalArgumentException("cannot modify or subscribe to reserved service: " + subject);
final DeliveryPlan plan = createOrAddDeliveryPlan(subject, receiver);
globalSubscriptions.add(subject);
fireSubscribeListeners(new SubscriptionEvent(false, null, plan.getTotalReceivers(), true, subject));
return new Subscription() {
@Override
public void remove() {
if (removeFromDeliveryPlan(subject, receiver).getTotalReceivers() == 0) {
globalSubscriptions.remove(subject);
subscriptions.remove(subject);
}
else {
boolean nonRemote = true;
for (final MessageCallback callback : plan.getDeliverTo()) {
if (!(callback instanceof RemoteMessageCallback)) {
nonRemote = false;
break;
}
}