if (topicSubscriptions == null) {
cb.operationFailed(ctx, new PubSubException.ServerNotResponsibleForTopicException(""));
return;
}
final ByteString subscriberId = subRequest.getSubscriberId();
InMemorySubscriptionState subscriptionState = topicSubscriptions.get(subscriberId);
CreateOrAttach createOrAttach = subRequest.getCreateOrAttach();
if (subscriptionState != null) {
if (createOrAttach.equals(CreateOrAttach.CREATE)) {
String msg = "Topic: " + topic.toStringUtf8() + " subscriberId: " + subscriberId.toStringUtf8()
+ " requested creating a subscription but it is already subscribed with state: "
+ SubscriptionStateUtils.toString(subscriptionState.getSubscriptionState());
logger.debug(msg);
cb.operationFailed(ctx, new PubSubException.ClientAlreadySubscribedException(msg));
return;
}
// otherwise just attach
if (logger.isDebugEnabled()) {
logger.debug("Topic: " + topic.toStringUtf8() + " subscriberId: " + subscriberId.toStringUtf8()
+ " attaching to subscription with state: "
+ SubscriptionStateUtils.toString(subscriptionState.getSubscriptionState()));
}
cb.operationFinished(ctx, subscriptionState.getLastConsumeSeqId());
return;
}
// we don't have a mapping for this subscriber
if (createOrAttach.equals(CreateOrAttach.ATTACH)) {
String msg = "Topic: " + topic.toStringUtf8() + " subscriberId: " + subscriberId.toStringUtf8()
+ " requested attaching to an existing subscription but it is not subscribed";
logger.debug(msg);
cb.operationFailed(ctx, new PubSubException.ClientNotSubscribedException(msg));
return;
}
// now the hard case, this is a brand new subscription, must record
final SubscriptionState newState = SubscriptionState.newBuilder().setMsgId(consumeSeqId).build();
createSubscriptionState(topic, subscriberId, newState, new Callback<Void>() {
@Override
public void operationFailed(Object ctx, PubSubException exception) {
cb.operationFailed(ctx, exception);
}
@Override
public void operationFinished(Object ctx, Void resultOfOperation) {
Callback<Void> cb2 = new Callback<Void>() {
@Override
public void operationFailed(Object ctx, PubSubException exception) {
logger.error("subscription for subscriber " + subscriberId.toStringUtf8() + " to topic "
+ topic.toStringUtf8() + " failed due to failed listener callback", exception);
cb.operationFailed(ctx, exception);
}
@Override